Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the intermediate table name in JPA OneToMany relationship

I have a table user and each user has a set of other users as friends. the way I defined it in my model class is as following:

     Public class User extends Models {
     ....
     @OneToMany
     public List<User> friends;
     .
     .
     }

Now the Play JPA is creating an intermediate table name user_user with following fields.

    mysql> desc user_user;
    ------------+------------+------+-----+---------+-------+
    | user_id     | bigint(20) | NO   | MUL | NULL    |       |
    | friends_id  | bigint(20) | NO   | PRI | NULL    | 

Question is how can i change the name of the intermediate table (user_user) and columns in it?

like image 500
nightograph Avatar asked Jul 24 '12 18:07

nightograph


People also ask

Can a JPA entity have multiple One-To-Many associations?

You can have as many of them as you want.

What best describes the one many relationship in JPA?

The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. Hence, in this type of association the instance of one entity can be mapped with any number of instances of another entity.

What is the default mechanism for representing One-To-Many unidirectional relationships in JPA?

Unidirectional @OneToMany with @JoinColumn.

How can we represent the many-to-many entity relationship in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.


1 Answers

Using the @JoinTable annotation should give you what you need. There is a good post ( JPA "@JoinTable" annotation ) specifically on how to use this annotation.

like image 149
Codemwnci Avatar answered Oct 05 '22 08:10

Codemwnci