Hibernate 已返回零结果,但本机 sql 返回不同
Posted
技术标签:
【中文标题】Hibernate 已返回零结果,但本机 sql 返回不同【英文标题】:Hibernate has returns the zero results but native sql returns different 【发布时间】:2016-11-10 01:13:43 【问题描述】:我想知道为什么我的hibernait查询和sql的结果不同?我映射错了吗? Java 休眠没有例外。在这种情况下,我使用 Oracle 11 数据库和 hibernate 3.5。
我有下一个休眠实体:
@Table(name = "pat_otb_registry")
public class OtbEntity implements Comparable<OtbEntity>
@Column(name = "legal_address_region")
private BigDecimal legalAddressRegion;
@Column(name = "declarant")
private String declarant;
@Column(name = "registry_cert_number")
private String registryCertNumber;
@Column(name = "decision_to_grant_date")
private Date decisionToGrantDate;
@Column(name = "competent_authority")
private BigDecimal competentAuthority;
@Column(name = "expiry_date")
private Date expiryDate;
@Column(name = "decision_to_grant_number")
private String decisionToGrantNumber;
@Column(name = "decision_to_cancel_number")
private String decisionToCancelNumber;
@Column(name = "decision_to_exclude_number")
private String decisionToExcludeNumber;
@Column(name = "certification_authority")
private String certificationAuthority;
@Column(name = "appraisee")
private String appraisee;
@Column(name = "date_of_end_stage")
private Date dateOfEndStage;
@Column(name = "certification_field")
private String certificationField;
@Column(name = "validity_years")
private BigDecimal validityYears;
@Column(name = "decision_to_extend_number")
private String decisionToExtendNumber;
@Column(name = "issue_date")
private Date issueDate;
@Column(name = "reason")
private String reason;
@Column(name = "decision_to_cancel_date")
private Date decisionToCancelDate;
@Column(name = "decision_to_exclude_date")
private Date decisionToExcludeDate;
@Column(name = "decision_to_extend_date")
private Date decisionToExtendDate;
@Column(name = "extended_to_date")
private Date extendedToDate;
@Column(name = "oa_registry_number")
private String oaRegistryNumber;
@Column(name = "grounds_for_issue")
private String groundsForIssue;
@Column(name = "doc_num")
private String docNum;
// then getters and setters....
SQL 表:
CREATE TABLE pat_otb_registry (
legal_address_region number(10,0) NULL,
declarant nvarchar2(2000) NOT NULL,
registry_cert_number nvarchar2(2000) NOT NULL,
decision_to_grant_date date NULL,
competent_authority smallint NOT NULL,
expiry_date date NULL,
decision_to_grant_number nvarchar2(2000) NULL,
decision_to_cancel_number nvarchar2(2000) NULL,
decision_to_exclude_number nvarchar2(2000) NULL,
certification_authority nvarchar2(2000) NOT NULL,
appraisee varchar2(2000) NOT NULL,
date_of_end_stage date NULL,
certification_field nvarchar2(2000) NULL,
validity_years integer NULL,
decision_to_extend_number nvarchar2(2000) NULL,
issue_date date NULL,
reason nvarchar2(2000) NULL,
decision_to_cancel_date date NULL,
decision_to_exclude_date date NULL,
decision_to_extend_date date NULL,
extended_to_date date NULL,
oa_registry_number nvarchar2(2000) NULL,
grounds_for_issue nvarchar2(2000) NULL,
doc_num nvarchar2(2000) NULL,
CONSTRAINT pat_otb_registry_pk PRIMARY KEY (registry_cert_number)
) ;
休眠查询:
getEntityManager().createQuery("SELECT o FROM " + getEntityName() + " o").getResultList()
SQL 查询:
getEntityManager().createNativeQuery("select * from pat_otb_registry")
【问题讨论】:
除了我没有看到你的主键的@Id 注释,它看起来对我来说还可以。我没有耐心检查所有列名,但我认为如果命名错误,它应该在部署期间抱怨。 本机 SQL 查询和休眠请求的一些示例肯定有助于理解问题 直觉告诉我,这与使用 NVARCHAR2 列作为主键的选择/原因有关。涉及的所有相关字符集是什么? 请将休眠和原生 SQL 查询附加到问题中。 没有@Id,因为它是仅供阅读的表格,我检查了所有名称,并且名称相等... 【参考方案1】:我发现了一个错误。我没有正常工作EntityManager - getEntityManager()
【讨论】:
以上是关于Hibernate 已返回零结果,但本机 sql 返回不同的主要内容,如果未能解决你的问题,请参考以下文章
Google 放置 getPlaceID 返回非空数组,但过滤器返回零结果
本机 SQL(用 xml 编写)结果集映射到 POJO(基于注释)