首先,我必须将我的实体类从Employee重命名为EMPLOYEE,因为无论出于何种原因我都有错误Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'EMPLOYEE' does not exist
,我正在使用这个类Employee,后来改名为EMPLOYEE
@Entity
public class Employee {
@Id
private int id;
private String name;
private long salary;
public Employee(){}
public Employee(int id){this.id=id;}
// getters, setters
}
这是持久性xml
<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="EmployeeService" transaction-type="RESOURCE_LOCAL">
<!--<exclude-unlisted-classes>false</exclude-unlisted-classes>
-->
<class>jpa.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa00"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
</properties>
</persistence-unit>
</persistence>
我正在使用一个名为jpa00
的模式,其中包含一个包含3列的表Employee
:主键,unsigned,not null,int id
; varchar name
和int salary
。
其余代码使用名为EmployeeServices的实用程序类
public class EmployeeTest {
public static void main (String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EmployeeService");
EntityManager em = emf.createEntityManager();
EmployeeService service = new EmployeeService(em);
// create and persist an employee
em.getTransaction().begin();
Employee emp = service.createEmployee(158, "John Doe", 45000);
em.getTransaction().commit();
System.out.println("Persisted "+emp);
// find specific employee
emp = service.findEmployee(158);
System.out.println("Found "+emp);
// find all employees
List<Employee> list = service.findAllEmployees();
for(Employee y:list)
System.out.println(y);
// update the employee
em.getTransaction().begin();
emp = service.raiseEmployeeSalary(158, 1000);
em.getTransaction().commit();
System.out.println("Updated "+emp);
// remove an employee
em.getTransaction().begin();
service.removeEmployee(158);
em.getTransaction().commit();
System.out.println("Removed employee 158");
// close the Em and EMF when done
em.close();
emf.close();
}
当我将实体类从Employee更改为EMPLOYEE并且我运行它时,我有第一个操作System.out.println("Persisted "+emp);
的输出,之后我有这个错误Exception in thread "main" java.lang.IllegalArgumentException: Unknown Entity bean class: class jpa.EMPLOYEE, please verify that this class has been marked with the @Entity annotation.
该类有@Entity注释,所以我不知道我做错了什么。我使用NetBeans 8.1,并且已导入到库 EclipseLink JPA 2.1 和MySQL JDBC Driver
中。我真的不知道我应该做什么以及所有大写的员工问题,我只是不明白这是怎么回事。
答案 0 :(得分:0)
首先;您正尝试使用Oracle Driver连接到MySql。 Oracle和mysql有不同的驱动程序,编辑你的persistence.xml来设置mysql驱动程序
二; @Table注释可能有助于类名和表名匹配(如果您的mysql配置具有区分大小写的表名,则需要这样做)
@Entity
@Table(name = "EMPLOYEE")
public class Employee {