Wednesday, October 18, 2006

one-to-one bidirectional (in hibernate example) xDoclet

create xDoclet tag both one side
1. assume each user only have one car

user class reference car
car class reference user

2. identify From & To direction

assume that user (from) - car (to)
then user primary key store to car table (foreign key)

3. xDoclet tag

== code ==
car class:
/**
* @hibernate.many-to-one
* class = "local.sample.oo.User"
* column = "userId"
*/
public User getUser()
{
return _user;
}
== code ==

car is to --> so that is many-to-one relationship (from logic it is one-to-one but if both side one to one cannot be identify to & from)
column -> represent the user primary key store column on car table

== code ==
user class:
/**
* @hibernate.one-to-one
* cascade = "save-update"
*/
public Car getCar()
{
return _car;
}
== code ==

user is from -> so that is one-to-one relationship
cascade -> when update user, it also request car to update (if car is modified)

No comments: