首页 文章

ValidationException异常描述:在关系属性中使用非实体作为目标实体

提问于
浏览
3

我收到以下异常并且之前完成了所有这1000次,但现在我收到了错误:

ValidationException异常描述:[class com.smartphonedev.rsvplaw.entities.Staff]使用非实体[class com.smartphonedev.rsvplaw.entities.Address]作为关系属性[field addresses]中的目标实体 .

这是有问题的实体 . 有人能帮我辨别我的人际关系有什么问题吗?

<code>
@Entity
@Table(name="address")
public class Address implements Serializable,
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="addressType")
@Enumerated(EnumType.ORDINAL)
private AddressType addressType;

@Column(name="streetLineOne")
private String streetLineOne;

@Column(name="streetLineTwo")
private String streetLineTwo;

@Column(name="city")
private String city;

@Column(name="adState")
private String state;

@Column(name="postCode")
private String postCode;    

@OneToMany(fetch=FetchType.LAZY, targetEntity=PhoneNumber.class, cascade = {CascadeType.MERGE,   CascadeType.REFRESH, CascadeType.DETACH},orphanRemoval=true)
@JoinColumn(name="phoneNumbers")
private Collection<PhoneNumber> phoneNumbers;
…..
}

@Entity
@Table(name="staff")
public class Staff implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

//private Profile profile;
@OneToOne(fetch=FetchType.LAZY,cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name="permission")
private Permission permission;

@OneToOne(fetch=FetchType.LAZY,cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name="login")    
private Login login;

@Column(name="firstName")
private String firstName;

@Column(name="surnameName")
private String surnameName;

@Column(name="emailAddress")
private String emailAddress;

@OneToMany(fetch=FetchType.LAZY, targetEntity=PhoneNumber.class, cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH},orphanRemoval=true)
@JoinColumn(name="phoneNumbers")
private Collection<PhoneNumber> phoneNumbers;

@OneToMany(fetch=FetchType.LAZY, targetEntity=Address.class, cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH},orphanRemoval=true)
@JoinColumn(name="addresses")
private Collection<Address> addresses;
}
</code

添加phoneNumber类 @Entity @Table(name="phone") public class PhoneNumber implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;
`@Column(name="numberType")
@Enumerated(EnumType.ORDINAL)
private PhoneNumberType numberType;

@Column(name="phoneNumber")
private String phoneNumber;
}
</code>
Adding persistance.xml`

<code>

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="RSVPLawServerPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/RSVPLaw</jta-data-source>
<class>com.smartphonedev.rsvplaw.entities.PhoneNumber</class>
<class>com.smartphonedev.rsvplaw.entities.Address</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
  <property name="eclipselink.target-server" value="SunAS9"/>  
  <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
  <property name="eclipselink.ddl-generation" value="create-tables"/>
  <property name="eclipselink.ddl-generation.output-mode" value="database"/>
  <property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>

<code>

1 回答

  • 2

    在将 @ManyToMany 关系分成两个 @OneToMany/@ManyToOne 关系时,我处于同样的情况,我解决了它 . 以下是从上述所有答案中得出的一些提示,以及我的发现:

    • 确保该类带有注释 @Entity ,并且具有正确的 Table(name=xxx) .

    • 如果使用JPA,请确保它在 persistence.xml 中 . 如果使用Hibernate,请确保它在 hibernate.cfg.xml 中 . 如果在两个文件中同时使用两者 .

    • 确保类与mapping / cfg文件位于同一JAR中 .

    • 我发现了一件事: You don't have to remove mappedBy=, but you have to ensure, that the attribute name is unique in your whole project!

    就像在我的情况下:如果我在 @OneToMany 方面删除 mappedBy (而 @ManyToOne 方是 @ManyToMany 关系的中间类,它有自己的字段,所以我必须使它 @Entity 而不是直接使用中间表),EclipseLink抱怨名为"EntityA_MiddleEntity"的表,该表不存在 . 我想,当遇到 mappedBy=xxx 时,EclipseLink会查找名为 xxx 的字段的所有实体,当它存在于多个类中,并且其中一个类是 @ManyToMany 的中间类时,它无法将它们关联起来 . 所以我改变了中产阶级的属性名称,一切正常 .

相关问题