Hi
I'm trying to save one row to table which got one column set to identity. While trying to save data and commit transaction there is exception:
Error while commiting the transaction
javax.persistence.RollbackException: Error while comiting the transaction
...
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [entities.WorkingDayEntry]
...
java.sql.SQLException: Cannot insert explicit value for identity column in table 'WorkingDayEntry' when IDENTITY_INSERT is set to OFF.
Interesting thing is that the same code is working if I change persistence provider to TopLinkEssentials in peristence.xml.
I think that hibernate generates wrong SQL because it add entryNo column to clause(TopLink is not doing it and then it works):
insert
into
WorkingDayEntry
(workingTime, startTime, endTime, area, workItemId, userName,
entryNo)
values
(?, ?, ?, ?, ?, ?, ?)
Can anyone help with this problem, how to configure hibernate properly to get it work?
I'm is using SQL Server and JDTS jdbc driver, database is configured properly(column entryNo is set to identity), hibernate version is 3.2.1.ga.
Thanks a lot for help!
Maciej
Entity class:
Code:
@IdClass(WorkingDayEntryPK.class)
@Table(name = "WorkingDayEntry")
public class WorkingDayEntry implements Serializable {
@Id
@Column(name = "workItemId", nullable = false)
private int workItemId;
@Id
@Column(name = "userName", nullable = false)
private String userName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "entryNo", nullable = false)
private Integer entryNo;
persistence.xml:
Code:
<persistence-unit name="WorkManPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.connection.password" value="user"/>
<property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://10.234.64.11:1433;databaseName=WorkMan;SelectMethod=cursor"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
Executed code:
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("WorkManPU");
EntityManager em = emf.createEntityManager();
WorkingDayEntry wde = new WorkingDayEntry();
wde.setWorkItemId(333);
wde.setUserName("logger1");
wde.setStartTime(new Date());
wde.setEntryNo(null);
wde.setEndTime(new Date());
wde.setArea(new BigDecimal(0));
wde.setWorkingTime(180);
EntityTransaction et = em.getTransaction();
et.begin();
em.persist(wde);
et.commit();
em.close();