我如何使用hibernate注释创建外键约束? [英] How can I create a foreign key constraint using hibernate annotations?

查看:303
本文介绍了我如何使用hibernate注释创建外键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个表,每个表都有一个主键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
}

问题表。

  @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

}

我还有一个表UserAnswer,它具有userId和questionId作为上述两个表中的外键。

但我无法找到如何引用这些

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

@Column(name =user_id)
private用户用户;

// @ ManyToMany
@Column(name =question_id)
私人提问;

@Column(name =response)
private String response;

// getter and setter
}

请帮助我做到这一点?
在此先感谢。

解决方案

@Column 不是适当的注释。您不希望将整个用户或问题存储在列中。你想创建一个实体之间的关联。首先将问题更改为问题,因为一个实例代表单个问题,而不是几个问题。然后创建关联:

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

//这个实体需要一个ID:
@Id
@Column(name =useranswer_id)
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@ManyToOne
@JoinColumn(name =user_id)
私人用户用户;

@ManyToOne
@JoinColumn(name =question_id)
私人问题问题;

@Column(name =response)
private String response;

// getter and setter
}

Hibernate文档解释了这一点。阅读。并阅读注释的javadoc。


I am trying to use hibernate annotation for writing a model class for my database tables.

I have two tables each having a primary key User and 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.

But I am unable to find how I can reference these constraints in the UserAnswer table.

@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 
}

Please help me achieve this? Thanks in advance.

解决方案

@Column is not the appropriate annotation. 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. 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. Read it. And also read the javadoc of the annotations.

这篇关于我如何使用hibernate注释创建外键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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