lundi 20 avril 2015

Hibernate mapping One class to two tables (the two tables have many to one relationship)

I am using Hibernate 4.3.6
I have one entity named StudyCase mapped with a table in database called StudyCase , I also got an other entity name Measurement mapped with a table in database callsed Measurement. Every StudyCase object can be assosiated with many Measurement objects and the question is this. I would like to map both of the tables in one new entity called mergedEntity and query it using clumns from both of the tables the result would be list of objects having one to one relation ship with Measurement objects of the same constrains but will have also assigned informations for the StudyCase object they belong to.

I have made something simmilar work in spring jpa :

public List<MergeObject> searchMeasurement(String actualConditions)
{   
    jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "select  *  from Measurement  INNER JOIN StudyCase ON Measurement.study_case_number=StudyCase.study_case_number where "+actualConditions  ;
    List<MergeObject> result =  this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<MergeObject>(MergeObject.class)) ;

    return result ; 
}

Is there any way that I can achieve the same thing using hibernate. thanks !

Aucun commentaire:

Enregistrer un commentaire