我在配置模型以创建数据库表时遇到问题。我使用的是h2嵌入式数据库,因为它是我项目的先决条件。 我已经使用必要的数据注释创建了实体类。我创建了persistence.xml文件,我将hibernate.hbm2dll.auto设置为" create"因为我希望在第一次运行应用程序时创建数据库。请在下面找到我的实体和persistence.xml文件配置:
package org.crowdlib.main;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.cfg.*;
import org.hibernate.tool.hbm2ddl.*;
@Entity
@Table(name = "tblperson")
public class Person implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;
@Column(name = "name", nullable = false)
private String Name;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
文件是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="kot">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.crowdlib.main.Person</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:./db/dbpersons" />
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.username" value="test" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2dll.auto" value="create"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
</properties>
</persistence-unit>
</persistence>
客户是:
public static void main(String[] args)
{
Person t = new Person();
t.setName("hi");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("kot");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(t);
em.getTransaction().commit();
}
奇怪的是我可以看到创建了h2数据库文件。我可以看到也创建了用户测试。我收到以下错误消息:引起:org.h2.jdbc.JdbcSQLException:表&#34; TBLPERSON&#34;没找到;
这使我得出结论,JPA没有创建表。我在persistence.xml中缺少smth吗?
修改
Nov 16, 2015 11:26:42 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: kot
...]
Nov 16, 2015 11:26:42 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Nov 16, 2015 11:26:42 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 16, 2015 11:26:42 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 16, 2015 11:26:42 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Nov 16, 2015 11:26:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Nov 16, 2015 11:26:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.h2.Driver] at URL [jdbc:h2:./db/dbpersons]
Nov 16, 2015 11:26:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=test}
Nov 16, 2015 11:26:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Nov 16, 2015 11:26:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Nov 16, 2015 11:26:43 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate:
insert
into
tblperson
(id, name)
values
(null, ?)
Nov 16, 2015 11:26:44 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 42102, SQLState: 42S02
Nov 16, 2015 11:26:44 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table "TBLPERSON" not found; SQL statement:
insert into tblperson (id, name) values (null, ?) [42102-190]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
at org.crowdlib.main.dbMain.main(dbMain.java:26)
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:108)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:78)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:39)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2791)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3361)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:474)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:163)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:198)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:774)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:752)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
... 1 more
Caused by: org.h2.jdbc.JdbcSQLException: Table "TBLPERSON" not found; SQL statement:
insert into tblperson (id, name) values (null, ?) [42102-190]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5293)
at org.h2.command.Parser.readTableOrView(Parser.java:5270)
at org.h2.command.Parser.parseInsert(Parser.java:1030)
at org.h2.command.Parser.parsePrepared(Parser.java:400)
at org.h2.command.Parser.parse(Parser.java:304)
at org.h2.command.Parser.parse(Parser.java:276)
at org.h2.command.Parser.prepareCommand(Parser.java:241)
at org.h2.engine.Session.prepareLocal(Session.java:460)
at org.h2.engine.Session.prepareCommand(Session.java:402)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1188)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:72)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:276)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1122)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:106)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
... 23 more