jpa mysql表不存在。

时间:2022-09-11 11:54:16

I am trying to insert a Employee entity into the database. I am using JPA and the database is mysql. When I try to insert the entity, it gives me test.employee does not exist. I assume that I dont have to create the table. Entity will automatically create the table with the annotated name. Please find the exception and code below.

我试图将一个Employee实体插入到数据库中。我使用的是JPA,数据库是mysql。当我尝试插入实体时,它会给我一个测试。员工不存在。我假设我不需要创建这个表。实体将自动创建带有注释名的表。请找到下面的异常和代码。

Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
Error Code: 1146
Call: INSERT INTO employee (FIRST_NAME, LAST_NAME) VALUES (?, ?)
    bind => [abc,def]
Query: InsertObjectQuery(com.example.entities.Employee@ee1ede)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at com.example.dao.BaseDao.commitAndClose(BaseDao.java:41)
    at com.example..dao.BaseDao.persist(BaseDao.java:29)
    at com.example..dao.EmployeeDao.createEmployee(EmployeeDao.java:29)
    at com.example.Main.main(KITMain.java:27)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
Error Code: 1146
Call: INSERT INTO employee (FIRST_NAME, LAST_NAME) VALUES (?, ?)
    bind => [ram, mohan]
Query: InsertObjectQuerycom.example.entities.Employee@ee1ede)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:900)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:962)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:631)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1991)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:298)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1793)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1775)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1726)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:125)
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4196)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:277)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
    ... 4 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:890)

persistence.xml

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd " >

    <class>com.example.entities.Employee</class>
    <properties>

        <!-- derby -->

        <!-- <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" 
            /> <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" 
            /> <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/test" 
            /> <property name="javax.persistence.jdbc.user" value="app" /> <property 
            name="javax.persistence.jdbc.password" value="admin" /> -->


        <!-- mysql -->

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="" />

        <!-- EclipseLink should create the database schema automatically -->
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode"
            value="database" />

        <!-- Configure simple SQL logging for demonstration. -->
        <property name="eclipselink.logging.level" value="FINE" />
        <property name="eclipselink.logging.thread" value="false" />
        <property name="eclipselink.logging.session" value="false" />
        <property name="eclipselink.logging.exceptions" value="false" />
        <property name="eclipselink.logging.timestamp" value="false" />


    </properties>
</persistence-unit>

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 
 * @author ddd
 */
@Entity
@Table(name = "employee")
public class Employee implements Serializable {



    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "EMP_ID")
    private String employeeId;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName ;

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }

    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}







[EL Config]: metadata: The access type for the persistent class [class de.allianz.kit.server.persistence.entities.Employee] is set to [FIELD].
[EL Config]: metadata: The alias name for the entity class [class de.allianz.kit.server.persistence.entities.Employee] is being defaulted to: Employee.
[EL Info]: EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Fine]: connection: Detected database platform: org.eclipse.persistence.platform.database.MySQLPlatform
[EL Config]: connection: Connection(3278348)--connecting(DatabaseLogin(
    platform=>MySQLPlatform
    user name=> "root"
    datasource URL=> "jdbc:mysql://localhost:3306/test"
))
[EL Config]: connection: Connection(5863106)--Connected: jdbc:mysql://localhost:3306/test
    User: root@localhost
    Database: MySQL  Version: 5.6.17
    Driver: MySQL Connector Java  Version: mysql-connector-java-5.1.30 ( Revision: alexander.soklakov@oracle.com-20140310090514-8xt1yoht5ksg2e7c )
[EL Info]: connection: file:/C:/myworkspace/kit_ws/com.example.server.persistence/classes/_kit login successful
[EL Fine]: sql: Connection(5863106)--DROP TABLE employee
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'test.employee'
Error Code: 1051
Call: DROP TABLE employee
Query: DataModifyQuery(sql="DROP TABLE employee")
[EL Fine]: sql: Connection(5863106)--DROP TABLE employee
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'test.employee'
Error Code: 1051
Call: DROP TABLE employee
Query: DataModifyQuery(sql="DROP TABLE employee")
[EL Fine]: sql: Connection(5863106)--DROP TABLE employee
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'test.employee'
Error Code: 1051
Call: DROP TABLE employee
Query: DataModifyQuery(sql="DROP TABLE employee")
[EL Fine]: sql: Connection(5863106)--DROP TABLE employee
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'test.employee'
Error Code: 1051
Call: DROP TABLE employee
Query: DataModifyQuery(sql="DROP TABLE employee")
[EL Fine]: sql: Connection(5863106)--DROP TABLE employee
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'test.employee'
Error Code: 1051
Call: DROP TABLE employee
Query: DataModifyQuery(sql="DROP TABLE employee")
[EL Fine]: sql: Connection(5863106)--CREATE TABLE employee (EMP_ID VARCHAR(255) AUTO_INCREMENT NOT NULL, FIRST_NAME VARCHAR(255), LAST_NAME VARCHAR(255), PRIMARY KEY (EMP_ID))
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect column specifier for column 'EMP_ID'
Error Code: 1063
Call: CREATE TABLE employee (EMP_ID VARCHAR(255) AUTO_INCREMENT NOT NULL, FIRST_NAME VARCHAR(255), LAST_NAME VARCHAR(255), PRIMARY KEY (EMP_ID))
Query: DataModifyQuery(sql="CREATE TABLE employee (EMP_ID VARCHAR(255) AUTO_INCREMENT NOT NULL, FIRST_NAME VARCHAR(255), LAST_NAME VARCHAR(255), PRIMARY KEY (EMP_ID))")
[EL Fine]: sql: Connection(5863106)--INSERT INTO employee (FIRST_NAME, LAST_NAME) VALUES (?, ?)
    bind => [ram, mohan]
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
Error Code: 1146

2 个解决方案

#1


3  

You will need extra configuration if you want an auto generated varchar.

如果需要自动生成varchar,则需要额外的配置。

The generation of DDL is translating to:

DDL的生成转化为:

CREATE TABLE employee (EMP_ID VARCHAR(255) AUTO_INCREMENT NOT NULL, FIRST_NAME VARCHAR(255), LAST_NAME VARCHAR(255), PRIMARY KEY (EMP_ID))

and the log is saying:

log的意思是

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect column specifier for column 'EMP_ID'
Error Code: 1063

MySql is rejecting EMP_ID becuase it cant have AUTO_INCREMENT with varchar, and you need to specify the auto generation for it. Probably Derby has this feature, and it works different for MySql.

MySql拒绝EMP_ID,因为它不能使用varchar进行AUTO_INCREMENT,您需要为它指定自动生成。可能Derby也有这个特性,它对MySql的工作方式也不同。

You might wanna look in this answer.

你可能想看看这个答案。

Or just change the type of EMP_ID to an integer number if you dont mind the type or for a quick test.

或者,如果您不介意类型或快速测试,只需将EMP_ID类型更改为整数。

#2


0  

Could be you mysql data is corrupted.

可能是你的mysql数据被破坏了。

Go to Mysql home directory and delete all .frm extension. and restart Msysql server.

转到Mysql主目录并删除所有.frm扩展名。并重启Msysql服务器。

Solved.

解决了。

#1


3  

You will need extra configuration if you want an auto generated varchar.

如果需要自动生成varchar,则需要额外的配置。

The generation of DDL is translating to:

DDL的生成转化为:

CREATE TABLE employee (EMP_ID VARCHAR(255) AUTO_INCREMENT NOT NULL, FIRST_NAME VARCHAR(255), LAST_NAME VARCHAR(255), PRIMARY KEY (EMP_ID))

and the log is saying:

log的意思是

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect column specifier for column 'EMP_ID'
Error Code: 1063

MySql is rejecting EMP_ID becuase it cant have AUTO_INCREMENT with varchar, and you need to specify the auto generation for it. Probably Derby has this feature, and it works different for MySql.

MySql拒绝EMP_ID,因为它不能使用varchar进行AUTO_INCREMENT,您需要为它指定自动生成。可能Derby也有这个特性,它对MySql的工作方式也不同。

You might wanna look in this answer.

你可能想看看这个答案。

Or just change the type of EMP_ID to an integer number if you dont mind the type or for a quick test.

或者,如果您不介意类型或快速测试,只需将EMP_ID类型更改为整数。

#2


0  

Could be you mysql data is corrupted.

可能是你的mysql数据被破坏了。

Go to Mysql home directory and delete all .frm extension. and restart Msysql server.

转到Mysql主目录并删除所有.frm扩展名。并重启Msysql服务器。

Solved.

解决了。