Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null list returned from hibernate query with embedded id

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.

When I run the following query:

Criteria criteria = session.createCriteria(getPersistentClass());
criteria.add(Restrictions.eq("id.profPropertyId", profileExtensionName));
Object obj = criteria.list();
log.info(obj);
return (List<ProfileExtensions>) obj; 

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.

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;
  }
}
like image 814
mransley Avatar asked Oct 18 '10 22:10

mransley


1 Answers

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;
like image 158
user405935 Avatar answered Sep 25 '22 05:09

user405935