-->
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.  [ 3 posts ] 
Author Message
 Post subject: Bug in hibernate?
PostPosted: Mon Nov 19, 2007 4:55 am 
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? Or maybe it is bug in Hibernate?

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:10 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
buddy do u know that y this ConstraintViolationException comes ?

try to check the data of the table or change the identity scheme


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 11:11 am 
Newbie

Joined: Fri Nov 16, 2007 12:21 pm
Posts: 3
Everything should be ok in database, identity column is set. Maybe the problem is that there is usage of @IdClass(WorkingDayEntryPK.class) in entity class? I will try to get rid of it and see if it will help.


Code:
public class WorkingDayEntryPK implements Serializable {
   

    private int workItemId;
   

    private String userName;
   

    private Integer entryNo;
   
    /** Creates a new instance of WorkingDayEntryPK */
    public WorkingDayEntryPK() {
    }
....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.