简体   繁体   中英

Can't manage to make jpa work with glassfish and mysql

First of all i had to rename my entity class from Employee to EMPLOYEE because for whatever reason i had the error Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'EMPLOYEE' does not exist , i am using this class Employee, later renamed 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
}

and this is the persistence 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>

I am using a schema called jpa00 containing a table Employee with 3 columns: a primary key, unsigned, not null, int id ; a varchar name , and an int salary . The rest of the code uses a utility class called 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();

}

When i change the entity class from Employee to EMPLOYEE and i run this i have an output of the first operation System.out.println("Persisted "+emp); and right after that i have this error 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. That class has @Entity annotation, so i don't get what i am doing wrong. I have using NetBeans 8.1 and i have imported into the Libraries EclipseLink JPA 2.1 and MySQL JDBC Driver . I am really out of ideas on what am i supposed to do and the all capitalized Employee issue, i just don' understand how it's that possible.

First; you are trying to connect to MySql with Oracle Driver. Oracle and mysql have different drivers, edit your persistence.xml to set mysql driver

Second; @Table annotation may help for class name and table name match(You need that if your mysql configuration have case sensitive table names)

@Entity 
@Table(name = "EMPLOYEE") 
public class Employee { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM