避免Hibernate将一对多(或一对多)关联映射到数据库表中的关系表 [英] avoid relation table in Hibernate's mapping one-to-many(or one-to-many) association into db tables

查看:279
本文介绍了避免Hibernate将一对多(或一对多)关联映射到数据库表中的关系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手。我注意到在Hibernate中,将java类映射到数据库表通常涉及关系表,即使有时候关系表也不是必需的(就像在一对多关系中或相反)。



例如:

我是一个公司类和一个Flight类,其中一个公司可以有很多航班(从公司到航班的一对多关联)。

我有以下使用hibernate注释的代码:

  @Entity 
@Table(name =COMPANY)
public class Company {

@Id
private Long ID;

@OneToMany
私人设置< Flight>航班=新的HashSet< Flight>();

......
获取和设置方法
......
}



@Entity
@Table(name =FLIGHT)
public class Flight {
@Id
private Long ID;

@ManyToOne
@JoinColumn(name =COMP_ID)
private公司ownerCompany;

......
getter和setter方法
......

}

这些类已成功映射到数据库中。有三个表格,分别是:


  1. 公司(ID字段) ID字段和COMP_ID字段)

  2. COMPANY_MANY_TO_ONE_FLIGHT(两个字段:MANY_TO_ONE_COMPANY_id和flights_id)

然而,最后一个表COMPANY_MANY_TO_ONE_FLIGHT是一个由hibernate添加的关系表,它是多余的。



显然,在FLIGHT表中有一个外键COMP_ID,这是合理的删除冗余关系表。



我怎样才能避免这种情况?

解决方案

尝试在@OneToMany注释中使用mappedBy属性:

  @OneToMany(mappedBy =ownerCompany)
私人设置< Flight>航班=新的HashSet< Flight>();

您可以在这里查看常见关联mappend和hibernate注释:
http://docs.jboss.org/hibernate/ stable / annotations / reference / en / html / entity.html#entity-mapping-association

I am new to Hibernate. I notice that in Hibernate, mapping the java classes into database tables often involve relation tables, even sometimes relation tables are not necessary(Like in a one-to-many relation or the opposite).

For example:

I am a Company class and a Flight class, in which a company can have many flights(a one to many association from Company to Flight).

I have the following code using hibernate annotations:

@Entity
@Table(name = "COMPANY")
public class Company {

    @Id
    private Long id;

    @OneToMany
    private Set<Flight> flights = new HashSet<Flight>();

    ......
    getter and setter methods
    ......
}



@Entity
@Table(name="FLIGHT")
public class Flight{
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "COMP_ID")
    private Company ownerCompany;

    ......
    getter and setter methods
    ......

}

The classes are successfully mapped into database. And there are three tables, which are:

  1. COMPANY(an ID field)
  2. FLIGHT(an ID field and an COMP_ID field)
  3. COMPANY_MANY_TO_ONE_FLIGHT(two fields:MANY_TO_ONE_COMPANY_id and flights_id )

However, the last table COMPANY_MANY_TO_ONE_FLIGHT is a relation table added by hibernate, which is redundant.

Obviously, there is a foreign key COMP_ID in FLIGHT table and it is reasonable remove the redundant relation table.

And how can I avoid such circumstance? Like through modifying the annotations.

解决方案

try using the mappedBy property in the @OneToMany annotation:

@OneToMany(mappedBy="ownerCompany")
private Set<Flight> flights = new HashSet<Flight>();

you can look up common associations mappend with hibernate annotations here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association

这篇关于避免Hibernate将一对多(或一对多)关联映射到数据库表中的关系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆