简体   繁体   中英

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 !

using what Hibernate Documentation says, you have to do

@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk")
    public Troop getTroop() {
    ...
}           

This makes a Bidirectional Relationship

Yes Using Hibernate you can create model/entity objects like below

 @Entity
    @Table(name="StudyCase")
    public class StudyCase {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;
    @Column(name="study_case_number")
    private Long study_case_number; //You might try to use Integer of String as per your requirement
        ... getter setter
    }

    @Entity
    @Table(name="StudyCase")
    public class Measurement {
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Long id;

        @ManyToOne
        @JoinColumn(name="StudyCase")
        private StudyCase studyCaseId

       ... getter setter
    }     

Now using Hibernate Query You can write Query like below

String sql = "from Measurement as measurement "
                + "inner join measurement.studyCaseId as studyCaseId "
                + "where studyCaseId.study_case_number= :studyCaseNumber";
        Query query = getCurrentSession().createQuery(sql);
        query.setParameterList("studyCaseNumber", studyCaseNumberLong);
        List<Measurement> measurementList = query.list();

Hope Above example will help you All the best!!!

At the end I end up using spring-jpa but in a more compact way I would say. here is the extra code :

private DriverManagerDataSource dataSource;
private JdbcTemplate jdbcTemplate;

I cerated a method to manually initialize the datasource :

public void initDataSource()
    {
    String url  = "jdbc:sqlserver://localhost:1433;DatabaseName=testdb" ;
    Properties properties = new Properties()  ; 
    properties.setProperty("username", "actualUsername") ; 
    properties.setProperty("password", "actualpassowrd") ; 
    properties.setProperty("driverClassName", "com.microsoft.sqlserver.jdbc.SQLServerDriver") ;  
    properties.setProperty("dialect", "org.hibernate.dialect.SQLServerDialect") ; 
    dataSource = new DriverManagerDataSource(url, properties) ; 
}

and I used it like this :

    initDataSource() ; 
    jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "select  *  from Measurement  INNER JOIN StudyCase ON Measurement.study_case_number=StudyCase.study_case_number where "  ;
    List<DaoObject> result =  this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<DaoObject>(DaoObject.class)) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM