Hibernate - TypedQuery.getResultList () returns a list of the same object

The following is a more detailed description of the problem:

I am making a simple select request. The returned list contains the exact number of records / objects, as if I were making the same query in the database, but the problem is that all objects are the same / identical.

For example, this is the result in the database (I deleted the null values):
26801 01-JAN-00 7 31-DEC-99 7 Obere Kirchstr. 26 CH 8304 Walliselln
26801 01-JAN-00 2 31-DEC-99 2 Obere Kirchstr. 26 CH 8304 Walliselln

And this is the contents of the addresses of the variables, as I see it in Eclipse during debugging after the query has completed:

ArrayList addresses (id = 81)
elementData Object [10] (id = 86)
[0] DLDBAddress (id = 82)
[1] DLDBAddress (id = 82)
[2] null
...
modCount 1
size 2

DLDBAddress [persid = 26801, valPeriodStart = 1900-01-01, valPeriodEnd = 9999-12-31, address Nr = 7, address Type = 7, address Row1 = null, address Row2 = Obere Kirchstr. 26, addressRow3 = null, country = CH, postalCode = 8304, city = Walliselln, phoneNr = null, faxNr = null, sekretaryPhoneNr = null, alternatPhoneNr = null, pagerNr = null]

DLDBAddress [persid = 26801, valPeriodStart = 1900-01-01, valPeriodEnd = 9999-12-31, address Nr = 7, address Type = 7, address Row1 = null, address Row2 = Obere Kirchstr. 26, addressRow3 = null, country = CH, postalCode = 8304, city = Walliselln, phoneNr = null, faxNr = null, sekretaryPhoneNr = null, alternatPhoneNr = null, pagerNr = null]]

As you can see, both objects are identical. They shoul instead differ from addressNr and addressType ...

This is the part of the code where I create the request:

public static <T> List<T> findBy(EntityManager eM, Class<T> boClass, String whereClause, String whereValue) { EntityManager entityManager = eM; Query query = entityManager.createQuery("from " + boClass.getName() + " s where s." + whereClause + " = " + whereValue); ... return (List<T>) query.getResultList(); } 

and this is a (fairly simple) query result:

from ch.ethz.id.wai.pdb.bo.DLDBAddress s, where s.persid = 26801

This is the generated request:

 Hibernate: select dldbaddres0_.PERSID as PERSID0_, dldbaddres0_.ADRNUM as ADRNUM0_, dldbaddres0_.ADRZEIL1 as ADRZEIL3_0_, dldbaddres0_.ADRZEIL2 as ADRZEIL4_0_, dldbaddres0_.ADRZEIL3 as ADRZEIL5_0_, dldbaddres0_.ADRTYP as ADRTYP0_, dldbaddres0_.ADRAUSWTEL as ADRAUSWTEL0_, dldbaddres0_.ADRORT as ADRORT0_, dldbaddres0_.ADRLAND as ADRLAND0_, dldbaddres0_.ADRFAX as ADRFAX0_, dldbaddres0_.ADRPSA as ADRPSA0_, dldbaddres0_.ADRTEL as ADRTEL0_, dldbaddres0_.ADRPLZ as ADRPLZ0_, dldbaddres0_.ADRSEKTEL as ADRSEKTEL0_, dldbaddres0_.BISDAT as BISDAT0_, dldbaddres0_.VONDAT as VONDAT0_ from NETHZ.V_DLDB_ADRESSE dldbaddres0_ where dldbaddres0_.PERSID=26801 

And here is the object:

 @Entity @Table(name = "V_DLDB_ADRESSE", schema="NETHZ") public class DLDBAddress { @Id @Column(name = "PERSID", insertable = false, updatable = false) private Integer persid; @Temporal(TemporalType.DATE) @Column(name = "VONDAT", insertable = false, updatable = false) private Date valPeriodStart; @Temporal(TemporalType.DATE) @Column(name = "BISDAT", insertable = false, updatable = false) private Date valPeriodEnd; @Column(name = "ADRNUM", insertable = false, updatable = false) private Integer addressNr; @Column(name = "ADRTYP", insertable = false, updatable = false) private Integer addressType; @Column(name = "ADRZEIL1", insertable = false, updatable = false) private String addressRow1; @Column(name = "ADRZEIL2", insertable = false, updatable = false) private String addressRow2; @Column(name = "ADRZEIL3", insertable = false, updatable = false) private String addressRow3; @Column(name = "ADRLAND", insertable = false, updatable = false) private String country; @Column(name = "ADRPLZ", insertable = false, updatable = false) private String postalCode; @Column(name = "ADRORT", insertable = false, updatable = false) private String city; @Column(name = "ADRTEL", insertable = false, updatable = false) private String phoneNr; @Column(name = "ADRFAX", insertable = false, updatable = false) private String faxNr; @Column(name = "ADRSEKTEL", insertable = false, updatable = false) private String secretaryPhoneNr; @Column(name = "ADRAUSWTEL", insertable = false, updatable = false) private String alternatPhoneNr; @Column(name = "ADRPSA", insertable = false, updatable = false) private String pagerNr; ... 

Did I miss something?

Ah, I'm connecting to Oracle DB.

Thanks in advance Francesco

+6
java hibernate jpa criteria
source share
1 answer
 where dldbaddres0_.PERSID=26801 @Id @Column(name = "PERSID", insertable = false, updatable = false) 

You defined PERSID as @Id, which is the primary key. Is it really unique to your application? From behavior, this is not so. But for Hib, it MUST be.

So what happens:

  • You have two records in the database with PERSID = 26801
  • You request them WHERE PERSID = 26801
  • SQL Query returns two + rows
  • Hib downloads the first and puts it into the session with PERSID as the key (since it is marked as @Id). The object is placed in the list of results.
  • Hib loads the second one, notices that an object with the same @Id is already in the session and simply puts the link in the list of results. String data is ignored.
  • Thus, you get two + copies.
+16
source share

All Articles