I am having problems using Oracle sequence generators in a composite primary key.
I am using Hibernate 3.2.3 , Oracle 10g
I have a table, CHILDS, with primary key ( parent_Id, child_Id ) .
I started using @EmbeddedId - which worked well.
Until I tried to use an Oracle Sequence Generator to generate the child_id value during an insert. Could not get it working. ( Oracle sequence generator name = "SQ_CHILD_ID" )
Then discovered that couldn't use @EmbeddedId with @GeneratedValue / @SequenceGenerator
[url]http://forums.hibernate.org/viewtopic.php?p=2393944&sid=57b20ef2c7de6f6bcba632130987bc22
[/url]
So then used an @IdClass instead. Again , same ORA error ( Cannot insert NULL into CHILDS.CHILD_ID ).
Any suggestions would be very helpful.
Here is the code snippet using IdClass
@Entity
@Table( name="CHILDS" )
@IdClass( ChildsId.class )
public class Child implements Serializable {
Integer parentId ;
Integer childId ;
[color=red] @Id
@Column(name = "CHILD_ID", nullable = false, precision = 8, scale = 0)
@SequenceGenerator( name="SQ_CHILD_ID", sequenceName="SQ_CHILD_ID" )
@GeneratedValue( strategy=GenerationType.SEQUENCE, generator="SQ_CHILD_ID")
[/color] public Integer getChildId() {
return childId ;
}
public void setChildId( Integer id ) {
childId = id ;
}
@Id
@Column(name = "PARENT_ID", nullable = false, precision = 8, scale = 0)
public Integer getParentId() {
return parentId;
}
public void setParentId( Integer id ) {
parentId = id ;
}
}
public class ChildsId implements Serializable{
Integer parentId ;
Integer childId ;
[color=red] @Column(name = "CHILD_ID", nullable = false, precision = 8, scale = 0)
@SequenceGenerator( name="SQ_CHILD_ID", sequenceName="SQ_CHILD_ID" )
@GeneratedValue( strategy=GenerationType.SEQUENCE, generator="SQ_CHILD_ID") [/color]
public Integer getChildId() {
return childId ;
}
public void setChildId( Integer id ) {
childId = id ;
}
@Column(name = "PARENT_ID", nullable = false, precision = 8, scale = 0)
public Integer getParentId() {
return parentId;
}
public void setParentId( Integer id ) {
parentId = id ;
}
}
|