gpt4 book ai didi

Java持久化API : class uses non-entity class as target entity in the relationship attribute

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:15 25 4
gpt4 key购买 nike

美好的一天。我尝试与持久性 API 建立多对多依赖关系。我填充新的用户数据并尝试将其添加到数据库中,但出现异常:

异常描述:[class main.java.entities.User] 使用非实体 [class main.java.entities.UserAccounts] 作为关系属性 [字段帐户] 中的目标实体。

我尝试在 name 部分中使用 @ManyToMany 注释的差异值,例如 bank_user_accountsuserAccounts。第二个值是 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;
}

持久性.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>

最佳答案

我在您的代码中观察到的一件事是您在 User 类中使用了 Manytomany 注释,但没有在 userAccounts 类中使用。要实现ManyToMany关系,两个实体都应该用ManyToMany注释。

关于Java持久化API : class uses non-entity class as target entity in the relationship attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com