2017-06-16 76 views
0

如果表名employee已经存在,该程序会将值更新到数据库中。如果该表不存在,它不会创建一个,而是会给出错误消息。有人善意地解决这个问题。我不知道我哪里有问题休眠不会自动创建表,但是在创建表时,会将值插入表中

hibernate.cfg.xml 
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 
<!-- Database connection settings --> 
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="hibernate.connection.url">jdbc:mysql://localhost/salarymanagement</property> 
<property name="hibernate.connection.username">root</property> 
<property name="hibernate.connection.password">root</property> 

<!-- JDBC connection pool --> 
<property name="connection.pool_size">1</property> 

<!-- SQL dialect --> 
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

<!-- Disable the second level cache --> 
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

<!-- Echo all executed SQl to stdout--> 
<property name="show_sql">true</property> 

<!-- drop and re-create the database schema--> 
<property name="hibernate.hbm2ddl.auto">create</property> 

<!-- Lists the annotated entity class --> 
<mapping class="com.Hibernate.Employee"/> 
</session-factory> 
</hibernate-configuration> 

MainClass

package com.Hibernate; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

public class EmployeeExecution 
{ 
public static void main(String args[]) 
{ 
Employee e1 = new Employee(1,"sri",27,30000); 
SessionFactory sessionfactory = new 
Configuration().configure().buildSessionFactory(); 
Session session=sessionfactory.openSession(); 
session.beginTransaction(); 
session.save(e1); 
session.getTransaction().commit(); 
} 

} 

POJO类

package com.Hibernate; 

import javax.persistence.Entity; 
import javax.persistence.Id; 


@Entity 
public class Employee 
{ 
@Id 
private int id; 
private String name; 
private int age; 
private int salary; 


public Employee(int id, String name, int age, int salary) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.age = age; 
    this.salary = salary; 
} 


public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public int getAge() { 
    return age; 
} 

public void setAge(int age) { 
    this.age = age; 
} 

public int getSalary() { 
    return salary; 
} 

public void setSalary(int salary) { 
    this.salary = salary; 
} 
} 

错误消息

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement 
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147) 
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) 
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) 
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1441) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:491) 
at 

org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3201) 
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2411) 
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467) 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146) 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38) 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220) 
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68) 
    at com.Hibernate.EmployeeExecution.main(EmployeeExecution.java:17) 
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement 
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) 
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3003) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3503) 
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463) 
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) 
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) 
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1435) 
    ... 9 more 
Caused by: java.sql.SQLSyntaxErrorException: Table 'salarymanagement.employee' doesn't exist 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513) 
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115) 
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983) 
    at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826) 
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2034) 
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:1970) 
    at com.mysql.cj.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5001) 
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1955) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205) 
    ... 18 more 
00:52:26.579 [pool-1-thread-1] DEBUG org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - Connection pool now considered primed; min-size will be maintained 
+0

更改并更新e' update' – soorapadman

+0

它仍然给出相同的错误 –

回答

1

请尝试这个

<prop key="hibernate.hbm2ddl.auto">create</prop>

+0

OP在'xml'中实际添加的 – soorapadman

+0

是的,因为配置中的属性如下所示。在测试过程中这是可以的,但在生产中您需要禁用此功能。
'<丙键= “hibernate.hbm2ddl.auto”>创建' –

+0

当我改为 <丙键= “hibernate.hbm2ddl.auto”>创建 它显示错误在 <会话厂>在hiberbate.cfg.xml文件中标记 –

2

hibernate.cfg.xml文件


` “ - //休眠/ Hibernate配置DTD 3.0 // EN”
“http://hibernate.sourceforge.net/冬眠配置-3.0.dtd“>

<session-factory> 

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/ems</property> 

    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 

    <property name="hbm2ddl.auto">create</property> 

    <mapping class="com.scs.model.Employee1" /> 



</session-factory> 

`

Employee.java `@实体 公共类Employee1 {

@Id 
private int id; 
private String name; 
private int age; 
private int salary; 


public Employee1(int id, String name, int age, int salary) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.age = age; 
    this.salary = salary; 
} 


public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getAge() { 
    return age; 
} 
public void setAge(int age) { 
    this.age = age; 
} 
public int getSalary() { 
    return salary; 
} 
public void setSalary(int salary) { 
    this.salary = salary; 
} 

} `

Main.java

'公共类主要{

public static void main(String arg[]){ 

     //Creating Configuration file 
     Configuration cfg = new AnnotationConfiguration().configure(); 

     //opening session 
     Session session = cfg.buildSessionFactory().openSession(); 

     Transaction transaction = session.beginTransaction(); 

     Employee1 emp1 = new Employee1(1,"sri",27,30000); 

     session.persist(emp1); 

     transaction.commit(); 
    }`