无法使用EntityManager运行hibernate

时间:2011-01-21 16:02:54

标签: hibernate

我正在尝试运行一个简单的hello world程序,如“Java Persistence with Hibernate”一书中提出的那样,由Cristian Bauer&加文·金。我所做的唯一改变是将数据库从HSQLDB更改为MySQL.Code是无关紧要的,因为当我使用带有SessionFactory或Annotaion的hibernate时,一切正常。 Log说它无法找到该表。这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="helloworld" transaction-type="RESOURCE_LOCAL">
    <class>hello.Message</class>
    <properties>
        <property name="hibernate.archive.autodetection" value="class, hbm"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="use_sql_comments" value="true"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost/helloworldjpa"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="xxxx"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hbm2ddl.auto" value="update"/>
        <property name="hibernate.c3p0.min_size" value="5"/>
        <property name="hibernate.c3p0.max_size" value="20"/>
        <property name="hibernate.c3p0.timeout" value="300"/>
        <property name="hibernate.c3p0.max_statements" value="50"/>
        <property name="hibernate.c3p0.idle_test_period" value="3000"/>
    </properties>
</persistence-unit>

由于“hbm2ddl.auto”属性是更新,我预计会创建架构。如果我手动创建架构,它可以正常工作。 日志读起来就像这样:

Hibernate: 
insert 
into
    MESSAGES
    (MESSAGE_TEXT, NEXT_MESSAGE_ID) 
values
    (?, ?)
21:26:57,703  WARN JDBCExceptionReporter:71 - SQL Error: 1146, SQLState: 42S02
21:26:57,718 ERROR JDBCExceptionReporter:72 - Table 'helloworldjpa.messages' doesn't exist
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [hello.Message]
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
    at hello.HelloWorld.main(HelloWorld.java:20)
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [hello.Message]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2093)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2573)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
    ... 1 more
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'helloworldjpa.messages' doesn't exist
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
    ... 15 more

1 个答案:

答案 0 :(得分:3)

未创建架构,因为hbm2ddl.auto应为hibernate.hbm2ddl.auto

<property name="hibernate.hbm2ddl.auto" value="update"/>