我正在使用 Netbeans IDE 8.0.2 和 eclipselink 2.5.2。打开连接时出现以下异常,问题是并非每次都发生这种情况。异常“Departmento”中描述的实体完全遵循其他类的模式,即我们的系统已经包含大约 500 个实体类,并且仅在新类中发生此异常。该实体是由 Netbeans 的“数据库中的实体类”选项生成的,并添加到持久化 XML ...
引起:javax.persistence.PersistenceException:异常 [EclipseLink-28018] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.EntityManagerSetupException 异常描述:PersistenceUnit [totemPU] 的预部署失败。内部异常:异常 [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.ValidationException 异常描述:[class entidade.Item] 使用非实体 [class entidade. Departamento] 作为关系属性 [field departamento] 中的目标实体。
实体部门
@Entity
@Table(name = "departamento")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Departamento.findAll", query = "SELECT d FROM Departamento d"),
@NamedQuery(name = "Departamento.findById", query = "SELECT d FROM Departamento d WHERE d.departamentoPK.id = :id"),
@NamedQuery(name = "Departamento.findByIdEmpresa", query = "SELECT d FROM Departamento d WHERE d.departamentoPK.idEmpresa = :idEmpresa"),
@NamedQuery(name = "Departamento.findByDescricao", query = "SELECT d FROM Departamento d WHERE d.descricao = :descricao"),
@NamedQuery(name = "Departamento.findByLixo", query = "SELECT d FROM Departamento d WHERE d.lixo = :lixo"),
@NamedQuery(name = "Departamento.findByIp", query = "SELECT d FROM Departamento d WHERE d.ip = :ip")})
public class Departamento implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected DepartamentoPK departamentoPK;
@Basic(optional = false)
@Column(name = "descricao")
private String descricao;
...
Getters() and Setters()
实体项目
@Entity
@Table(name = "item")
public class Item implements Serializable {
@EmbeddedId
protected ItemPK itemPK;
@JoinColumns({
@JoinColumn(name = "departamento_id", referencedColumnName = "id"),
@JoinColumn(name = "departamento_id_empresa", referencedColumnName = "id_empresa")})
@ManyToOne(optional = true)
private Departamento departamento;
...
Getters() and Setters()
持久性.xml
<persistence-unit name="totemPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entidade.Departamento</class>
<class>entidade.item</class>
... More classes ...
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://xxx"/>
<property name="javax.persistence.jdbc.password" value="xxx"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="xxx"/>
<property name="javax.persistence.jdbc.autoReconnect" value="true"/>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.level" value="OFF"/>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.query-results-cache" value="false"/>
</properties>
在同一个 persistence.xml 中还有更多的三个持久性单元。