简体   繁体   English

Hibernate ForeignKey映射注释

[英]Hibernate ForeignKey mapping annotations

I want to have hibernate generate some tables with foreign keys and so on. 我想让休眠生成一些带有外键的表,等等。 Ill give you an example of the query i want hibernate to generate: 病给你一个我想休眠生成查询的例子:

create table RealtimeCost(id INTEGER not null primary key Autoincrement,
    mnemonic varchar(50)not null references Exchange(mnemonic),
    sid int not null references License(sid),
    price numeric(10,2) not null)

so this query should be generated by hibernate via Annotations. 因此该查询应由休眠通过注释生成。 The corresponding class to this is: 与此对应的类是:

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @MapsId("mnemonic")
    @JoinColumn(referencedColumnName="sid")
    private String mnemonic;
    @MapsId("sid")
    @JoinColumn(referencedColumnName="sid")
    private Integer sid;
    @Column
    private Double price;

Example for what the mnemonic in RealtimeCost should be mapped to (each mnemonic in RealtimeCost has exactly 1 value in Exchange ): 应将RealtimeCostmnemonic映射到的示例( RealtimeCost每个助记符在Exchange恰好具有1值):

@Entity
@Table
public class Exchange {
    @Id
    @Column(name="mnemonic")
    private String exchange;
    @Column
    private String description;

As you can see I've tried a bit with the help of the docs, but I was not able to have the foreign keys be generated by hibernate. 如您所见,我已经在文档的帮助下进行了一些尝试,但是我无法让休眠生成外键。 It would be really kind, if anyone could tell me the needed annotations and values for this class, so i can do it myself for the other classes as well. 如果有人可以告诉我该类所需的注释和值,那将是非常好,因此我也可以为其他类自己做。 Also please tell me if i need to change anything in the Exchange class for the mapping to work. 还请告诉我是否需要更改Exchange类中的任何内容才能使映射起作用。 Thanks in advance 提前致谢

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "accommodation_type", unique = true, nullable = false)
private AccommodationType accommodationType;

@ManyToOne() creates a relationship according to @JoinColumn() name in @JoinColumn() is the table name that you want to make a connection. @ManyToOne()根据@JoinColumn() name创建关系, @JoinColumn() @JoinColumn()是您要建立连接的表名称。

Then when you create a class that is going to be connected to main class, you first need to give it a table name below @Entity eg @Table(name="accommodation_types") 然后,当您创建要连接到主类的类时,首先需要在@Entity为其指定一个表名,例如@Table(name="accommodation_types")

Then you create your variable. 然后创建变量。

//bi-directional many-to-one association to Accommodation
@OneToMany(mappedBy="accommodationType", fetch=FetchType.EAGER)
private List<Accommodation> accommodations;

value of mappedBy is the variable name in main class. mappedBy值是主类中的变量名称。

I'm not an expert but we let hibernate do all the work with the javax.persistence annotations for joining entities. 我不是专家,但是我们让hibernate使用javax.persistence注释完成所有工作,以加入实体。

  @javax.persistence.ManyToOne( fetch = javax.persistence.FetchType.EAGER, optional = true )
  @javax.persistence.JoinColumn( name = "VIEWTYPE_ID", nullable = true, unique = false, insertable = true, updatable = true )
  private com.company.other.subproject.ViewType viewType;

Maybe this is what you need. 也许这就是您所需要的。 Since this let's hibernate care about the tables that have to be created or not and the foreignKeys get created automatically with the dialect of the database you communicate with. 既然这样,我们就可以在休眠状态下关心必须创建或不创建的表以及如何使用与您通信的数据库的方言自动创建foreignKeys。

You should set up the association in one entity and use the mappedBy in the other. 您应该在一个实体中建立关联,而在另一个实体中使用mappedBy You don't need @MapsId because you are not using embedded entities (read the docs). 您不需要@MapsId因为您没有使用嵌入式实体(请阅读文档)。 Take a look at the @OneToMany and @ManyToOne relationships: 看一下@OneToMany@ManyToOne关系:

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany
    @JoinColumn(name="mnemonic")
    private Exchange exchange;
    ...
}

@Entity
@Table
public class Exchange {
    @Id
    @Column(name="mnemonic")
    private String mnemonic;

    @Column
    private String description;

    @ManyToOne(mappedBy="exchange")
    private RealtimeCost realtimeCost;
    ...
}

Every answer posted here got an upvote from me, because everyone was kinda right, but it was not 100% what i was searching for, yet it helped me solving my problem by myself. 此处发布的每个答案都受到我的支持,因为每个人都还算不错,但这并不是我要搜索的内容的100%,但这可以帮助我自己解决问题。 For the example i posted, the solution i was seeking is as follows (i also added not nullable): 对于我发布的示例,我正在寻求的解决方案如下(我还添加了不可为空的内容):

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "mnemonic",nullable=false)
    private Exchange exchange;
    @ManyToOne
    @JoinColumn(name = "sid",nullable=false)
    private License license;
    @Column(nullable=false)
    private Double price;

these are the annotations i was seeking for RealtimeCost class. 这些是我正在为RealtimeCost类寻求的注释。 I did not need any special annotations in Exchange class. 我在Exchange类中不需要任何特殊的注释。 @Nico answer was closest to what i need, therefore his answer will be accepted @Nico的答案最接近我的需求,因此他的答案将被接受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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