自从我开始使用openJKD 12.0.2和Netbeans 11.1以来,在用netbeans创建实体后就遇到了这个异常。
如果我在项目属性中选择源/二进制格式,则jdk8可以正常工作!源/二进制格式为jdk9及更高版本的我会遇到异常!
package tesyapp;
import Model.Testtable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class TesyApp {
public static EntityManager em;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//String PERSISTENCE_UNIT_NAME = "TestAppPU";
//ClassLoader cL = Activator.class.getClassLoader();
//Map<String, Object> properties = new HashMap<>();
//properties.put(PersistenceUnitProperties.CLASSLOADER, cL);
//properties.put(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_UNITS, PERSISTENCE_UNIT_NAME);
//create the data base ...
EntityManagerFactory emf;
Testtable tt;
System.out.println("Creating the Factory...");
emf = Persistence.createEntityManagerFactory("TestAppPU"); //, properties);
em = emf.createEntityManager();
em.getTransaction().begin();
tt = new Testtable(3);
tt.setField1("F1");
tt.setField2("F2");
em.persist(tt);
em.getTransaction().commit();
em.close();
emf.close();
}
}
和持久性xml
<?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="TestAppPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>Model.Testtable</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Test"/>
<property name="javax.persistence.jdbc.user" value="Test"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="Test"/>
</properties>
</persistence-unit>
</persistence>
实体
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "TESTTABLE")
@NamedQueries({
@NamedQuery(name = "Testtable.findAll", query = "SELECT t FROM Testtable t"),
@NamedQuery(name = "Testtable.findByField1", query = "SELECT t FROM Testtable t WHERE t.field1 = :field1"),
@NamedQuery(name = "Testtable.findByField2", query = "SELECT t FROM Testtable t WHERE t.field2 = :field2"),
@NamedQuery(name = "Testtable.findById", query = "SELECT t FROM Testtable t WHERE t.id = :id"),
@NamedQuery(name = "Testtable.findByField3", query = "SELECT t FROM Testtable t WHERE t.field3 = :field3")})
public class Testtable implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "FIELD1")
private String field1;
@Column(name = "FIELD2")
private String field2;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@Column(name = "FIELD3")
@Temporal(TemporalType.DATE)
private Date field3;
public Testtable() {
}
public Testtable(Integer id) {
this.id = id;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getField3() {
return field3;
}
public void setField3(Date field3) {
this.field3 = field3;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Testtable)) {
return false;
}
Testtable other = (Testtable) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "tesyapp.Testtable[ id=" + id + " ]";
}
}
和堆栈跟踪
run:
Creating the Factory...
[EL Info]: 2019-08-08 15:25:30.61--ServerSession(1477657879)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2019-08-08 15:25:30.8--ServerSession(1477657879)--file:/C:/Users/e.kriezis/Documents/NetBeansProjects/TestApp/build/classes/_TestAppPU login successful
[EL Warning]: metamodel: 2019-08-08 15:25:30.82--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
Exception in thread "main" java.lang.IllegalArgumentException: No [EntityType] was found for the key class [Model.Testtable] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>Model.Testtable</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
[EL Warning]: metamodel: 2019-08-08 15:25:30.841--The collection of metamodel [EntityType] types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element. The lookup on [class Model.Testtable] will return null.
at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entityEmbeddableManagedTypeNotFound(MetamodelImpl.java:173)
at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entity(MetamodelImpl.java:193)
at tesyapp.TesyApp.main(TesyApp.java:45)
C:\Users\e.kriezis\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\e.kriezis\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 1 second)
我如何克服此异常,并将openjdk与netbens 11结合使用,以便使用jdk8升级用netbeans 8编写的现有Java项目