Here is a sample of one-to-one with Person and Car
I hope it helps!
Car class mapping
Code:
<hibernate-mapping>
<class
name="samples_0.Car"
table="SAMPLE0_CAR"
>
<id
name="CAR_ID"
column="CAR_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="hilo">
<param name="table">SAMPLE0_CAR_SEQ</param>
<param name="column">NEXT</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Person.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="CAR_NAME"
update="true"
insert="true"
not-null="false"
unique="false"
type="defaultString"
>
<column name="CAR_NAME" default="n/a"/>
</property>
<many-to-one
name="person"
class="samples_0.Person"
cascade="all"
column="PERSON_ID"
unique="true"
/>
</class>
</hibernate-mapping>
Car java code:
Code:
public class Car
{
Person person=null;
Long CAR_ID=null;
String CAR_NAME="";
...with getters and setters foreach attribute defined.
}
Person Class mapping.
Code:
<hibernate-mapping
>
<typedef class="java.lang.String" name="defaultString">
<param name="default">n/a</param>
</typedef>
<class
name="samples_0.Person"
table="SAMPLE0_Person"
>
<id
name="PERSON_ID"
column="PERSON_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="hilo">
<param name="table">SAMPLE0_PERSON_SEQ</param>
<param name="column">NEXT</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Person.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="FIRST_NAME"
update="true"
insert="true"
not-null="false"
unique="false"
type="defaultString"
>
<column name="FIRST_NAME" default="n/a"/>
</property>
<property
name="LAST_NAME"
type="java.lang.String"
update="true"
insert="true"
column="LAST_NAME"
not-null="true"
unique="false"
/>
<sql-delete callable="true">{? = call deletePerson (?)}</sql-delete>
</class>
</hibernate-mapping>
Person java code:
Code:
public class Person
{
Long PERSON_ID=null;
String FIRST_NAME=null;
String LAST_NAME=null;
...with getters and setters foreach attribute defined.
}
Calling Java code:
Code:
Person p1 = new Person();
p1.setFIRST_NAME("JEFF");
p1.setLAST_NAME("JOHnson");
Car c=new Car();
c.setCAR_NAME("BMW");
c.setPerson(p1);
sess.saveOrUpdate(c);
tx.commit();
System.out.println("c.getPerson().getPERSON_ID() = " + c.getPerson().getPERSON_ID());
System.out.println("p1.getPERSON_ID() = " + p1.getPERSON_ID());
SQL code:
Code:
drop table SAMPLE0_PERSON;
drop table SAMPLE0_PERSON_SEQ;
create table SAMPLE0_PERSON
(
PERSON_ID number primary key,
FIRST_NAME varchar2(64),
LAST_NAME varchar2(64),
created timestamp default systimestamp
);
create table SAMPLE0_PERSON_SEQ
(
NEXT number default 0
);
insert into SAMPLE0_person_seq values(1);
commit;
-- for the one-to-one relationship
create table SAMPLE0_CAR_SEQ
(
NEXT number default 0
);
insert into SAMPLE0_CAR_SEQ values(1);
commit;
drop table SAMPLE0_CAR;
create table SAMPLE0_CAR
(
CAR_ID number primary key,
CAR_NAME varchar2(256),
PERSON_ID number
);
alter table sample0_car add constraint fk_car_person foreign key (person_id)
references sample0_person(person_id);