Considering the following parent entity:
{code:java} @Entity public class Phone {
@Id private Long id;
@ManyToOne private Person person;
private String number;
@Enumerated(EnumType.STRING) private PhoneType type;
@OneToMany(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true) private List<Call> calls = new ArrayList<>( );
@OneToMany(mappedBy = "phone") @MapKey(name = "timestamp") @MapKeyTemporal(TemporalType.TIMESTAMP ) private Map<Date, Call> callHistory = new HashMap<>();
@ElementCollection private List<Date> repairTimestamps = new ArrayList<>( );
//Getters and setters are omitted for brevity } {code}
and its child-side relationship:
{code:java} @Entity public class Call {
@Id @GeneratedValue private Long id;
@ManyToOne private Phone phone;
private Date timestamp;
private int duration;
//Getters and setters are omitted for brevity } {code}
{code:java} When executing the following SQL query: List<Phone> phones = session.createSQLQuery( "SELECT * " + "FROM phone ph " + "JOIN call c ON c.phone_id = ph.id" ) .addEntity("phone", Phone.class ) .addJoin( "c", "phone.calls") .setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY ) .list(); {code}
Hibernate returns a {{List<Call>}} instead of a {{List<Person>}}. The test is located under the following path:
{noformat} hibernate-orm/documentation/src/test/java/org/hibernate/userguide/sql/SQLTest.java#test_sql_hibernate_entity_associations_query_one_to_many_join_example_1 {noformat} |
|