-->
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.  [ 6 posts ] 
Author Message
 Post subject: Duplicate key or integrity constraint violation in Hibernate
PostPosted: Thu Sep 29, 2005 12:02 pm 
Newbie

Joined: Fri Sep 09, 2005 3:21 am
Posts: 4
Hi,

I am currently implementing Hibernate in my project. I am getting a BatchUpdateException.

I have a table USER_PROFILE and a hibernate persistence class UserProfile.java When I attempt to save an instance of UserProfile in hibernate, though not frequently once in a while an error occurs, the text of which is placed below:

"java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'ram03' for key 1"

'ram03' is the value I am giving for the USER_PROFILE table's primary key column 'USERID'.

the hibernate code snippet I am using is as below:
public void setPracticeInfo(PracticeInfoDTO practiceDTO)
throws NamingException, HibernateException {
logger.debug("Entering updateUserProfile()");
Session session = HibernateUtil.getHibernateSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Object obj =
session.get(PracticeInfo.class, practiceDTO.getUserId());

if (obj != null) {
PracticeInfo practiceInfo = (PracticeInfo) obj;
moveDataFromDtoToPracticeInfo(practiceDTO,
practiceInfo, session);
session.update(practiceInfo);
} else {
PracticeInfo practiceInfo = new PracticeInfo();
moveDataFromDtoToPracticeInfo(practiceDTO,
practiceInfo, session);
session.save(practiceInfo);
}

tx.commit();
} catch( HibernateException he) {
if(tx!=null)
tx.rollback();
logger.error("Caught HibernateException:
updateUserProfile()",he);
throw he;
} finally {
session.close();
}
logger.debug("Exiting updateUserProfile()");
}

Environment: JBoss4.0, MySQL4.1, Windows2000 & Hibernate2 that is shipped along with JBoss4.0

thanks,
Ram

_________________
RamPrasad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 4:56 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
In the following line on code:
Code:
Object obj = session.get(PracticeInfo.class, practiceDTO.getUserId());



practiceDTO.getUserId() is mapped to the primary key (ram03) right?
Do you have an automatic initialization of the primary key field in your constructor by any chance?

Please post your mapping. I would like to see how the primary key is mapped. also, post the constructor of your PracticeInfo object.


PS - Use the CODE tag when you post code. It makes it a lot easier to read...

Code:
public void setPracticeInfo(PracticeInfoDTO practiceDTO) throws NamingException, HibernateException {
        logger.debug("Entering updateUserProfile()");

        Session session = HibernateUtil.getHibernateSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();

            Object obj = session.get(PracticeInfo.class, practiceDTO.getUserId());

            if (obj != null) {
                PracticeInfo practiceInfo = (PracticeInfo) obj;
                moveDataFromDtoToPracticeInfo(practiceDTO, practiceInfo, session);
                session.update(practiceInfo);
            } else {
                PracticeInfo practiceInfo = new PracticeInfo();
                moveDataFromDtoToPracticeInfo(practiceDTO, practiceInfo, session);
                session.save(practiceInfo);
            }

            tx.commit();
        } catch (HibernateException he) {
            if (tx != null) {
                tx.rollback();
            }

            logger.error("Caught HibernateException:
updateUserProfile()", he);
            throw he;
        } finally {
            session.close();
        }

        logger.debug("Exiting updateUserProfile()");
    }

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 5:26 pm 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
ram03(may be PK also) is Unique in the Database.you are trying to insert the row with the same value of ram03 into the database..Give a different value to ram03 thats already not there in the DB and try it.Iam sure you won't get this exception...

for example you may already have a value for ram03 as 3..try to give a new value to ram03(for example 4) and try


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 2:14 am 
Newbie

Joined: Fri Sep 09, 2005 3:21 am
Posts: 4
Hi,
The PracticeInfoDTO has no explicit constructor declared.

All the persistance classes (viz. com.mymedwork.hibernate.PracticeInfo) have a no argument constructor.

I am posting contents of .hbm.xml files.

practiceInfo.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.mymedwork.hibernate.PracticeInfo" table="t_practice_info">
        <id name="userId" column="USERID">
            <generator class="foreign">
                <param name="property">user</param>
            </generator>
        </id>

        <property name="branchName" type="string" column="BRANCH_NAME"/>
        <property name="departmentName" type="string" column="DEPARTMENT_NAME"/>
        <property name="soloPractice" type="char" column="SOLO_PRACTICE"/>
        <property name="groupPractice" type="char" column="GROUP_PRACTICE"/>
        <property name="multiSpecialtyGroup" type="char" column="MULTI_SPECIALTY_GROUP"/>
        <property name="hospital" type="char" column="HOSPITAL"/>
        <property name="researchInstitution" type="char" column="RESEARCH_INSTITUTION"/>
        <property name="address" type="string" column="ADDRESS"/>
        <property name="city" type="string" column="CITY"/>
        <property name="state" type="string" column="STATE"/>
        <property name="zipCode" type="integer" column="ZIPCODE"/>
        <property name="country" type="string" column="COUNTRY"/>
        <property name="phone" type="string" column="PHONE"/>
        <property name="fax" type="string" column="FAX"/>
        <property name="pager" type="string" column="PAGER"/>
        <property name="websiteURL" type="string" column="WEBSITE_URL"/>


        <one-to-one name="user" class="com.mymedwork.hibernate.User"/>
        <many-to-one  name="hospitalID"
                      column="HOSPITALID"
                      class="com.mymedwork.hibernate.Hospital"/>
        <many-to-one  name="healthInsurance"
                      column="HEALTHINSURANCEID"
                      class="com.mymedwork.hibernate.HealthInsurance"/>

    </class>
</hibernate-mapping>


user.hbm.xml

Code:
<hibernate-mapping>
    <class name="com.mymedwork.hibernate.User" table="t_users">
        <id name="id" type="string" column="ID">
            <generator class="assigned"/>
        </id>

        <property name="firstName" type="string" column="FIRST_NAME"/>
        <property name="middleName" type="string" column="MIDDLE_NAME"/>
        <property name="lastName" type="string" column="LAST_NAME"/>
        <property name="suffix" type="string" column="SUFFIX"/>
        <property name="licenseNumber" type="string" column="LICENSE_NUMBER"/>
        <property name="emailID" type="string" column="EMAIL_ID"/>
        <property name="password" type="string" column="PASSWORD"/>

        <one-to-one name="userProfile" class="com.mymedwork.hibernate.UserProfile"/>
    </class>
</hibernate-mapping>


hospital.hbm.xml (healthInsurance.hbm.xml - is a similar one)
Code:
<hibernate-mapping>
    <class name="com.mymedwork.hibernate.Hospital" table="t_hospital">
        <id name="id" type="integer" column="ID">
            <generator class="native"/>
        </id>

        <property name="hospitalName" type="string" column="HOSPITAL_NAME"/>
    </class>

    <query name="com.mymedwork.hibernate.getAllHospitals">
    <![CDATA[
        from com.mymedwork.hibernate.Hospital as hospital
        order by hospital.hospitalName
    ]]>
    </query>

    <query name="com.mymedwork.hibernate.getHospitalsByName">
    <![CDATA[
        from com.mymedwork.hibernate.Hospital as hospital
        where hospital.hospitalName = :hospitalName
    ]]>
    </query>
</hibernate-mapping>


Note: the error message for "ram03" is just one instance, I am not hardcoding the UserID value, it comes from a JSP.

Hope the info I have posted will be sufficient to give a clear picture of my problem.

_________________
RamPrasad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:38 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I think the problem resides here:

Code:
<class name="com.mymedwork.hibernate.PracticeInfo" table="t_practice_info">
        <id name="userId" column="USERID">
            <generator class="foreign">
                <param name="property">user</param>
            </generator>
        </id>


You are telling hibernate to use the ID of the associated user. You are most likely associating multiple PracticeInfo object to the same User. Hence, the same primary key is submitted more than once.

Change your primary key to something native to the DB or change your mapping to make sure that it isp impossible to enter two PracticeInfo associated to the same user.

I hope this helps solve your proble,.

Thanks,
Vincent.
If it helped, please rate my post!

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:44 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I think I answered two fast. the problem is on your User class aparently!
Could you post your file mapping file please?

_________________
Vincent Giguère
J2EE Developer


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