-->
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.  [ 10 posts ] 
Author Message
 Post subject: attaching transient object with parent and dtos
PostPosted: Sun Aug 06, 2006 11:00 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I have a two objects ReleaseDBO and ProjectDBO (DBO=DatabaseObject) and I have a DTO ReleaseDTO that contains the release id and project id.

I am trying to implement a method that will save new ones or existing ones like saveOrUpdate().

My first try was to create a detached instance and merge it like so....

Code:
ReleaseDBO rel = Converter.convert(releaseDto);
           
ProjectDBO project = session.load(ProjectDBO.class, releaseDto.getProjectId());
           
project.addRelease(rel);

session.merge(rel);


Naturally, this complains about persisting a detached instance because of project.addRelease(rel);

I also cannot do the following code because a release has to have a project(not-null constraint) and so this fails during commit

Code:
ReleaseDBO rel = Converter.convert(releaseDto);

session.update(rel);
           
ProjectDBO project = session.load(ProjectDBO.class, releaseDto.getProjectId());


project.addRelease(rel);


Next, I thought of loading first but this is really bad because now my version is not used and compared for validity......
Code:
           ReleaseDBO rel = Db.get().get(ReleaseDBO.class, releaseDto.getId());
           if(rel == null)
              rel = new ReleaseDBO();
            Converter.convert(releaseDto, rel);
           
            ProjectDBO project = Db.get().load(ProjectDBO.class, releaseDto.getProjectId());
            project.addRelease(rel);


So to go over what I am trying to do again....
1. I would like to save new Releases
2. I would like to save changes to old Releases
3. I have to add a release to a project since every release must have an instance of a project(this is what causes most of my problems)

The only solution I can get so far is the last one above BUT with the addition of my own version checking which seems kind of ugly. Is there a better way to do this?
thanks,
dean

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 12:11 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
anyone know the answer?????

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 2:10 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Why do you have a DTO and a DBO?

Can't you have just one of them, with a private default constructor (which can be used by hibernate), and a public argumented constructor for your use?

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 2:11 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Why do you have a DTO and a DBO?

Can't you have just one of them, with a private default constructor (which can be used by hibernate), and a public argumented constructor for your use?
-----------------------

DON'T FORGET THE CREDITS

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 10:17 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
No, I can't. The product is using Jaxme which generates the DTO's and the DBO's represent the domain better than the DTO's do.

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject: does no one have a cleaner solution. please help!
PostPosted: Tue Aug 15, 2006 8:24 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
does no one have a cleaner solution than mine????? pleeeaaase help!!!

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 24, 2006 7:36 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
anyone???? would love to see a better solution than this?

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 12, 2006 9:33 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
anyone? a reply with this is impossible would be nice as well.

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject: is this a bug(very advanced...talk about hibernate code here
PostPosted: Mon Nov 13, 2006 2:36 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I think this may be a bug as I believe the data that is in the cache should be that of my projDbo bean instead of the data from the database. Either that or I have to program some real ugly code here to compare versions and stuff (or write a save method and an update method separately).

private ProjectDto updateOrSave(ProjectDto projectDto)
{
ProjectDBO projDbo = convert(projectDto);

Session session = openSession();
try {
session.getTransaction().begin();

//session.saveOrUpdate(projDbo);
FolderDBO folder = (FolderDBO)session.load(FolderDBO.class, projectDto.getFolderId());

//this method causes the folder and projDBO from database to load into cache. The projDBO
//in the cache has the values of the database not the projDbo values from projDbo
//this method causes the folder and projectDbo from database to load into cache. The projDBO
//in the cache has the values of the database the values of the projDbo variable being passed in here.....
//I am referring to session's StatefulPersistenceContext which has a map of entitiesByKey and
//after this method is called, 2 values exist...one FolderDBO and one ProjectDBO
folder.addChildProject(projDbo);

//session.merge(projDbo);
session.saveOrUpdate(projDbo);

session.getTransaction().commit();
} finally {
session.close();
}

return convert(projDbo);
}

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 3:32 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
NEVER MIND, YEAH!!!! got it. I had a method trying to follow the hibernate pattern for updating both ends of the association.....

Code:
   public void addChildProject(ProjectDBO childProject) {
      if(childProject == null)
         throw new IllegalArgumentException("requirement may not be null and was");
      else if(getChildProjects().contains(childProject))
         return; //we only allow it to be added once
      else if(childProject.getParent() != null) {
         //make sure we reattach the release we are removing to the session...
         childProject.getParent().removeChildProject(childProject);
      }
      
      childProject.setParent(this);
      getChildProjects().add(childProject);
   }


Looks fine and all, but getChildProjects().contains(childProject) was loading the childProject from the database. Once I get rid of that if statement, everything works great. Thanks to a debugger and just stepping through the hibernate code!!!
sweet,
dean

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


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