如何正确地在 Hibernate 中创建相同实体之间的关系?
How to correct way create relation beetwen the same entities in the Hibernate?
我有以下包含朋友列表的用户实体,即相同实体的列表。
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
List<User> friends;
}
在 Hibernate 中映射这种关系的正确方法是什么?
在本机 SQL table 中,它通过创建定义用户之间关系的第二个 table 来解决。
您必须添加适当的 @OneToMany
和 @ManyToOne
注释。您必须引入一个新字段作为父字段。
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
@ManyToOne
private User parentUser;
@OneToMany(mappedBy = "parentUser")
List<User> friends;
}
这里可以使用下面的例子:
@Entity
@Table(name = "users")
class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_friends")
Set<User> friends;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Set<User> getFriends() {
return friends;
}
public void setFriends(Set<User> friends) {
this.friends = friends;
}
public void addFriend(User u) {
if (this.friends == null) {
this.friends = new HashSet();
}
this.friends.add(u);
}
}
persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="deneme" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.deneme.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://127.0.0.1;databaseName=springbootdb"/>
<property name="javax.persistence.jdbc.user" value="emre"/>
<property name="javax.persistence.jdbc.password" value="asdf_1234"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.ddl-generation" value="auto"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
主要内容:
public class Main {
public static void main(String[] args) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "deneme");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction().begin();
User u = new User();
u.setName("test");
u.setSurname("test");
User uf = new User();
uf.setName("fri");
uf.setSurname("fri");
u.addFriend(uf);
entitymanager.persist(u);
entitymanager.getTransaction().commit();
entitymanager.close();
emfactory.close();
}
}
生成sql:
Hibernate: insert into users (name, surname) values (?, ?)
Hibernate: insert into users (name, surname) values (?, ?)
Hibernate: insert into user_friends (User_id, friends_id) values (?, ?)
来自数据库的屏幕截图:
我有以下包含朋友列表的用户实体,即相同实体的列表。
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
List<User> friends;
}
在 Hibernate 中映射这种关系的正确方法是什么? 在本机 SQL table 中,它通过创建定义用户之间关系的第二个 table 来解决。
您必须添加适当的 @OneToMany
和 @ManyToOne
注释。您必须引入一个新字段作为父字段。
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
@ManyToOne
private User parentUser;
@OneToMany(mappedBy = "parentUser")
List<User> friends;
}
这里可以使用下面的例子:
@Entity
@Table(name = "users")
class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String surname;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_friends")
Set<User> friends;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Set<User> getFriends() {
return friends;
}
public void setFriends(Set<User> friends) {
this.friends = friends;
}
public void addFriend(User u) {
if (this.friends == null) {
this.friends = new HashSet();
}
this.friends.add(u);
}
}
persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="deneme" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.deneme.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://127.0.0.1;databaseName=springbootdb"/>
<property name="javax.persistence.jdbc.user" value="emre"/>
<property name="javax.persistence.jdbc.password" value="asdf_1234"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.ddl-generation" value="auto"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
主要内容:
public class Main {
public static void main(String[] args) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "deneme");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction().begin();
User u = new User();
u.setName("test");
u.setSurname("test");
User uf = new User();
uf.setName("fri");
uf.setSurname("fri");
u.addFriend(uf);
entitymanager.persist(u);
entitymanager.getTransaction().commit();
entitymanager.close();
emfactory.close();
}
}
生成sql:
Hibernate: insert into users (name, surname) values (?, ?)
Hibernate: insert into users (name, surname) values (?, ?)
Hibernate: insert into user_friends (User_id, friends_id) values (?, ?)
来自数据库的屏幕截图: