-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why save automatically?
PostPosted: Mon Mar 07, 2005 4:27 pm 
Beginner
Beginner

Joined: Thu Jul 29, 2004 7:14 pm
Posts: 41
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.17

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Strange problem. My DAO layer is using Hibernate. I retrieve a POJO "Task" from the DAO. It has a Set which contains POJOs of type "TaskJob" (Task is parent of TaskJob).
I define a new TaskJob, set its parent to the Task I have retrieved; and then add it in the Set of the Task I have retrieved. Now I do not call the save method of my DAO and just commit the transaction. I expect Hibernate not to save it as I should call the save method of my DAO explicitly. What happening is Hibernate is saving it.

Can someboddy please explain? Thanks


This is my script :

String taskName = "Task A";

try {
HibernateUtil.getSession();
HibernateUtil.beginTransaction();

// creates a SchedulerDAOFactory
SchDAOFactory aSchDAOFactory = SchDatastoreSelector.getSchDAOFactory();

// creates a TaskDAO
TaskDAO aTaskDAO = aSchDAOFactory.getTaskDAO();

// retrieve the Task whose Name = ? and display it
Task aTask = aTaskDAO.getTaskByTaskName( taskName );

Set aTask_TaskJobs = aTask.getTaskJobs();

// define a new TaskJob for this Task
TaskJob aTaskJob = new TaskJob();
aTaskJob.setJob( new Integer(1) );
aTaskJob.setTask( aTask );

// add this new TaskJob to this Task
aTask_TaskJobs.add( aTaskJob );

HibernateUtil.commitTransaction();
}
catch (InfrastructureException iex) {
System.out.println( "index.jsp: iex.getMessage() = " + iex.getMessage() );
HibernateUtil.rollbackTransaction();
}
finally {
HibernateUtil.closeSession();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 4:37 pm 
Beginner
Beginner

Joined: Thu Jul 29, 2004 7:14 pm
Posts: 41
The funny thing is I am not even setting the Set "aTask_TaskJobs" back in the Task object after adding a TaskJob object to it. Still Hibernate saves it. This is quite dangerous. I must be doing something wrong. Any input?


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