Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / September 2004

Tip: Looking for answers? Try searching our database.

[Hibernate]session.update() calls INSERT SQL statement on a many-to-many association table in DB

Thread view: 
Tung Chau - 27 Sep 2004 20:47 GMT
Hi,
I have problem with Hibernate's session.update() and it buzzed me for
many hours already. I don't understand why and I don't know how to
solve it. I really need help.
My COURSES table has only 1 record:
COURSE_ID  COURSE_NAME     COURSE_NUMBER          DESCRIPTION
1          chicken            chick            how to cook chicken
My USERS table has only 1 user:
USER_ID LOGIN   PASSWORD   FIRST_NAME LAST_NAME  LAST_LOGIN
1      teacher  ------       teacher   teacher   2004-09-26 18:31:37

My PROFESSOR_TEACHES table has many-to-many relationship which maps
users to courses:
PROFESSOR_ID  CID          CREATION_DATE
1             1             2004-09-26 17:45:47

To map a many-to-many association in Hibernate, I used the trick of
having 2 one-to-many associations in User.hbm.xml and Course.hbm.xml
as well as 2 many-to-one associations in ProfessorTeachs.hbm.xml.
=================================================
In Course.hbm.xml
.....
<set    name="professorTeaches_list" table="PROFESSOR_TEACHES"
        cascade="all-delete-orphan"
        inverse="true" outer-join="false">
<key>
<column name="CID" not-null="true"/>
</key>
   <one-to-many class="elearning.mapping.ProfessorTeaches"/>
</set>
.........
=================================================
In User.hbm.xml
.........
<set name="professorTeaches_list" table="PROFESSOR_TEACHES"
inverse="true"
       cascade="all-delete-orphan" outer-join="false">
<key>
<column name="PROFESSOR_ID" not-null="true"/>
</key>
<one-to-many class="elearning.mapping.ProfessorTeaches"/>
</set>
.........
==================================================
In ProfessorTeaches.hbm.xml
.........
<many-to-one  name="professor" insert="false" update="false"
not-null="true" class="elearning.mapping.User" column="PROFESSOR_ID"/>

<many-to-one name="course" insert="false" update="false"
not-null="true" class="elearning.mapping.Course" column="CID"/>
........
===================================================
In my login.jsp, I want to update the LAST_LOGIN column in DB every
time a user log into the webpage.
U.setLast_login(DateUtil.getCurrentDate());
U.modifyInDB();
and my modifyInDB() function in User.java actually  only calls
session.update function as following:
public void modifyInDB() throws Exception{
    Session s = HibernateUtil.currentSession();
    Transaction tx = null;
    try{
     tx= s.beginTransaction();
     s.update(this);
     tx.commit();
    }catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
    }
    finally {
     HibernateUtil.closeSession();
    }
 }//end of modifyInDB function
====================================================
I got the following error message right after a user tries to log into
the webpage
Hi,
I have problem with Hibernate's session.update() and it buzzed me for
many hours already. I don't understand why and I don't know how to
solve it. I really need help.
My COURSES table has only 1 record:
COURSE_ID  COURSE_NAME     COURSE_NUMBER          DESCRIPTION
1          chicken            chick            how to cook chicken
My USERS table has only 1 user:
USER_ID LOGIN   PASSWORD   FIRST_NAME LAST_NAME  LAST_LOGIN
1      teacher  ------       teacher   teacher   2004-09-26 18:31:37

My PROFESSOR_TEACHES table has many-to-many relationship which maps
users to courses:
PROFESSOR_ID  CID          CREATION_DATE
1             1             2004-09-26 17:45:47

To map a many-to-many association in Hibernate, I used the trick of
having 2 one-to-many associations in User.hbm.xml and Course.hbm.xml
as well as 2 many-to-one associations in ProfessorTeachs.hbm.xml.
=================================================
In Course.hbm.xml
.....
<set    name="professorTeaches_list" table="PROFESSOR_TEACHES"
        cascade="all-delete-orphan"
        inverse="true" outer-join="false">
<key>
<column name="CID" not-null="true"/>
</key>
   <one-to-many class="elearning.mapping.ProfessorTeaches"/>
</set>
.........
=================================================
In User.hbm.xml
.........
<set name="professorTeaches_list" table="PROFESSOR_TEACHES"
inverse="true"
       cascade="all-delete-orphan" outer-join="false">
<key>
<column name="PROFESSOR_ID" not-null="true"/>
</key>
<one-to-many class="elearning.mapping.ProfessorTeaches"/>
</set>
.........
==================================================
In ProfessorTeaches.hbm.xml
.........
<many-to-one  name="professor" insert="false" update="false"
not-null="true" class="elearning.mapping.User" column="PROFESSOR_ID"/>

<many-to-one name="course" insert="false" update="false"
not-null="true" class="elearning.mapping.Course" column="CID"/>
........
===================================================
In my login.jsp, I want to update the LAST_LOGIN column in DB every
time a user log into the webpage.
U.setLast_login(DateUtil.getCurrentDate());
U.modifyInDB();
and my modifyInDB() function in User.java actually  only calls
session.update function as following:
public void modifyInDB() throws Exception{
    Session s = HibernateUtil.currentSession();
    Transaction tx = null;
    try{
     tx= s.beginTransaction();
     s.update(this);
     tx.commit();
    }catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
    }
    finally {
     HibernateUtil.closeSession();
    }
 }//end of modifyInDB function
====================================================
I got the following error message right after a user tries to log into
a webpage
12:28:12,985 DEBUG SQL:223 - insert into PROFESSOR_TEACHES
(CREATION_DATE, PROFESSOR_ID, CID) values (?, ?, ?)
12:28:12,994  WARN JDBCExceptionReporter:38 - SQL Error: 1062,
SQLState: S1009
12:28:12,995 ERROR JDBCExceptionReporter:46 - Invalid argument value:
Duplicate entry '1-1' for key 1
12:28:12,999 ERROR JDBCExceptionReporter:38 - could not insert:
[elearning.mapping.ProfessorTeaches#elearning.mapping.ProfessorTeaches$Id@2]
java.sql.SQLException: Invalid argument value: Duplicate entry '1-1'
for key 1
====================================================
It seems to me that session.update() function calls INSERT on the
PROFESSOR_TEACHES table again with the same record that is available
in DB already. I don't understand why update has to call insert. I did
have {inverse="true"} in both Course.hbm.xml and User.hbm.xml and
{"insert="false" update="false"} in ProfessorTeaches.hbm.xml.
Can someone explain about this to me? I am completely lost. I would
appricate alot. It seems like I am missing some concept in Hibernate.
Thanks in advance,
Tung Chau
Tung Chau - 28 Sep 2004 16:50 GMT
Hi,
Please ignore any thread with the above title. I solved the problem
myself.
The reason was because I had {unsaved-value="any"} in the
"composite-id" in my
ProfessorTeaches.hbm.xml. Hope this would help some one who runs into
the same problem.
=========================================
<class name="elearning.mapping.ProfessorTeaches"
      table="PROFESSOR_TEACHES"
      lazy="true">
<composite-id name="id" class="elearning.mapping.ProfessorTeaches$Id"
         unsaved-value="any">

<key-property name="professor_id" type="java.lang.Integer">
<column name="PROFESSOR_ID" sql-type="integer" not-null="true"/>
</key-property>

<key-property name="course_id" type="java.lang.Integer">
<column name="CID" sql-type="integer" not-null="true"/>
</key-property>

</composite-id>
============================================
> Hi,
> I have problem with Hibernate's session.update() and it buzzed me for
[quoted text clipped - 170 lines]
> Thanks in advance,
> Tung Chau


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.