hibernate没有创建表

时间:2014-11-09 18:30:25

标签: java hibernate

所以我有几个@Entity我正在测试,前两个是创建和删除,因为<property name="hibernate.hbm2ddl.auto">create</property>在xml hibernate cfg文件中,但最后一个表不是正在创建,无法找到原因,有什么建议吗?

这是最后一张表:

        @Entity
    @Table(name="ETAPAS")
    public class Etapa {

        @Id
        @Column(name = "ID")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String origen, destino, fecha, hora, categoria, tipo;
        @Embedded
        private Puerto puerto;
        @Embedded
        private Mapa  mapa;

       //  setters &  getters 


@Embeddable
public class Mapa {


    @Column(name = "NOMBRE_MAPA")
    private String nombre;
    @Lob
    private byte[] imagen;  

       //  setters &  getters   



@Embeddable
public class Puerto {

    private int altura;
    @Column(name = "NOMBRE_PUERTO")
    private String nombre;

       //  setters &  getters 

和错误:

Starting persistence 
nov 09, 2014 1:49:42 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
nov 09, 2014 1:49:42 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
nov 09, 2014 1:49:42 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
nov 09, 2014 1:49:42 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
nov 09, 2014 1:49:42 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
nov 09, 2014 1:49:42 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
nov 09, 2014 1:49:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
nov 09, 2014 1:49:42 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
nov 09, 2014 1:49:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
nov 09, 2014 1:49:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/infoiii]
nov 09, 2014 1:49:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root}
nov 09, 2014 1:49:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
nov 09, 2014 1:49:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 5 (min=1)
nov 09, 2014 1:49:43 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
nov 09, 2014 1:49:43 PM org.hibernate.dialect.H2Dialect <init>
WARN: HHH000431: Unable to determine H2 database version, certain features may not work
nov 09, 2014 1:49:43 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
nov 09, 2014 1:49:43 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: 
    drop table CICLISTA if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table CICLISTA if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists' at line 1
Hibernate: 
    drop table ETAPAS if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table ETAPAS if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists' at line 1
Hibernate: 
    drop table USUARIOS_ADMON if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table USUARIOS_ADMON if exists
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists' at line 1
Hibernate: 
    create table CICLISTA (
        numero integer generated by default as identity,
        equipo varchar(255),
        nacionalidad varchar(255),
        nombre varchar(255),
        primary key (numero)
    )
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table CICLISTA (numero integer generated by default as identity, equipo varchar(255), nacionalidad varchar(255), nombre varchar(255), primary key (numero))
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generated by default as identity,
        equipo varchar(255),
        nacionali' at line 2
Hibernate: 
    create table ETAPAS (
        ID integer generated by default as identity,
        categoria varchar(255),
        destino varchar(255),
        fecha varchar(255),
        hora varchar(255),
        imagen blob,
        NOMBRE_MAPA varchar(255),
        origen varchar(255),
        altura integer not null,
        NOMBRE_PUERTO varchar(255),
        tipo varchar(255),
        primary key (ID)
    )
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table ETAPAS (ID integer generated by default as identity, categoria varchar(255), destino varchar(255), fecha varchar(255), hora varchar(255), imagen blob, NOMBRE_MAPA varchar(255), origen varchar(255), altura integer not null, NOMBRE_PUERTO varchar(255), tipo varchar(255), primary key (ID))
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generated by default as identity,
        categoria varchar(255),
        destin' at line 2
Hibernate: 
    create table USUARIOS_ADMON (
        ID integer generated by default as identity,
        CONTRASEÑA varchar(255),
        USUARIO varchar(255),
        primary key (ID)
    )
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table USUARIOS_ADMON (ID integer generated by default as identity, CONTRASEÑA varchar(255), USUARIO varchar(255), primary key (ID))
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generated by default as identity,
        CONTRASEÑA varchar(255),
        USUA' at line 2
nov 09, 2014 1:49:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: 
    insert 
    into
        USUARIOS_ADMON
        (ID, CONTRASEÑA, USUARIO) 
    values
        (null, ?, ?)
Hibernate: 
    insert 
    into
        CICLISTA
        (numero, equipo, nacionalidad, nombre) 
    values
        (null, ?, ?, ?)
Hibernate: 
    insert 
    into
        ETAPAS
        (ID, categoria, destino, fecha, hora, imagen, NOMBRE_MAPA, origen, altura, NOMBRE_PUERTO, tipo) 
    values
        (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
nov 09, 2014 1:49:45 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
nov 09, 2014 1:49:45 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table 'infoiii.etapas' doesn't exist
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at com.uci.ubosque.modelo.UsuariosAdmonTester.main(UsuariosAdmonTester.java:48)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'infoiii.etapas' 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.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)

2 个答案:

答案 0 :(得分:4)

原来我的问题出在Dialect文件

中的hibernate.cfg.xml

我的配置:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

这解决了我的问题:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

答案 1 :(得分:0)

根据this answer,问题在于您需要将GenerationType.IDENTITY与MySQL一起使用,而不是GenerationType.AUTO