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.  [ 1 post ] 
Author Message
 Post subject: How to insert only new objects in hierarchy
PostPosted: Tue Nov 18, 2008 5:16 am 
Newbie

Joined: Tue Nov 18, 2008 4:50 am
Posts: 3
Location: Ahmedabad
Hi,

I have two classes one is Person and another one is car.

public class Person {
protected int oid;//key column
protected String name;
protected Car car;

public Person() {
}

public int getOid() {
return oid;
}

public void setOid(int oid) {
this.oid = oid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}

public void setCar(Car car) {
this.car = car;
}
}//Person class end

public class Car{
protected int oid;//key column
protected String carName;

public Car(){
}

public int getOid() {
return oid;
}

public void setOid(int oid) {
this.oid = oid;
}

public String getCarName() {
return carName;
}

public void setCarName(String carName) {
this.carName = carName;
}
}//Car class end

Now i am creating one new Person object which is referencing already persisted Car objects.[This Car object is not avilabel in current session, but it is there in database.]
Person person = new Person();
person.setName("Person2");

//This car object is already present in Database with oid 1.
Car car = new Car();
car.setOid(1);
car.setCarName("car1");
person.setCar(car);

Transaction tx = session.beginTransaction(); // Start Transaction
session.save(person);
session.flush();
tx.commit();

in this case it is creating one new insert for Person and one update for Car object.

Here i want to create one insert only for person. Here i don't want any update for car as i know this is already persisted.

So how can i achieve this in hibernate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.