-->
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.  [ 15 posts ] 
Author Message
 Post subject: manual saveOrUpdate, emergent help needed
PostPosted: Sun Mar 27, 2005 9:17 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
I need to implement manual saveOrUpdate method,

(Object's id's are semantic keys that hibernate generates, but all they have a business key)

pseudocode is

saveOrUpdate(Object o) {
find according to business key
if exists assign o to the object in the database and update object in the database
else save
}

but for many-to-one relations, I got "Object references to unsaved object error", which is true.

Such as

saveOrUpdateA(A a) {
// Go, saveOrUpdate the referenced object
// Try to do what <many-to-one name= "b" class=... cascade= "save- // update" in mapping file does.
saveOrUpdateB(a.getB())

findA
if exists, assign a to the A object in the database, update object in the database
else save a

}

saveOrUpdateB(B b) {
findB according to the business key
if exists, assign b to the B object in the database, update object in the database
else save b
}

As you can see, the referenced object b remains unsaved if the copy of b according to the business key in the database exists.


I need to implement what "<many-to-one name= "b" class=... cascade= "save-update" " does manually.

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 9:21 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
The exact problem is I set objects id's as semantic keys such as hibernate_next_val
But I want to do saveOrUpdates not according to that semantic key, according to the business key that objects have.

If you would ask why you didnt set the "id" as the business keys, I can say that I had to do because of many reasons.

So, what would u offer me to do for such a saveOrUpdate scenario

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 9:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Interceptor.isUnsaved() ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 10:26 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Thank you max, for reply,
This can help me to catch the error but exactly I dont need that

If I try to solve the problem with mapping I need something like that

<many-to-one name="b" class="B" cascade="save-update" "SOMETHING SHOULD BE HERE TO IDENTIFY SAVE-OR-UPDATE COLUMN" ... />

I need cascade save-update to work like update if the b exists but check the existence of b not according to the PK (Long id) value of b,
check it by the property b.name.

My exact problem is I saved objects by semantic id's but I want to check the existence of them in many-to-one mappings by their business keys.

Does "property-ref" property in many-to-one solve that?

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 10:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I really have no clue on what you want.

for me it sounds like you want two different ways for an object to be "unsaved" and that sounds really really bad.

and i have no clue why you need to write your own saveorupdate code...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:11 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Yes you are right,

I had to save the object with assigning a semantic ID, and I want to make save-update work on the business key

class A
Long ID // semantic key(hibernate_nextval)
String name // business key (unique)
B b;

class B
Long ID // semantic key (hibernate_nextval)
String loginName // business key (unique)
String protocolName// business key (unique)

many-to-one name="b" class="B" cascade="save-update"

A a = new A();
a.setName("qw21DE");
B b = new B();
b.setLoginName("zuzu");
a.setB(b);
save(A);

// New A inserted with ID 1, and new B inserted with ID 2


A a = new A();
a.setName("dsaasdr21");
B b = new B(); // (!!!)
b.setLoginName("zuzu");
a.setB(b);
save(A);

// New A inserted with ID 3, and "NEW" B inserted with ID 4

what I want not to insert new B , I want many-to-one name="b" class="B" cascade="save-update" works as , go find B according to the property I gave (here the b.name) not the primary key, if found call update.

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
okeey - but why dont you just map with the biz key ?

(property-ref might be usefull for you here - but you cant have one strategy for some saves and another for the rest...for that you should map it twice)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:25 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Thank for your reply, max.


"but you cant have one strategy for some saves and another for the rest...for that you should map it twice"

what did u mean by that?

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you showed two idential pieces of code (except for some string values)

That stuff would need to be saved/loaded with the same strategy. What else would you want hibernate to do ?

and why dont you look up the B before assigning it to A ?

-max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and yes property-ref does what you want - but again, you can only have many-to-one that EITHER uses the primary id OR the unique biz key.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:46 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
max wrote:
you showed two idential pieces of code (except for some string values)

That stuff would need to be saved/loaded with the same strategy. What else would you want hibernate to do ?

and why dont you look up the B before assigning it to A ?

-max


You are completely right. I dont look up the B before assigning it to A, because at that point I am not sure that this data will be persisted,
I dont want to wait "for querying b". Exactly, I have very large data, and I dont want hibernate to be involved in process until the user calls "save".

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 11:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I dont think i can help ,)

you keep saying i'm completely right, but still wants to do the impossible of having hibernate magically guess when you want to use one or the other save strategy.

Have you *tried* using property-ref ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 12:31 pm 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Yes I tried, but it completely causes a mess-))

I think I made the wrong decision of not choosing mapping objects with their biz as id's.

do u agree?

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 27, 2005 12:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no you did the right thing about using the synthetic ids.

why dont you perform a lookup to get a persisted version of B ?

The lookup can be done in a cache (just enable second-level cache for it)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 7:09 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Thanks for your reply, max.

I will think it.

_________________
-developer


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