JPA:MySQL表不存在,但它实际上存在 [英] JPA: MySQL says table don't exist, but it exist actually

查看:154
本文介绍了JPA:MySQL表不存在,但它实际上存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本创建表autolife.log:

  CREATE TABLE`log`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`idpoint` int(11)NOT NULL,
`value` varchar(10)NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
KEY`fk_point_id`(`idpoint`),
CONSTRAINT`fk_point_id` FOREIGN KEY(`idpoint`)参考` (`id`)ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB AUTO_INCREMENT = 155562 DEFAULT CHARSET = latin2 $$

所以我使用 JPA工具从Eclipse Indigo中的表创建实体:

  @Entity 
public class Log implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name =LOG_ID_GENERATOR,sequenceName =SEQUENCE_KEY)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =LOG_ID_GENERATOR)
private整数id;

私人时间戳时间;

private String value;

//双向多对一关联到点
@ManyToOne
@JoinColumn(name =idpoint)
私有点;

public Log(){
}

//获取和设置

当我在本地计算机上运行,​​工作正常,但是当我导出.war并投入生产时,给我这个错误(下面你可以看到代码生成的异常): ubuntu @ domU-12-31-39-0A-12-7B:〜/ glassfish / glassfish3 / glassfish / domains / domain1 / logs $ vi server.log
在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism。 executeSelectCall(DatasourceCallQueryMechanism.java:264)
在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
在org.eclipse.persistence.internal.queries.ExpressionQueryMechanism。 selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
在org.eclip se.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2570)
在org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:420)
在org.eclipse。 persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
在org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
在org.eclipse.persistence.queries。 ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
在org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
在org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork( ObjectLevelReadQuery.java:1128)
在org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
在org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery( AbstractSession.java:1516)
在org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
在org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
在org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
在org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
在com.brainset.eao.LogEAO.list(LogEAO.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在org.glassfish .ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
在org.glassfish.ejb.security.application.EJBSe curityManager.invoke(EJBSecurityManager.java:1124)
在com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
在com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation。 java:619)
在com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
在com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
在com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
在com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
在com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
在com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
在com.sun.ejb.containers.BaseContainer .__ intercept(BaseContainer.java:5360)
at com .sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
在com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
... 44更多
导致:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'autolife.LOG'不存在
在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
在太阳.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.Delegat ingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
在java.lang.reflect.Constructor.newInstance(Constructor.java:513)
在com.mysql.jdbc.Util.handleNewInstance(Util.java: 411)
在com.mysql.jdbc.Util.getInstance(Util.java:386)
在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com .mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4098)
在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4030)
在com.mysql.jdbc.MysqlIO.sendCommand (MysqlIO.java:2490)
在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2134)
在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2300)
在com.sun。 gjc.spi.jdbc40.PreparedStatementWrapper40.exe cuteQuery(PreparedStatementWrapper40.java:642)
在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931)
在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor。 basicExecuteCall(DatabaseAccessor.java:607)
... 88更多
|#]



您可以看到它表示该表不存在,但存在我从我的本地数据库导出并导入生产主机。
但仍然提供此错误:

 导致:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'autolife.LOG'不存在

我该怎么办?

解决方案

MySQL表名的区分大小写依赖于操作系统,我猜是这样的:


MySQL,数据库对应于数据目录中的目录。数据库中的每个表对应于数据库目录中的至少一个文件(可能更多的是根据存储引擎)。因此,底层操作系统的区分大小写在数据库和表名的区分大小写中起了一定的作用。这意味着数据库和表名在Windows中不区分大小写,并且在大多数Unix系列中区分大小写。一个显着的例外是Mac OS X,它是基于Unix的,但使用不区分大小写的默认文件系统类型(HFS +)。但是,Mac OS X还支持UFS卷,与任何Unix一样,区分大小写。


所以,你需要设置表在正确的情况下明确指定名称:

  @Entity @Table(name =log)
public class Log implements可序列化{...}


I have the follow script to create the table autolife.log:

CREATE TABLE `log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `idpoint` int(11) NOT NULL,
  `value` varchar(10) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_point_id` (`idpoint`),
  CONSTRAINT `fk_point_id` FOREIGN KEY (`idpoint`) REFERENCES `point` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=155562 DEFAULT CHARSET=latin2$$

So I use the JPA Tools to create Entity from Tables in Eclipse Indigo:

@Entity
public class Log implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="LOG_ID_GENERATOR", sequenceName="SEQUENCE_KEY")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="LOG_ID_GENERATOR")
    private Integer id;

    private Timestamp time;

    private String value;

    //bi-directional many-to-one association to Point
    @ManyToOne
    @JoinColumn(name="idpoint")
    private Point point;

    public Log() {
    }

    // get's and set's

When I run in my localhost computer, works fine, but when I export the .war and put in production, gives me this error (below you can see the exception generated by the code):

ubuntu@domU-12-31-39-0A-12-7B:~/glassfish/glassfish3/glassfish/domains/domain1/logs$ vi server.log
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
        at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
        at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2570)
        at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:420)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
        at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
        at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
        at com.brainset.eao.LogEAO.list(LogEAO.java:54)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
        at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
        at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
        at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
        at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
        ... 44 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'autolife.LOG' doesn't exist
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4098)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4030)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2134)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2300)
        at com.sun.gjc.spi.jdbc40.PreparedStatementWrapper40.executeQuery(PreparedStatementWrapper40.java:642)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:607)
        ... 88 more
|#]

As you can see it says that the table don't exist, but exist I export from my localhost database and import to the production host. But still gives this error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'autolife.LOG' doesn't exist

What should I do here ?

解决方案

Case sensitivity of MySQL table names depends on operating system, I guess that's the cause:

In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. One notable exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive. However, Mac OS X also supports UFS volumes, which are case sensitive just as on any Unix.

So, you need to set table name in the correct case explicitly:

@Entity @Table(name = "log")
public class Log implements Serializable { ... }

这篇关于JPA:MySQL表不存在,但它实际上存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆