Hibernate:自动创建数据库表和一些错误

时间:2013-07-30 07:12:51

标签: hibernate myeclipse

我在MyEclipse中有实体类:

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

@Entity
public class User{
    @Id 
    private int userId;
    private String userName;

    public User(int userId, String userName){
        this.userId = userId;
        this.userName = userName;
    }

我有我的配置xml:

<?xml version='1.0' encoding='UTF-8'?>

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
    <property name="dialect">
        org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.url">
        jdbc:sqlserver://localhost:1433;databaseName=SecondHibernate
    </property>
    <property name="connection.username">maomaoda1988</property>
    <property name="connection.password">maomaoda1988</property>
    <property name="connection.driver_class">
        com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="myeclipse.connection.profile">
        SecondHibernate
    </property>
    <property name="show_sql">true</property>
    <property name="connection.pool_size">1</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <mapping class="entity.User" />

</session-factory>

</hibernate-configuration>

我有主要方法

package hibernateTest;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.*;
import hibernate.*;

public class HibernateTest {

    public static void main(String[] args) {
        Session session = HibernateSessionFactory.getSession();
        User user = new User(1, "Chris");
        Transaction t = null;

        try{
        t = session.beginTransaction();
        session.save(user);
        t.commit();
        }
        catch (HibernateException e){
            if(t != null) t.rollback();
            e.printStackTrace();
        }
        finally{
        session.close();
        }
    }

}

但是我收到了错误:

Jul 30, 2013 12:06:36 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Jul 30, 2013 12:06:36 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
Jul 30, 2013 12:06:36 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 30, 2013 12:06:36 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 30, 2013 12:06:36 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jul 30, 2013 12:06:36 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jul 30, 2013 12:06:36 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jul 30, 2013 12:06:36 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jul 30, 2013 12:06:36 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
Jul 30, 2013 12:06:36 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Jul 30, 2013 12:06:36 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=SecondHibernate]
Jul 30, 2013 12:06:36 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=maomaoda1988, password=****}
Jul 30, 2013 12:06:36 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Jul 30, 2013 12:06:36 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jul 30, 2013 12:06:36 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jul 30, 2013 12:06:37 AM org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: entity.User (class must be instantiated by Interceptor)
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table User
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table User
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Incorrect syntax near the keyword 'User'.
Hibernate: create table User (userId int not null, userName varchar(255), primary key (userId))
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table User (userId int not null, userName varchar(255), primary key (userId))
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Incorrect syntax near the keyword 'User'.
Jul 30, 2013 12:06:37 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into User (userName, userId) values (?, ?)
Jul 30, 2013 12:06:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 156, SQLState: S0001
Jul 30, 2013 12:06:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Incorrect syntax near the keyword 'User'.
org.hibernate.exception.SQLGrammarException: Incorrect syntax near the keyword 'User'.
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy7.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2859)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3300)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at hibernateTest.HibernateTest.main(HibernateTest.java:19)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'User'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 16 more
Jul 30, 2013 12:06:37 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements

数据库没有表格。我希望Hibernate基于实体类自动创建表。我怎么做到这一点? 非常感谢你。

1 个答案:

答案 0 :(得分:3)

我遇到了问题。语法“create table User(userId int not null,userName varchar(255),primary key(userId))”中的表名“User”是SQL中的关键字。我将其更改为“MyUser”。 如果您有任何语法错误,最好在SQL环境中尝试,然后您将找到答案。