I'm wondering if I'm looking at a bug in uniqueResult() or if I'm not understanding the documentation.
I have the following code snippet:
Code:
long duplicateId = 38440L;
Session session = getHibernateSession();
Query query = session.createQuery("from GenoAssayUploadInfo aui" +
" where aui.assay.assayId = :id");
query.setParameter("id", duplicateId);
List list = query.list();
System.out.println("List size: " + list.size());
// Prints 2, which is correct.
GenoAssayUploadInfo aui = (GenoAssayUploadInfo) query.uniqueResult();
System.out.println("Unique result: " + aui);
// Returns a unique result ?
Which yields the following (show sql enabled):
[testng] Hibernate: select genoassayu0_.assay_id as assay1_285_, genoassayu0_.upload_group_id as upload26_285_, genoassayu0_.assay_pk_ext as assay2_285_, genoassayu0_.assay_id_ext as assay3_285_, genoassayu0_.amp_len_ext as amp4_285_, genoassayu0_.up_conf_ext as up5_285_, genoassayu0_.mp_conf_ext as mp6_285_, genoassayu0_.tm_gc_ext as tm7_285_, genoassayu0_.pc_gc_ext as pc8_285_, genoassayu0_.pwarn_ext as pwarn9_285_, genoassayu0_.probe_mass_ext as probe10_285_, genoassayu0_.callexte1 as callexte11_285_, genoassayu0_.massexte1 as massexte12_285_, genoassayu0_.seqexte1 as seqexte13_285_, genoassayu0_.callexte2 as callexte14_285_, genoassayu0_.massexte2 as massexte15_285_, genoassayu0_.seqexte2 as seqexte16_285_, genoassayu0_.callexte3 as callexte17_285_, genoassayu0_.massexte3 as massexte18_285_, genoassayu0_.seqexte3 as seqexte19_285_, genoassayu0_.callexte4 as callexte20_285_, genoassayu0_.massexte4 as massexte21_285_, genoassayu0_.seqexte4 as seqexte22_285_, genoassayu0_.first_pause_ext as first23_285_, genoassayu0_.well_ext as well24_285_, genoassayu0_.hap_map_snp_class as hap25_285_ from geno_assay_upload_info genoassayu0_ where genoassayu0_.assay_id=?
[testng] List size: 2
[testng] Hibernate: select genoassayu0_.assay_id as assay1_285_, genoassayu0_.upload_group_id as upload26_285_, genoassayu0_.assay_pk_ext as assay2_285_, genoassayu0_.assay_id_ext as assay3_285_, genoassayu0_.amp_len_ext as amp4_285_, genoassayu0_.up_conf_ext as up5_285_, genoassayu0_.mp_conf_ext as mp6_285_, genoassayu0_.tm_gc_ext as tm7_285_, genoassayu0_.pc_gc_ext as pc8_285_, genoassayu0_.pwarn_ext as pwarn9_285_, genoassayu0_.probe_mass_ext as probe10_285_, genoassayu0_.callexte1 as callexte11_285_, genoassayu0_.massexte1 as massexte12_285_, genoassayu0_.seqexte1 as seqexte13_285_, genoassayu0_.callexte2 as callexte14_285_, genoassayu0_.massexte2 as massexte15_285_, genoassayu0_.seqexte2 as seqexte16_285_, genoassayu0_.callexte3 as callexte17_285_, genoassayu0_.massexte3 as massexte18_285_, genoassayu0_.seqexte3 as seqexte19_285_, genoassayu0_.callexte4 as callexte20_285_, genoassayu0_.massexte4 as massexte21_285_, genoassayu0_.seqexte4 as seqexte22_285_, genoassayu0_.first_pause_ext as first23_285_, genoassayu0_.well_ext as well24_285_, genoassayu0_.hap_map_snp_class as hap25_285_ from geno_assay_upload_info genoassayu0_ where genoassayu0_.assay_id=?
[testng] Unique result: edu.mit.broad.espcore.datavo.geno.GenoAssayUploadInfo@c9537b
Shouldn't uniqueResult() always throw an exception if list.size() > 1 ?
Now, for full disclosure, it turns out that there are some inconsistencies with the data in the database.
The GenoAssayUploadInfo and GenoAssay classes looks like this:
Code:
@Entity
@Table(name="GENO_ASSAY_UPLOAD_INFO")
public class GenoAssayUploadInfo implements Serializable {
@Id
private Long assayId;
@OneToOne
@PrimaryKeyJoinColumn
private GenoAssay assay;
// remainder omitted for brevity...
}
@Entity
@Table(name="GENO_ASSAY")
public class GenoAssay implements Serializable {
@Id
@Column(nullable=false)
@GeneratedValue(generator="assaySeqGen" )
@GenericGenerator(name="assaySeqGen", strategy = "sequence",
parameters = {
@Parameter(name="sequence", value="SQ_GENO_ASSAY")
}
)
private Long assayId;
// remainder omitted for brevity...
}
GENO_ASSAY_UPLOAD_INFO.ASSAY_ID is
supposed to be unique
in the database, but there's no unique constraint (for historical reasons - don't ask...), and in this case there's a duplicate record with ASSAY_ID 38440.
In this case, I was expecting uniqueResult() to throw an exception complaining about multiple results, but instead it seems to be picking one at random. Is this correct behavior of uniqueResult?
Hibernate version: 3.2
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using: Oracle 10g
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Thanks in advance.