Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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 } 

How can I achieve this?

like image 525
vikiiii Avatar asked Mar 15 '13 07:03

vikiiii


People also ask

What is the annotation for foreign key in hibernate?

Entity class SubMenu will be used by Hibernate to create T_Submenu table in database. @JoinColumn annotation in line 27 indicates that this entity is the owner of the relationship (which will contain Foreign Key in Database perspective). This annotation is always used with @ManyToOne side of association.

How does JPA define foreign key?

Implementing With a Foreign Key in JPA. Note that we place the @OneToOne annotation on the related entity field, Address. Also, we need to place the @JoinColumn annotation to configure the name of the column in the users table that maps to the primary key in the address table.

How do you make a foreign key a primary key in hibernate?

You can use JPA's @MapsId annotation to tell Hibernate that it shall use the foreign key of an associated entity as the primary key. Let's take a look at a simple example. Each Book has a Manuscript, and each Manuscript belongs to 1 Book. The foreign key of the Book is also the primary key of the Manuscript.

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


2 Answers

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

like image 108
JB Nizet Avatar answered Sep 17 '22 18:09

JB Nizet


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:

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

Enter image description here

@ManyToOne @JoinColumn(name = "bok_aut_id", referencedColumnName = "aut_id") private Author bok_aut_id; 
like image 24
Vaibhav Jain Avatar answered Sep 19 '22 18:09

Vaibhav Jain