I am new to Hibernate. I am using SQL Server 2008, Java 1.7, Hibernate 4.17. I can retrieve data from the database using Hibernate but when I try to save using session.save(Object object) it get the following message.
Batch update returned unexpected row count from update [0]; actual row count: -1; expected: 1
The data in the database isn't affected. I have tried turning off and on NOCOUNT and I get the same error. I have been fighting this for three days. Below is all code and configurations I have. Any Help would be very appreciated.
Hibernate Config XML <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.microsoft.sqlse rver.jdbc.SQLServerDriver</property> <property name="connection.url">jdbc:sqlserver://xxxxxxxxxxxx;databaseName=ACCOUNTING; </property> <property name="connection.username">ACCTPROXY</property> <property name="connection.password">proxyacct</property> <property name="dialect">org.hibernate.dialect.SQLServer2008 Dialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.No CacheProvider</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="order_updates">true</property> <property name="current_session_context_class">thread</property> <property name="hibernate.cache.use_second_level_cache">fals e</property> <property name="hibernate.cache.use_query_cache">false</property> <property name="hbm2ddl.auto">validate</property> <mapping resource="com/xxx/xml/mapping/cc.hbm.xml" /> </session-factory> </hibernate-configuration>
Mapping XML File
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "- //Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="com.AccountingObj.CashCollection" table="tblCashCollections"> <id name="cashCollVouID" type="integer" column="CashCollVouID" > <generator class="identity"/> </id> <property name="CCVoucherNum" type="string"> <column name="CCVoucherNum" sql-type="varchar" length="30" not-null="true"/> </property> <property name="CCVouDate" type="string"> <column name="CCVouDate" sql-type="date" not-null="true"/> </property> <property name="ALCID" type="int"> <column name="ALCID" sql-type="int" not-null="true" default="-1"/> </property> <property name="Obsolete" type="boolean"> <column name="Obsolete" sql-type="bit" not-null="true" default="0"/> </property> <property name="Posted" type="string"> <column name="Posted" sql-type="datetime" not-null="true"/> </property> </class> </hibernate-mapping>
public class CashCollection {
private int cashCollVouID = -1, alcid = -1;
private String ccVoucherNum = "";
private String ccVouDate, posted;
private boolean obsolete = false;
public void setCashCollVouID(int cashCollVouID) { this.cashCollVouID = cashCollVouID; } public int getCashCollVouID() { return this.cashCollVouID; }
public void setCCVoucherNum(String ccVoucherNum) { this.ccVoucherNum = ccVoucherNum; } public String getCCVoucherNum() { return this.ccVoucherNum; } public void setCCVouDate(String ccVouDate) { this.ccVouDate = ccVouDate; } public String getCCVouDate() { return this.ccVouDate; } public void setALCID(int alcid) { this.alcid = alcid; } public int getALCID() { return this.alcid; } public void setPosted(String posted) { this.posted = posted; } public String getPosted() { return this.posted; } public void setObsoleteID(boolean obsolete) { this.obsolete = obsolete; } public boolean getObsoleteID() { return this.obsolete; } @Override public String toString() {
String returnValues = "cashCollVouID=" + Integer.toString(cashCollVouID) + ";\n "; returnValues += "ccVoucherNum=" + ccVoucherNum + ";\n "; returnValues += "ccVouDate=" + ccVouDate + ";\n "; returnValues += "alcid=" + alcid + ";\n "; returnValues += "posted=" + posted + ";\n "; returnValues += "obsolete=" + obsolete + ";\n ";
return returnValues; }
}
public class CashCollectionDaoHibernate implements CashCollectionDao {
private HibernateConfig hibernateConfig = null; private SessionFactory sessionFactory;
public void setHibernateConfig(HibernateConfig hibernateConfig) { this.hibernateConfig = hibernateConfig; sessionFactory = hibernateConfig.getSessionFactory(); }
public void saveorupdateCashCollection(CashCollection cashCollectionObject) { Session session = sessionFactory.openSession(); try { session.createSQLQuery("SET NOCOUNT ON").executeUpdate(); session.getTransaction().begin(); session.saveOrUpdate(cashCollectionObject); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { session.close(); } } //This method works great. public CashCollection getCashCollectionByID(int CashCollectionID) { String queryStr = "from com.AccountingObj.CashCollection where CashCollVouID = :ccid ";
Session session = sessionFactory.openSession(); Query query = session.createQuery(queryStr).setInteger("ccid", CashCollectionID); @SuppressWarnings("unchecked") List<CashCollection> entities = (List<CashCollection>)query.list(); if (entities.size() > 0) return (CashCollection)entities.get(0); else return null; }
}
I can pull data from table:
cashCollVouID=1; ccVoucherNum=UTILITY; ccVouDate=2011-08-26; alcid=1; posted=2012-09-14; obsolete=false;
Show SQL Displays: Hibernate:
SET NOCOUNT ON Hibernate: update tblCashCollections set CCVoucherNum=?, CCVouDate=?, ALCID=?, Obsolete=?, Posted=? where CashCollVouID=?
|