Hibernate version:3.1.3
Hibernate Annotations version: 3.1beta9
Oracle version: 10.1.0.2.0
I'm trying to create a parent/child one-to-one association where the parent PK is generated by an oracle sequence. I want the child's FK to be set using the same sequence generated Id. The only way I've been able to get this to work is to create a bidirectional 1-to-1 mapping using the @GenericGenerator(strategy="foreign"...) annotation.
Questions...
1) Is there an ejb 3 "standard" way to do this without having to introduce the hibernate dependency?
2) is there a way to do this with a unidirectional 1-to-1 mapping?
Parent Class
Code:
@Entity
@Table(name="TblParent")
@SequenceGenerator(name="ParentSeq", sequenceName="PARENT_SEQ")
public class Parent
{
private long _id;
...
private Child _child;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ParentSeq")
public long getId()
{
return _id;
}
public void setId(long id)
{
_id = id;
}
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
public Child getChild()
{
return _child;
}
public void setChild(Child child)
{
_child = child;
}
}
Child Class
Code:
@Entity
@Table(name="TblChild")
public class Child
{
private long _id;
...
private Parent _parent;
@Id
@GeneratedValue(generator="foreign")
@GenericGenerator(name="foreign",strategy="foreign",parameters = {@Parameter(name="property", value="parent")})
public long getId()
{
return _id;
}
public void setId(long id)
{
_id = id;
}
@OneToOne
@PrimaryKeyJoinColumn
public Parent getParent()
{
return _parent;
}
public void setParent(Parent parent)
{
_parent = parent;
}
Session Persisting Code...
Code:
Parent parent = new Parent();
parent.setName("Parent");
Child child = new Child();
child.setName("Child");
child.setParent(parent);
parent.setChild(child);
session.save(parent);
thanks,
Brett