从带有嵌入ID的休眠查询返回的空列表 [英] Null list returned from hibernate query with embedded id
问题描述
我有一个嵌入式密钥的实体。实体只有键作为字段,键有7个字段,其中一些可以为空。
I have an entity with an embedded key. The entity has only the key as a field and the key has 7 fields, some of which can be null.
当我运行以下查询时:
Criteria criteria = session.createCriteria(getPersistentClass());
criteria.add(Restrictions.eq("id.profPropertyId", profileExtensionName));
Object obj = criteria.list();
log.info(obj);
return (List<ProfileExtensions>) obj;
我得到正确数量的结果,但每个结果都为空(即,我得到一个4000空对象)。我曾尝试使用HQL查询和条件,但它们都给出了相同的结果。
I get the correct number of results, but each result is null (i.e. I get a list of 4000 null objects). I have tried using both a HQL query and criteria but they both give the same result.
映射类是从现有数据库生成的。
The mapping classes were generated from an existing database.
为查询生成的SQL如下所示:
The SQL generated for the query is as follows
select this_.PROF_DATA_TYPE as PROF1_14_0_, this_.PROF_EXT_KEY as PROF2_14_0_,
this_.PROF_KEY as PROF3_14_0_, this_.PROF_NAME as PROF4_14_0_,
this_.PROF_PROPERTY_ID as PROF5_14_0_, this_.PROF_VALUE as PROF6_14_0_,
this_.PROF_VALUE_EXTENDED as PROF7_14_0_
from EMPINST.PROFILE_EXTENSIONS this_
where this_.PROF_PROPERTY_ID=?
看起来它应该返回正确的数据。
Looks like it should return the correct data.
映射文件非常简单(并且从模式生成):
The mapping file is pretty simple (and generated from the schema:
// Generated Oct 18, 2010 11:08:08 PM by Hibernate Tools 3.2.2.GA
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name="PROFILE_EXTENSIONS"
,schema="EMPINST"
, uniqueConstraints = @UniqueConstraint(columnNames={"PROF_KEY", "PROF_PROPERTY_ID"}) )
public class ProfileExtensions implements java.io.Serializable {
private ProfileExtensionsId id;
public ProfileExtensions() {
}
public ProfileExtensions(ProfileExtensionsId id) {
this.id = id;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="profKey", column=@Column(name="PROF_KEY", nullable=false, length=36) ),
@AttributeOverride(name="profPropertyId", column=@Column(name="PROF_PROPERTY_ID", nullable=false, length=64) ),
@AttributeOverride(name="profExtKey", column=@Column(name="PROF_EXT_KEY", length=256) ),
@AttributeOverride(name="profName", column=@Column(name="PROF_NAME", length=256) ),
@AttributeOverride(name="profDataType", column=@Column(name="PROF_DATA_TYPE", length=64) ),
@AttributeOverride(name="profValue", column=@Column(name="PROF_VALUE", length=1024) ),
@AttributeOverride(name="profValueExtended", column=@Column(name="PROF_VALUE_EXTENDED") ) } )
public ProfileExtensionsId getId() {
return this.id;
}
public void setId(ProfileExtensionsId id) {
this.id = id;
}
}
推荐答案
一般来说,将可为空的列作为组合ID的一部分可能不是一个好主意。在我的情况下,它是必需的,因为我有一个使用外连接创建的视图,并且我遇到了与您所描述的完全相同的问题。我能够使用 https://hibernate.atlassian.net/browse/上提供的信息来解决此问题HHH-1109 。我使用附件中提供的NullableStringType来访问这个Hibernate Jira。在我使用的复合id类中:
In general it's probably not a good idea to have nullable columns as a part of composite id. In my case it was required because I had a view created using outer join and I faced exactly the same problem as you described. I was able to solve it using information provided on https://hibernate.atlassian.net/browse/HHH-1109. I used NullableStringType provided in the attachment to this Hibernate Jira. In my composite id class I used:
@Type(type = "nl.pinkroccade.quarant.common.model.hibernate.type.NullableStringType")
private String nullableField;
这篇关于从带有嵌入ID的休眠查询返回的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!