Good day. I try to have many-to-many dependencies with persistence api. I fill an new user data and try to add it to the database, but get an exception:
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].
I try to use difference values for @ManyToMany annotation in name
section such as bank_user_accounts
and userAccounts
. Second value is a name of unit in persistence.xml
. But this trouble still exists.
User class:
@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 class:
@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>
The relationship between the User
and UserAccounts
sound rather @OneToMany
or simply an embeddable class.
@ManyToMany
should have the annotation on both sides (bidirectional) and should have the mappedBy
attribute set on one side, to point to the other side's property name referencing this entity as a collection.
One thing I have observed in your code is you have used manytomany annotation in User class but you have not used in userAccounts class. To achieve ManyToMany relation, both the entities should be annotated with ManyToMany.
The problem is (although maybe outdated) that you have a reference from one persistence unit to another. You can easily put both to the same unit:
<?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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.