gpt4 book ai didi

java - ValidationException 异常说明 : uses a non-entity as target entity in the relationship attribute

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:41 24 4
gpt4 key购买 nike

我收到以下异常并且之前已经完成所有这些操作 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;



<pre><code>@Column(name="numberType")
@Enumerated(EnumType.ORDINAL)
private PhoneNumberType numberType;

@Column(name="phoneNumber")
private String phoneNumber;
}
</code>
</code></pre>

<p>Adding persistance.xml</p>

<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>

最佳答案

我在将 @ManyToMany 关系分成两个 @OneToMany/@ManyToOne 关系时遇到了同样的情况,我解决了它。这里有一些提示,是从以上所有答案和我的发现中得出的结论:

  1. 确保类带有注解@Entity,并且有正确的Table(name=xxx)
  2. 如果使用 JPA,请确保它位于 persistence.xml 中。如果使用 Hibernate,请确保它位于 hibernate.cfg.xml 中。如果两者都使用,则在两个文件中。
  3. 确保类与映射/cfg 文件位于同一 JAR 中。
  4. 我发现一件事:您不必删除 mappedBy=,但您必须确保该属性名称在您的整个项目中是唯一的!

就像我的情况一样:如果我在 @OneToMany 端删除 mappedBy(而 @ManyToOne 端是 的中间类code>@ManyToMany 关系,它有自己的字段,所以我必须让它成为 @Entity 而不是直接使用中间表),EclipseLink 提示一个名为“EntityA_MiddleEntity”的表,它不存在。我猜,当遇到 mappedBy=xxx 时,EclipseLink 寻找所有字段名为 xxx 的实体,当它存在于多个类中时,这些类之一是@ManyToMany 的中间类,它无法关联它们。所以我更改了中间类的属性名称,一切正常。

关于java - ValidationException 异常说明 : uses a non-entity as target entity in the relationship attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971584/

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