• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

创建表时出现休眠异常

java 来源:Mariusz Mikołajczak 14次浏览

在yt通道上找到一个教程后,我尝试使用Hibernate创建一个简单的应用程序。当我执行代码时,我得到了一堆异常,我无法确定原因。但是,创建表格和插入数据的结果是可见的,并且工作正常 – 这使我最困惑。下面我提供必要的课程代码,persistence.xml和堆栈跟踪。我怎么解决这个问题?创建表时出现休眠异常

STACK TRACE: 
    cze 24, 2017 7:39:31 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException 
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement 
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement 
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:331) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:230) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:144) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309) 
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889) 
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) 
    at org.hibernate.test.Main.main(Main.java:12) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate_tests.employee' doesn't exist 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
    at com.mysql.jdbc.Util.getInstance(Util.java:408) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2444) 
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745) 
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) 
    ... 16 more 

cze 24, 2017 7:39:31 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection 
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.[email protected]363042d7] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 
cze 24, 2017 7:39:31 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources 
INFO: HHH000476: Executing import script 'org.hiber[email protected]59d2400d' 
cze 24, 2017 7:39:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate_tests] 

persistence.xml

<persistence 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" 
version="2.0"> 
<persistence-unit name="myDatabase" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <properties> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
     <property name="javax.persistence.jdbc.user" value="miki" /> 
     <property name="javax.persistence.jdbc.password" value="***" /> 
     <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate_tests" /> 
     <property name="hibernate.dialect.storage_engine" value="innodb" /> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" /> 
     <property name="hibernate.hbm2ddl.auto" value="create" /> 
    </properties> 
</persistence-unit> 

Main.java

public class Main { 

public static void main(String[] args) { 
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myDatabase"); 
    EntityManager entityManager = entityManagerFactory.createEntityManager(); 

    Employee employee = new Employee(); 
    Address address = new Address(); 
    employee.setAddress(address); 
    employee.setFirstName("aaa"); 
    employee.setLastName("bbb"); 
    employee.setSalary(10000); 

    address.setLocalization("ccc"); 
    address.setStreet("ddd"); 
    address.setPostalCode("12-456"); 

    /* entityManager.getTransaction().begin(); 
    entityManager.persist(address); 
    entityManager.persist(employee); 
    entityManager.getTransaction().commit();*/ 

    entityManager.close(); 
    entityManagerFactory.close(); 
} 

} 

Employee.java

@Entity 
public class Employee { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private long id; 
@Column(name = "Imie") 
private String firstName; 
@Column(name = "Nazwisko") 
private String lastName; 
@Transient 
private double salary; 
@OneToOne 
@JoinColumn 
private Address address; 

/*sett.&gett*/ 
} 

Address.java

@Entity 
public class Address { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private long id; 

@Column(name = "Miejscowosc") 
private String localization; 
@Column(name = "Ulica") 
private String street; 
@Column(name = "Kod_Pocztowy") 
private String postalCode; 
/*sett.&gett.*/ 


===========解决方案如下:

首先确保你的数据库hibernate_tests包含表employee,其次要确保把你的表的名称中的实体,如:

@Entity 
@Table(name = "employee", catalog = "hibernate_tests", schema = "schema_name") 
public class Employee {...} 

如果您有一个默认模式,那么你不需要使用它只是使用:

@Entity 
@Table(name = "employee") 
public class Employee {...} 

如果这样还是不行,您可以检查使用数据库的名称:

@Entity 
@Table(name = "employee", catalog = "hibernate_tests") 
public class Employee {...} 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)