-->
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: First inserts and then Updates
PostPosted: Sat May 07, 2011 2:29 am 
Newbie

Joined: Sat May 07, 2011 2:16 am
Posts: 3
Hello All,

I have a simple java page in which I am trying to insert data into the DB. Following is the code for it -->

Code:
public void execute() throws JobExecutionException {
      TaskUser tu= new TaskUser();
      Session session = null;
      try{   
      
      session = HibernateUtil.getSessionFactory().openSession();
      org.hibernate.Transaction tx = session.beginTransaction();
      java.util.List taskList = session.createQuery("from Task as t where t.frequency = 'W'").list();
      Iterator i = taskList.iterator();
      int len=taskList.size();
      while(i.hasNext())
       {
         Task task=(Task)i.next();

         System.out.println(task.getProcessOwner());
         tu.setUserId(task.getProcessOwner());

         System.out.println(task.getTaskId());
         tu.setTaskId(task.getTaskId());

         System.out.println(task.getDueDate());
         tu.setDueDate(task.getDueDate());

         System.out.println(task.getDurationDays());
         tu.setDurationDays(task.getDurationDays());

         System.out.println(task.getReportTO());
         tu.setReportingTo(task.getReportTO());
         
         tu.setScanned("Y");
         
         tu.setLevels(3);
         
         tu.setStatus(3);
         
         session.save(tu);
         
      }      
      tx.commit();
         



When I run the above code the output obtained is -->
Hibernate: select task0_.Task_Id as Task1_, task0_.Task_Number as Task2_8_, task0_.Task_Desc as Task3_8_, task0_.Start_Date as Start4_8_, task0_.End_Date as End5_8_, task0_.Frequency as Frequency8_, task0_.Duration_Days as Duration7_8_, task0_.Due_Date as Due8_8_, task0_.Next_Start_Date as Next9_8_, task0_.Next_Due_Date as Next10_8_, task0_.Category as Category8_, task0_.Project as Project8_, task0_.Process_Owner as Process13_8_, task0_.Reporting_To as Reporting14_8_, task0_.Location as Location8_ from Task_Mast task0_ where (task0_.Frequency='W')
11
1
2011-05-25 00:00:00.0
8
20

11
2
2011-05-25 00:00:00.0
8
20


11
3
2011-05-25 00:00:00.0
8
20


11
4
2011-05-25 00:00:00.0
8
20

Hibernate: insert into Task_User_Rel (Usr_Id, Task_Id, Due_Date, Duration_Days, Status, Scanned, Path, Levels, Reporting_To, Task_User_Id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: update Task_User_Rel set Usr_Id=?, Task_Id=?, Due_Date=?, Duration_Days=?, Status=?, Scanned=?, Path=?, Levels=?, Reporting_To=? where Task_User_Id=?


My select statement is returning four rows which I want to insert into another table. The issue is while inserting for the first time there is an insert made but after that the next set of data is getting updated and not inserted as a new record. What can be the possible reason?
Note :- ProcessOwner and TaskId are primary keys in my master table.

Thanks in advance.
Sweta


Top
 Profile  
 
 Post subject: Re: First inserts and then Updates
PostPosted: Sat May 07, 2011 12:05 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You are creating a new TaskUser at the beginning of the code. Then you are using the same instance in the loop, so Hibernate after the first insert Hibernate only sees that you have updated the existing object and do update instead. To insert four records you need to create a new TaskUser object for each iteration in the loop.


Top
 Profile  
 
 Post subject: Re: First inserts and then Updates
PostPosted: Sun May 08, 2011 11:42 pm 
Newbie

Joined: Sat May 07, 2011 2:16 am
Posts: 3
Thankyou for your reply. I made the required changes and it is working now.

Code:

try {         
         session = HibernateUtil.getSessionFactory().openSession();
         org.hibernate.Transaction tx = session.beginTransaction();
         java.util.List taskList = session.createQuery(
               "from Task as t where t.frequency = 'W'").list();
         Iterator i = taskList.iterator();
         //populate data into Task_User_Rel table.
         while (i.hasNext()) {
            Task task = (Task) i.next();


            TaskUser tu = new TaskUser();   // was creating this object outside the while loop so hibernate was first inserting the data using  //the tu object and as no new object was created hence it was updating it. I moved the object creation statement inside the while loop and the issue was //fixed.


            tu.setUserId(task.getProcessOwner());
            tu.setTaskId(task.getTaskId());
            tu.setDueDate(task.getDueDate());
            tu.setDurationDays(task.getDurationDays());
            tu.setReportingTo(task.getReportTO());
            tu.setScanned("Y");
            tu.setLevels(3);
            tu.setStatus(3);
            session.save(tu);
            session.flush();



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.