错误就像项目启动时一样 “org.apache.jasper.JasperException:java.lang.IllegalArgumentException:Object:emp.Customer [id = 100]不是已知的实体类型。” 如何解决这个错误请告诉我
1)newjsp.jsp
<%--
Document : newjsp
Created on : Oct 10, 2015, 1:10:21 PM
Author : Niral
--%>`enter code here`
<%@page import="emp.*"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("WebApplicationJPAPU");
EntityManager em = emf.createEntityManager();
//javax.transaction.UserTransaction utx;
Customer c1 = new Customer();
em.getTransaction().begin();
c1.setId(100l);
c1.setBalance(10000);
em.persist(c1);
em.getTransaction().commit();
%>
</body>
</html>
2)
/*
* 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 emp;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.*;
import javax.persistence.Id;
/**
*
* @author Niral
*/
@Entity
@Table(name = "CustMaster")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CustId")
private Long id;
@Column(name = "CustBalance")
private double Balance;
public double getBalance() {
return Balance;
}
public void setBalance(double Balance) {
this.Balance = Balance;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return "emp.Customer[ id=" + id + " ]";
}
}
3)persistence.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">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<persistence-unit name="WebApplicationJPAPU" transaction-type="JTA">
<jta-data-source>jdbc/e4</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>`enter code here`
</persistence-unit>
</persistence>
答案 0 :(得分:0)
您的问题可能类似于post。
理想情况下,由于您在persistence.xml文件中注释了<exclude-unlisted-classes>false</exclude-unlisted-classes>
,因此它应该自动检测类路径中的实体类。问题是,出于某种原因它没有这样做。
如果您只有一小组实体类,或假设您只有一个实体需要管理(在本例中是您的客户实体),那么我建议您明确列出persistence.xml文件中的实体类。见下面的例子:
<persistence-unit name="WebApplicationJPAPU" transaction-type="JTA">
<class>emp.Customer</class>
...
</persistence-unit>