-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Identity column problem
PostPosted: Fri Nov 16, 2007 1:44 pm 
Newbie

Joined: Fri Nov 16, 2007 12:21 pm
Posts: 3
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();       


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 5:07 pm 
Newbie

Joined: Thu Nov 08, 2007 5:02 pm
Posts: 6
Hibernate doesn't like @id annotations on more than one field. Only the primary key (in this case entryNo) should have the @Id annotation. Delete the @Id annotations on workIteimId and userName. See if that works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.