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 ref of element in list is important while updating list?
PostPosted: Fri Dec 16, 2005 2:01 pm 
Newbie

Joined: Tue Jun 21, 2005 1:03 am
Posts: 5
We have following Models

public class FruitCollection {
Integer id;
Integer version;
List<Fruit> fruits;
}

abstract class Fruit {
Integer id;
Integer version;
}

class Apple extends Fruit {
Integer vitamin;
}

class Orange extends Fruit {
String taste;
}

class Mango extends Fruit {
boolean juice;
}

All the three fruit classes have unique properties (thats why we have three subclasses). There is one-many relation between FruitCollection and Fruit. In addition, we have also set the discriminitor value for each subclass as we are using table-per-hirerachy approach.

As you can see from the below code snippet...

FruitCollection collection = new FruitCollection();

Apple apple1 = new Apple();
Orange orange1 = new Orange();

collection.getFruits().add(apple1);
collection.getFruits().add(orange1);

hibernateSession.save(collection);


//open a new session
FruitCollection collection1 = (FruitCollection) hibernateSession.load(FruitCollection.class, 10L);
Integer appleId = collection1.getFruits().get(0).getId();
Integer orangeId = collection1.getFruits().get(1).getId();

collection1.getFruits().clear();

Mango mango1 = new Mango();
mango1.setId(appleId);

Mango mango2 = new Mango();
mango2.setId(orangeId);

collection1.getFruits().add(mango1);
collection1.getFruits().add(mango2);

hibernateSession.update(collection1);


...we expected the new object to be persisted in the DB however with the same ID that we have set explicitely. This does not happen. The old objects are removed and new objects get created with new Id's.

We would like preserve Id for instance of Fruit on every update on FruitCollection. Is it anyways possible to have different objects of objects however with the same id in the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 3:18 pm 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
What is the id generation logic in you rmapping file?

Not clear why you would want to change the id of an instance? But if you wanted to do that, you should probably used "assigned" ids and set them specifically in your code?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.