美好的一天。我尝试使用持久性api具有多对多的依赖关系。 我填写一个新的用户数据并尝试将其添加到数据库,但得到一个例外:
Exception Description: [class main.java.entities.User] uses a non-entity [class main.java.entities.UserAccounts] as target entity in the relationship attribute [field accounts].
我尝试在name
部分使用@ManyToMany注释的差异值,例如bank_user_accounts
和userAccounts
。第二个值是persistence.xml
中的单位名称。但这种麻烦仍然存在。
用户类:
@Entity(name="bank_users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany
@JoinTable(
name = "bank_user_accounts",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "account_id")
)
private List<UserAccounts> accounts;
}
UserAccounts类:
@Entity(name="bank_user_accounts")
public class UserAccounts {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "user_id")
private int userId;
@Column(name = "account_id")
private int accountId;
}
的persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="users" transaction-type="RESOURCE_LOCAL">
<class>main.java.entities.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="deleted" />
<property name="javax.persistence.jdbc.user" value="deleted" />
<property name="javax.persistence.jdbc.password" value="deleted" />
</properties>
</persistence-unit>
<persistence-unit name="userAccounts" transaction-type="RESOURCE_LOCAL">
<class>main.java.entities.UserAccounts</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="deleted" />
<property name="javax.persistence.jdbc.user" value="deleted" />
<property name="javax.persistence.jdbc.password" value="deleted" />
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:0)
User
和UserAccounts
之间的关系,而不是@OneToMany
,或者只是一个可嵌入的类。
@ManyToMany
应该在双方都有注释(双向),并且应该在一侧设置mappedBy
属性,以指向引用此实体作为集合的另一方的属性名称。
答案 1 :(得分:0)
我在您的代码中观察到的一件事是您在User类中使用了许多非常规的注释,但您还没有在userAccounts类中使用过。要实现ManyToMany关系,两个实体都应使用ManyToMany进行注释。
答案 2 :(得分:0)
问题是(尽管可能已过时)您有一个持久性单元到另一个持久性单元的引用。您可以轻松地将两者放在同一个单位:
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="users" transaction-type="RESOURCE_LOCAL">
<class>main.java.entities.User</class>
<class>main.java.entities.UserAccounts</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="deleted" />
<property name="javax.persistence.jdbc.user" value="deleted" />
<property name="javax.persistence.jdbc.password" value="deleted" />
</properties>
</persistence-unit>
</persistence>