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