Ok, I am using hibernate 2.1.6 & hsqldb 1.7.2. My mapping looks like this:
Code:
<hibernate-mapping>
<class name="my.package.model.User" table="users">
<id name="id">
<generator class="increment"/>
</id>
<property name="username">
<column name="username" length="16" not-null="true" unique="true"/>
</property>
<property name="password"/>
<property name="firstName"/>
<property name="lastName"/>
<property name="email"/>
</class>
</hibernate-mapping>
I am using Spring, so my dao looks like this:
Code:
public void save(User user) {
getHibernateTemplate().saveOrUpdate(user);
}
I have a test like this:
Code:
try {
User user = new User();
user.setUsername("pbarry");
dao.save(user);
} catch(Exception ex) {
log.error(ex);
throw ex;
}
pbarry already exists in the DB. Here is what I get in log:
Code:
11:27:38,595 - DEBUG - net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.BatchUpdateException: failed batch
at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:214)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:177)
at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:317)
at my.package.dao.impl.UserDAOImpl.save(UserDAOImpl.java:21)
at my.package.dao.UserDAOTest.testSaveNewDup(UserDAOTest.java:91)
...
Is BatchUpdateException the correct exception? Shouldn't it be something that indicates the unique constraint has been violated? For example, If I change the code to not use hiberate at all, like this:
Code:
try {
DataSource ds = (DataSource)context.getBean("dataSource");
Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement(
"INSERT INTO users (id,username) VALUES (?,?)");
ps.setInt(1,8);
ps.setString(2,"pbarry");
ps.executeUpdate();
} catch(Exception ex) {
log.error(ex);
throw ex;
}
The log has this:
Code:
java.sql.SQLException: Unique constraint violation: SYS_CT_127 in statement [INSERT INTO users (id,username) VALUES (?,?)]
So is this a hibernate misconfiguration or error on my part or is there a bug here?