vzateychuk
vzateychuk

Reputation: 301

Hibernate Entity with @ElementCollection and @CollectionTable. Hibernate add attribute name to SQL statement when retrieve the collection

In my case I have two Tables: parent 'VDI_DICT_EMPOYEES_SV' and chield 'VDI_DICT_EMPOYEES_RV' joined by column with the same name 'R_OBJECT_ID'. (See ER Diagram)

enter image description here

The structure of the 'parent' Entity is:

@Entity
@Table(name="VDI_DICT_EMPLOYEE_SV")
public class DmDictEmployee {
  @Column (name="R_OBJECT_ID") private String objectId;

... other columns are skipped...

  @ElementCollection
  @CollectionTable(name="VDI_EMPLOYEE_RV, joinColumns=@JoinColumn(name="CROC_SECRETARY_ID",referencedColumnName="R_OBJECT_ID"))
 private List<String> crocSecretaryIds; // the element collection

... getters and setters are skipped too...

}

To fetch the master and slave data I use the native @Query

"SELECT parent.*, chield.CROC_SECRETARY_ID from VDI_DICT_EMPLOYEE_SV parent left outer join VDI_DICT_EMPLOYEE_RV chield on parent.R_OBJECT_ID=chield.R_OBJECT_ID"

(This selects employee entry with list of secretary in one select)

And it works fine as I see from the log. The query returns data just fine. But when I try to retrieve the list of crocSecretaryIds I see the HIBERNATE prepares the SQL which includes the collection name: "SELECT CROC_SECRETARY_ID, crocSecretaryIds FROM VDI_DICT_EMPLOYEES_RV WHERE R_OBJECT_ID=..." (see next screenshot)

enter image description here

which is quite odd. I expected to see the SQL query without crocSecretaryIds so, I assume something wrong with my declaration of the @ElementCollection column. Could you please to advise any ideas what is missed and why the HIBERNATE prepares query with crocSecretaryIds?

Upvotes: 2

Views: 2109

Answers (1)

vzateychuk
vzateychuk

Reputation: 301

Finally I realized that I incorrectly referenced the "child" table. Instead of

@ElementCollection
@CollectionTable(name="VDI_EMPLOYEE_RV, joinColumns=@JoinColumn(name="CROC_SECRETARY_ID",referencedColumnName="R_OBJECT_ID"))
 private List<String> crocSecretaryIds; // the element collection

I need to do:

@ElementCollection
@CollectionTable(name="VDI_EMPLOYEE_RV, joinColumns=@JoinColumn(name="R_OBJECT_ID",referencedColumnName="R_OBJECT_ID"))
@Column(name="CROC_SECRETARY_ID") // the column name in child table
private List<String> crocSecretaryIds; 

So, it works now.

Upvotes: 2

Related Questions