I really don't understand why this is happening. I must have missed some fundamental concept of Hibernate, but I just don;t see it.
Here's the code:
Code:
@Entity
public class Car {
long id;
private String make;
private String color;
private List<Driver> drivers = new ArrayList<Driver>();
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@OneToMany
public List<Driver> getDrivers() {
return drivers;
}
public void setDrivers(List<Driver> drivers) {
this.drivers = drivers;
}
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
@Entity
public class Driver {
long id;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
If I have already inserted a car and driver then run this:
Code:
HibernateUtil.begineTransaction();
Car car = (Car)HibernateUtil.getSession().load(Car.class, new Long(1));
Driver driver = new Driver();
HibernateUtil.getSession().saveOrUpdate(driver);
driver.setName("Bob");
car.getDrivers().add(driver);
HibernateUtil.commitTransaction();
My log says:
INFO - Version - Hibernate Annotations 3.3.0.GA
INFO - Environment - Hibernate 3.2.4.sp1
Hibernate: insert into Driver (car_id, name) values (?, ?)
DEBUG - LongType - binding null to parameter: 1
DEBUG - StringType - binding null to parameter: 2
Hibernate: select car0_.id as id7_0_, car0_.color as color7_0_, car0_.make as make7_0_ from Car car0_ where car0_.id=?
DEBUG - LongType - binding '1' to parameter: 1
DEBUG - StringType - returning 'blue' as column: color7_0_
DEBUG - StringType - returning 'audi' as column: make7_0_
Hibernate: select drivers0_.Car_id as Car1_2_, drivers0_.drivers_id as drivers2_2_, driver1_.id as id8_0_, driver1_.car_id as car3_8_0_, driver1_.name as name8_0_, car2_.id as id7_1_, car2_.color as color7_1_, car2_.make as make7_1_ from Car_Driver drivers0_ left outer join Driver driver1_ on drivers0_.drivers_id=driver1_.id left outer join Car car2_ on driver1_.car_id=car2_.id where drivers0_.Car_id=?
DEBUG - LongType - binding '1' to parameter: 1
DEBUG - LongType - returning '1' as column: id8_0_
DEBUG - LongType - returning null as column: id7_1_
DEBUG - LongType - returning null as column: car3_8_0_
DEBUG - StringType - returning 'JILL' as column: name8_0_
DEBUG - LongType - returning '1' as column: Car1_2_
DEBUG - LongType - returning '1' as column: drivers2_2_
Hibernate: update Driver set car_id=?, name=? where id=?
DEBUG - LongType - binding '1' to parameter: 1
DEBUG - StringType - binding 'Bob' to parameter: 2
DEBUG - LongType - binding '2' to parameter: 3
Hibernate:
delete from Car_Driver where Car_id=?
DEBUG - LongType - binding '1' to parameter: 1
Hibernate:
insert into Car_Driver (Car_id, drivers_id) values (?, ?)
DEBUG - LongType - binding '1' to parameter: 1
DEBUG - LongType - binding '1' to parameter: 2
Hibernate:
insert into Car_Driver (Car_id, drivers_id) values (?, ?)
DEBUG - LongType - binding '1' to parameter: 1
DEBUG - LongType - binding '2' to parameter: 2
You can see that its deleting the join table and inserting all the elements again.
Please anyone can tell me what I'm doing wrong?
thanks[/b]