这是一个类似的帖子,我之前看到的关于这个例外,但我完全迷失了。我还没有使用JPA将实体持久化到数据库,尽管我已经从使用它的表中读取没有问题。我的设置是使用Glassfish 3.1.1的Netbeans 7.1,EclipseLink是我的持久性提供程序。我有一个非常简单的场景,我只想测试将人名和年龄写入数据库并使id自动递增。它是一个MySql数据库,包含字段:Id,FirstName和Age。继承我的代码:
Web servlet从html表单中获取名称和年龄:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
if(userPath.equals("/addUser")){
//get request parameters from form
String name = request.getParameter("name");
String age = request.getParameter("age");
//set request attributes to be used by forwarded page
request.setAttribute("name", name);
request.setAttribute("age", age);
//create manager class to add person to database
Manager manager = new Manager();
manager.addPerson(name, age);
userPath = "/result";
}
// use RequestDispatcher to forward request internally
String url = "/WEB-INF/view" + userPath + ".jsp";
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
接受名称和年龄的经理类,创建人物对象并保留它。
public class Manager {
private static final String PERSISTENCE_UNIT_NAME = "FormPU";
private static EntityManagerFactory factory;
public Manager() {
}
public void addPerson(String name, String age) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Persons persons = new Persons();
persons.setName(name);
persons.setAge(age);
em.getTransaction().begin();
em.persist(persons);
em.getTransaction().commit();
em.close();
}
}
人物实体类:
/**
*
* @author esmiala
*/
@Entity
@Table(name = "persons")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Persons.findAll", query = "SELECT p FROM Persons p"),
@NamedQuery(name = "Persons.findById", query = "SELECT p FROM Persons p WHERE
p.id = :id"),
@NamedQuery(name = "Persons.findByFirstName", query = "SELECT p FROM Persons p
WHERE p.firstName = :firstName"),
@NamedQuery(name = "Persons.findByAge", query = "SELECT p FROM Persons p WHERE
p.age = :age")})
public class Persons implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "FirstName")
private String firstName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "Age")
private String age;
public Persons() {
}
public Persons(Integer id) {
this.id = id;
}
public Persons(Integer id, String firstName, String age) {
this.id = id;
this.firstName = firstName;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@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 Persons)) {
return false;
}
Persons other = (Persons) 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 "entity.Persons[ id=" + id + " ]";
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="FormPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/form</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
注意:我也尝试将exclude-unlisted-classes标记设置为true并单独列出该类,但这也不起作用。
例外:
WARNING: StandardWrapperValve[Controller]: PWC1406: Servlet.service() for servlet
Controller threw exception
java.lang.IllegalArgumentException: Object: entity.persons[ id=null ] is not a
known entity type.
atorg.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObject
ForPersist(UnitOfWorkImpl.java:4141)
atorg.eclipse.persistence.internal.jpa.EntityManagerImpl.
persist(EntityManagerImpl.java:368)
at manager.Manager.addPerson(Manager.java:36)
at controller.Controller.doPost(Controller.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
......等等。任何帮助将不胜感激!
答案 0 :(得分:1)
<exclude-unlisted-classes>
无法正常工作 - persistence.xml
中此元素的存在会禁用@Entity
类的自动发现,无论其内部是什么。
此外,@Entity(name="persons")
可能不是您想要的,请改用@Entity @Table (name="persons")
。
答案 1 :(得分:1)
所以你说你可以读得很好,但是在持续新实例时会出错?
您可以更新您阅读的对象吗?
您似乎遇到了某种类加载器问题。不知何故,你在类路径上有两次类,或者有两个不同的类加载器。您传递给的对象持久化来自与JPA正在使用的不同的类加载器。您可以检查读取内容的类加载器,以及持久化对象以查看它们之间的差异。
您是否已重新部署应用程序或已部署?如果正确关闭/重新启动服务器,它是否有效。确保在重新部署之前关闭旧的EntityManagerFactory。
答案 2 :(得分:0)
关于具体问题,请尝试查看此link是否有帮助。
无论如何,实例化EntityManager的方式不是线程安全的。
您可以看到here为什么。或者,更好的是,您可以使用NetBeans的向导从实体类创建JPA控制器类,并查看它如何注入EntityManager:
@PersistenceContext
private EntityManager em;
另请参阅控制器类(相当于Manager POJO)具有Stateless
注释。这是因为您可以安全地在其生命周期由Web容器管理的对象中注入EJB(在本例中为EntityManager)(有关访问Enterprise Bean的更多参考,请参阅here。)