I have 2 classes in a One-To-Many relationship and a JPQL query that is now working as I expected. Even after read some post about it, it does not seem clear to me.
@Entity
@Table(name = "context_entity")
public class ContextEntity {
@Id
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = "entity_id")
private List<TypeSpecCharacteristicValue> metadata = null;
}
@Entity
@Table(name = "type_spec_characteristic_value")
public class TypeSpecCharacteristicValue {
@Id
private Long id;
private String value;
}
BTW I cut only the important part of my code.
If I run the following query I get duplicates TypeSpecCharacteristicValue objects:
session.createQuery("select e.metadata from ContextEntity e left join e.metadata where e.id=:contextId")
Then I check the SQL statement generated by hibernate, which resulted in the following:
select metadata2_.id as id1_5_, metadata2_.value as value3_5_ from context_entity contextent0_ left outer join type_spec_characteristic_value metadata1_ on contextent0_.id=metadata1_.entity_id inner join type_spec_characteristic_value metadata2_ on contextent0_.id=metadata2_.entity_id where contextent0_.id=[some_context_id];
Why is hibernate generating a second join with the table type_spec characteristic_value
? Should I use a distinct?
Thank you in advance.