我是Hibernate的新手。 我使用NetBeans 7.4,Hibernate 4.3.6和数据库作为Ms Sql Server。 它适用于从数据库中获取数据,但它为Insert,Update,Delete提供了错误。 我的Hibernate配置文件为:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://SqlSer:1433;databaseName=HibSample</property>
<property name="hibernate.connection.username">SA</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Entity/Sample.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的HibernateUtil文件如下:
public class NewHibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
我的逆向工程文件:
<hibernate-reverse-engineering>
<schema-selection match-catalog="HibSample" match-schema="dbo"/>
<table-filter match-name="Sample"/>
</hibernate-reverse-engineering>
我的Pojo和映射文件是:
public class Sample implements java.io.Serializable {
private int id;
private String fullName;
private String address;
public Sample() {
}
public Sample(int id, String fullName) {
this.id = id;
this.fullName = fullName;
}
public Sample(int id, String fullName, String address) {
this.id = id;
this.fullName = fullName;
this.address = address;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return this.fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
和
<hibernate-mapping>
<class name="Entity.Sample" table="Sample" schema="dbo" catalog="HibSample">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="fullName" type="string">
<column name="FullName" length="50" not-null="true" />
</property>
<property name="address" type="string">
<column name="Address" length="50" />
</property>
</class>
</hibernate-mapping>
主要档案是:
public class Main
{
public Main(){};
public static void main(String args[])
{
Session session = NewHibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Sample s = new Sample();
s.setId(3);
s.setFullName("Demo");
s.setAddress("Demo");
Sample s1 = (Sample)session.load(Sample.class, 2);
System.out.println("ID :"+s1.getId());
System.out.println("Full Name : "+s1.getFullName());
System.out.println("Address : "+s1.getAddress());
session.save(s);
if(!tx.wasCommitted())
{
tx.commit();
}
}catch(Exception ex)
{
if(tx != null)
{
tx.rollback();
ex.printStackTrace();
}
}finally{
session.close();
}
System.exit(0);
}
}
表数据如下:
ID Full Name Address
1 XYZ India
2 ABC India
错误IS:
Dec 17, 2014 4:42:42 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Dec 17, 2014 4:42:42 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Dec 17, 2014 4:42:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Entity/Demo.hbm.xml
Dec 17, 2014 4:42:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Entity/Sample.hbm.xml
Dec 17, 2014 4:42:42 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Dec 17, 2014 4:42:42 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Dec 17, 2014 4:42:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Dec 17, 2014 4:42:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://150.10.10.233;databaseName=HibSample]
Dec 17, 2014 4:42:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=SA, password=****}
Dec 17, 2014 4:42:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Dec 17, 2014 4:42:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Dec 17, 2014 4:42:43 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Dec 17, 2014 4:42:43 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Dec 17, 2014 4:42:43 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
结果
ID :2
Full Name : ABC
Address : India
Hibernate: insert into HibSample.dbo.Sample (FullName, Address, ID) values (?, ?, ?)
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: -1; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at UI.Main.main(Main.java:45)
Dec 17, 2014 4:42:43 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
BUILD SUCCESSFUL (total time: 1 second)
我已经搜索了所有可能性,但无法找到解决方案。