简体   繁体   中英

Table not found in JPA with MySQL database

I try to create a sample project with JPA, EJB and REST service. Service is working well and I can send Post and GET requests. When I tried to connect insert simple object through EJB, I got table not found error. I tried creating the table manually but it didn't work tr

Here is my Entity class

Employee

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

    @Id
    @Column(name = "emp_id")
    private int empId;

    @Basic(optional = false)
    @Column(name = "name", nullable = false, length = 63)
    private String name;

    @Basic(optional = false)
    @Column(name = "email", nullable = false, length = 255)
    private String email;

    @Basic(optional = false)
    @Column(name = "salary", columnDefinition = "integer default 2500")
    private int salary;

}

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="EmployeePU" transaction-type="JTA">
        <class>com.ams.company.Entity.Employee</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/company" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
            <property name="eclipselink.ddl-generation-mode" value="ddl_database_generation" />
        </properties>

    </persistence-unit>
</persistence>

EmployeeManager

@Stateless
public class EmployeeManager {
    @PersistenceContext(unitName = "EmployeePU")
    private EntityManager entityManager;

    public EmployeeManager() {
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean addEmployee(Employee employee) {
        entityManager.persist(employee);

        return true;
    }
}

Solution based on comments above:

When connecting to the MySQL Server, you must use the MySQL JDBC driver .

You can't use the JDBC driver for any other brand of database (the problem in this case was using the Apache Derby JDBC driver).

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