如何创建具有JPA注释的连接表? [英] How to create join table with JPA annotations?
问题描述
我需要在我的数据库中使用JPA注释创建一个连接表,因此结果将是:
到目前为止,我只是实现了2个实体:
@Entity
@Table(name =USERS,schema =ADMIN)
public class用户实现Serializable {
private static final long serialVersionUID = -1244856316278032177L;
@Id
@Column(nullable = false)
private String userid;
@Column(nullable = false)
private String password;
public String getUserid(){
return userid;
}
public void setUserid(String userid){
this.userid = userid;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
$ b b
@Entity
@Table(name =GROUPS,schema =ADMIN)
public class Group implements Serializable {
private static final long serialVersionUID = -7274308564659753174L;
@Id
@Column(nullable = false)
private String groupid;
public String getGroupid(){
return groupid;
}
public void setGroupid(String groupid){
this.groupid = groupid;
}
}
如果我创建另一个实体叫做USER_GROUP,添加一些注释,所以当我从实体(ORM)运行创建表时,将自动创建连接表?
我应该如何注释我的实体以实现与图像?
你绝对不应该创建User_Group实体,因为它比面向对象的基础数据库表示更多。 / p>
您可以通过定义以下内容来实现连接表:
@实体
@Table(name =USERS,schema =ADMIN)
public class用户实现Serializable {
// ...
@ManyToOne
@JoinTable(name =USER_GROUP)
组组;
@实体
@Table(name =GROUPS,schema =ADMIN)
public class Group implements Serializable {
// ...
@OneToMany mappedBy =group)
设置< User>用户;
编辑:如果要显式设置列的名称可以使用@JoinColumn元素,如下所示:
@ManyToOne
@JoinTable(name =USER_GROUP,
joinColumns = @JoinColumn(name =userid,
referencedColumnName =userid),
inverseJoinColumns = @JoinColumn(name =groupid,
referencedColumnName =groupid))
组群;
I need to create a join table in my database ussing JPA annotations so the result will be this:
So far i just implemented 2 entities:
@Entity
@Table(name="USERS", schema="ADMIN")
public class User implements Serializable {
private static final long serialVersionUID = -1244856316278032177L;
@Id
@Column(nullable = false)
private String userid;
@Column(nullable = false)
private String password;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Entity
@Table(name="GROUPS", schema="ADMIN")
public class Group implements Serializable {
private static final long serialVersionUID = -7274308564659753174L;
@Id
@Column(nullable = false)
private String groupid;
public String getGroupid() {
return groupid;
}
public void setGroupid(String groupid) {
this.groupid = groupid;
}
}
Should i create another entity called USER_GROUP or i can just add some annotations, so the join table will be created automatically when i run create tables from entities(ORM)?
How should i annotate my entities to achieve the same as in the image?
You definitely shouldn't create User_Group entity as it's more the underlying database representation than the object oriented one.
You can achieve the join table by defining something like:
@Entity
@Table(name="USERS", schema="ADMIN")
public class User implements Serializable {
//...
@ManyToOne
@JoinTable(name="USER_GROUP")
Group group;
@Entity
@Table(name="GROUPS", schema="ADMIN")
public class Group implements Serializable {
//...
@OneToMany(mappedBy="group")
Set<User> users;
Edit: If you want to explicitly set the names of the columns you could use @JoinColumn elements as shown below:
@ManyToOne
@JoinTable(name="USER_GROUP",
joinColumns = @JoinColumn(name = "userid",
referencedColumnName = "userid"),
inverseJoinColumns = @JoinColumn(name = "groupid",
referencedColumnName = "groupid"))
Group group;
这篇关于如何创建具有JPA注释的连接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!