我正在使用 Hibernate 4.1.9 创建一个简单的 Java 应用程序。我可以从数据库中检索数据,但是当我尝试插入新记录时,它们没有出现在表中,并且出现了一个我无法弄清楚的错误。
这是输出:
feb. 24, 2013 10:01:52 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
feb. 24, 2013 10:01:52 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/mine/hibernateproject01/UserEntity.hbm.xml
feb. 24, 2013 10:01:52 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
feb. 24, 2013 10:01:52 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
feb. 24, 2013 10:01:52 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
feb. 24, 2013 10:01:52 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
feb. 24, 2013 10:01:52 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [sun.jdbc.odbc.JdbcOdbcDriver] at URL [jdbc:odbc:MySqlServer]
févr. 24, 2013 10:01:52 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=, password=****}
feb. 24, 2013 10:01:52 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
feb. 24, 2013 10:01:52 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
févr. 24, 2013 10:01:52 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
feb. 24, 2013 10:01:52 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
java.lang.UnsupportedOperationException
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(JdbcOdbcConnection.java:1750)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy4.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:105)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:103)
at org.hibernate.id.IdentityGenerator$InsertSelectDelegate.prepare(IdentityGenerator.java:137)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2936)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:744)
at org.mine.hibernateproject01.App.main(App.java:31)
这是代码
public static void main( String[] args )
{
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
UserEntity u = new UserEntity("name","firstname");
session.save(u); <== error occur here
session.flush();
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
System.err.println(e);
} finally{
session.close();
}
sessionFactory.close();
}
这是 hibernate.cfg.xml :
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
<property name="connection.url">jdbc:odbc:MySqlServer</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</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 on startup -->
<!-- <property name="hbm2ddl.auto">create</property>-->
<mapping resource="org/mine/hibernateproject01/UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是 UserEntity 类和配置:
public class UserEntity {
private int UserID;
private String UserName;
private String UserFirstName;
public UserEntity(){
}
public UserEntity(String UserName, String UserFirstName) {
this.UserName = UserName;
this.UserFirstName = UserFirstName;
}
public int getUserID() {
return UserID;
}
public void setUserID(int UserID) {
this.UserID = UserID;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
public String getUserFirstName() {
return UserFirstName;
}
public void setUserFirstName(String UserFirstName) {
this.UserFirstName = UserFirstName;
}
}
<hibernate-mapping package="org.mine.hibernateproject01" schema="dbo">
<class name="UserEntity" table="T_USERLIST" lazy="false">
<id name="UserID" column="UserID" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="UserName" column="UserName" type="java.lang.String" length="50" not-null="true"/>
<property name="UserFirstName" column="UserFirstName" type="java.lang.String" length="50" not-null="true"/>
</class>
</hibernate-mapping>
我可能做错了什么?
对此的任何建议或帮助都会很棒。
谢谢