简体   繁体   English

如何使用Hibernate注释标记外键约束?

[英]How can I mark a foreign key constraint using Hibernate annotations?

I am trying to use Hibernate annotation for writing a model class for my database tables. 我正在尝试使用Hibernate注释为我的数据库表编写模型类。

I have two tables, each having a primary key User and Question. 我有两个表,每个表都有一个主键User和Question。

@Entity
@Table(name="USER")
public class User
{
    @Id
    @Column(name="user_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name="username")
    private String username;

    // Getter and setter
}

Question Table. 问题表。

@Entity
@Table(name="QUESTION")
public class Questions extends BaseEntity{

    @Id
    @Column(name="question_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="question_text")
    private String question_text;

    // Getter and setter
}

And I have one more table, UserAnswer, which has userId and questionId as foreign keys from the above two tables. 我还有一个表UserAnswer,它有userId和questionId作为上述两个表中的外键。

But I am unable to find how I can reference these constraints in the UserAnswer table. 但我无法找到如何在UserAnswer表中引用这些约束。

@Entity
@Table(name="UserAnswer ")
public class UserAnswer
{
    @Column(name="user_id")
    private User user;

    //@ManyToMany
    @Column(name="question_id")
    private Questions questions ;

    @Column(name="response")
    private String response;

    // Getter and setter
}

How can I achieve this? 我怎样才能做到这一点?

@Column is not the appropriate annotation. @Column不是合适的注释。 You don't want to store a whole User or Question in a column. 您不希望在列中存储整个用户或问题。 You want to create an association between the entities. 您想要在实体之间创建关联。 Start by renaming Questions to Question , since an instance represents a single question, and not several ones. 首先将Questions重命名为Question ,因为实例代表单个问题,而不是几个问题。 Then create the association: 然后创建关联:

@Entity
@Table(name = "UserAnswer")
public class UserAnswer {

    // this entity needs an ID:
    @Id
    @Column(name="useranswer_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "question_id")
    private Question question;

    @Column(name = "response")
    private String response;

    //getter and setter 
}

The Hibernate documentation explains that. Hibernate文档解释了这一点。 Read it. 阅读。 And also read the javadoc of the annotations. 并且还阅读了注释的javadoc。

There are many answers and all are correct as well. 答案很多,而且都是正确的。 But unfortunately none of them have a clear explanation. 但不幸的是,他们都没有明确的解释。

The following works for a non-primary key mapping as well. 以下适用于非主键映射。

Let's say we have parent table A with column 1 and another table, B, with column 2 which references column 1: 假设我们有父表A和列1,另一个表B,第2列引用第1列:

@ManyToOne
@JoinColumn(name = "TableBColumn", referencedColumnName = "TableAColumn")
private TableA session_UserName;

在此输入图像描述

@ManyToOne
@JoinColumn(name = "bok_aut_id", referencedColumnName = "aut_id")
private Author bok_aut_id;

@JoinColumn(name="reference_column_name")注释可以在从某个其他实体引用的类的属性或字段上方使用。

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

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