-->
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: Hibernate Bug
PostPosted: Tue Nov 25, 2003 6:16 am 
I've read this postings: http://forum.hibernate.org/viewtopic.php?t=549
and i think this IS a bug. To be sure i made a small sample with 3 classes: Person, Vehicle and Car

A Test class shows the behavior. you can switch between the "working" and the "not working" saveOrUpdate with the static boolean inverse = true|false

here are the classes, ma


package test;
import java.util.Collection;
/**
*
* @hibernate.class table = "person"
*
*/
public class Person {
private String personId;
private String name;
private Collection cars;
/**
* @return
*
* @hibernate.bag cascade = "all" inverse = "true"
* @hibernate.collection-one-to-many class = "test.Car"
* @hibernate.collection-key column = "id_person"
*
*/
public Collection getCars() {
return cars;
}

/**
* @return
*
* @hibernate.property column = "pname"
*/
public String getName() {
return name;
}

/**
* @return
*
* @hibernate.id column = "id_person" generator-class = "uuid.hex"
*/
public String getPersonId() {
return personId;
}

/**
* @param collection
*/
public void setCars(Collection collection) {
cars = collection;
}

/**
* @param string
*/
public void setName(String string) {
name = string;
}

/**
* @param string
*/
public void setPersonId(String string) {
personId = string;
}
}



package test;
/**
*
* @hibernate.class table = "vehicle"
*/
public class Vehicle {
private String vehicleId;
private String serialNumber;
private Person owner;

/**
* @return
*
* @hibernate.property column = "sernum"
*
*/
public String getSerialNumber() {
return serialNumber;
}

/**
* @return
*
* @hibernate.id column = "id_vehicle" generator-class = "uuid.hex"
*/
public String getVehicleId() {
return vehicleId;
}

/**
* @param string
*/
public void setSerialNumber(String string) {
serialNumber = string;
}

/**
* @param string
*/
public void setVehicleId(String string) {
vehicleId = string;
}

/**
* @return
*
* @hibernate.many-to-one column = "id_person" cascade = "all"
*/
public Person getOwner() {
return owner;
}

/**
* @param person
*/
public void setOwner(Person person) {
owner = person;
}
}




package test;
/**
*
* @hibernate.joined-subclass table="cars"
* @hibernate.joined-subclass-key column = "id_vehicle"
*
*/
public class Car extends Vehicle {
private String licensePlate;
/**
* @return
* @hibernate.property column = "licplate"
*/
public String getLicensePlate() {
return licensePlate;
}

/**
* @param string
*/
public void setLicensePlate(String string) {
licensePlate = string;
}
}




//////////////////////////////
// Person.hbm.xml
//////////////////////////////


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="test.Person"
table="person"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="personId"
column="id_person"
type="java.lang.String"
>
<generator class="uuid.hex">
</generator>
</id>

<bag
name="cars"
lazy="false"
inverse="true"
cascade="all"
>

<key
column="id_person"
/>

<one-to-many
class="test.Car"
/>
</bag>

<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="pname"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Person.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

//////////////////////////////
// Vehicle.hbm.xml
//////////////////////////////

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="test.Vehicle"
table="vehicle"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="vehicleId"
column="id_vehicle"
type="java.lang.String"
>
<generator class="uuid.hex">
</generator>
</id>

<property
name="serialNumber"
type="java.lang.String"
update="true"
insert="true"
column="sernum"
/>

<many-to-one
name="owner"
class="test.Person"
cascade="all"
outer-join="auto"
update="true"
insert="true"
column="id_person"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Vehicle.xml
containing the additional properties and place it in your merge dir.
-->

<joined-subclass
name="test.Car"
table="cars"
dynamic-update="false"
dynamic-insert="false"
>
<key
column="id_vehicle"
/>
<property
name="licensePlate"
type="java.lang.String"
update="true"
insert="true"
column="licplate"
/>

</joined-subclass>

</class>

</hibernate-mapping>




-- **********
-- Person
-- **********
DROP TABLE IF EXISTS person
;
CREATE TABLE person
(
id_person VARCHAR (32) NOT NULL PRIMARY KEY
, pname VARCHAR (255)
)
;

-- **********
-- Vehicle
-- **********
DROP TABLE IF EXISTS vehicle
;
CREATE TABLE vehicle
(
id_vehicle VARCHAR (32) NOT NULL PRIMARY KEY
, id_person VARCHAR (32)
, sernum VARCHAR (255)
)
;

-- **********
-- Car
-- **********
DROP TABLE IF EXISTS cars
;
CREATE TABLE cars
(
id_vehicle VARCHAR (32) NOT NULL PRIMARY KEY
, licplate VARCHAR (255)
)
;


Top
  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 6:20 am 
.... and here is the Test class:


package test;

import java.util.Vector;


import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class Test {

public static Configuration cfg;
static SessionFactory sessions;

public static boolean inverse = true;

public static void main(String[] args) {
try {
cfg = new Configuration().configure();
sessions = cfg.buildSessionFactory();
Session sess = null;
Transaction tx = null;

sess = sessions.openSession();
tx = sess.beginTransaction();

Person pers = new Person();
pers.setName("test");

Car car = new Car();
car.setLicensePlate("AB-1234");
car.setSerialNumber("987654321");

if (inverse) {
car.setOwner(pers);
sess.saveOrUpdate(car);
} else {
Vector cars = new Vector();
cars.add(car);
pers.setCars(cars);
sess.saveOrUpdate(pers);
}

tx.commit();

} catch (HibernateException e) {
e.printStackTrace();
}
}
}


Top
  
 
 Post subject: safeOrUpdate problem
PostPosted: Tue Nov 25, 2003 6:58 am 
after some testing i found out that i have to saveOrUpdate the collection elements first! after that it works as it should (the FK is set right)

is this just a safeOrUpdate bug?
or did i miss something?


Top
  
 
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.