-->
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: Very simple CRUD operation questions
PostPosted: Thu Oct 04, 2007 12:11 pm 
Newbie

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 15
I'm at the point where I have a webservice up and running that allows me to save a single object to the database, via an EntityManager. After that, I'm having some problems.

1) em.contains() does not seem to work. I have a web service "insertOrUpdate" which does the following:

Code:
            if(em.contains(server)) {
                utx.begin();           
                em.merge(server);
                utx.commit();
            } else {
                utx.begin();           
                em.persist(server);
                utx.commit();
            }


When I run it the first time, em.contains() returns false and it does a persist. When I run it the second time, using the same object, em.contains() still returns false and the persist fails because the entity is already in the DB.

2) em.merge() does not delete items in a collection (inside the entity) that are no longer there, it simply adds any additional ones it finds. Is there an easy way to do this or do I have to remove an entity and persist it again?

3) Why does em.remove() require an entity as an argument. Isn't it enough to just have the entity's primary key and be done with it? Or do I have to do something like get the entity via em.find() then use the entity it returns and do a em.remove() on that entity.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 5:16 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
Quote:
When I run it the second time, using the same object, em.contains() still returns false and the persist fails because the entity is already in the DB.

Try to use a HQL query instead, because this will try to lookup the entity in the database instead of only in the cache of entitymanager.

Quote:
2) em.merge() does not delete items in a collection (inside the entity) that are no longer there, it simply adds any additional ones it finds. Is there an easy way to do this or do I have to remove an entity and persist it again?

Well this is exactly the problem i'm having. I suppose you have set cascade to DELETE. Deleting items in the collection does not invoke any cascade delete. For this you will have to delete the owning entity (server in your case), like you say. But there must be an easier way. Currently I'm using a for loop to delete every item manually. Have you come up with a solution yet?

Quote:
3) Why does em.remove() require an entity as an argument. Isn't it enough to just have the entity's primary key and be done with it?


Well. You can of course use a delete HQL statement, like Delete FROM server where id =:id


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 9:00 pm 
Newbie

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 15
I'm actually working on another project, as this one has been put on hold. :(

I was able to deal with deleting collections by making the objects "Embeddable" and the collection as a "CollectionOfElements". That way, not only did I not have to give the objects a unique ID, but they were automatically deleted when the parent object was removed, and they were automatically deleted/inserted when the parent object was merged.

The only drawback was that you could only do this down 1 level. You can't have an embeddable object that contains other embeddable objects...only simple types. At least, I couldn't get it to work when I tried it.

Thanks for the HQL tips. I never delved into that aspect of Hibernate but, next time I actually get a project that requires persistance, I'll be sure to check it out.


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.