Hi,
I'm facing a problem regarding the using of @GeneretedValue annotation in a composite primary key.
I have a first table named PARENT.
Here's its colums :
DBKEY NUMBER(9)
...
Here's this mapped class :
Code:
@Entity
@Table (name = "PARENT")
public class ParentBO implements Serializable {
/**
*
*/
private static final long serialVersionUID = -170862807387203780L;
private Long dbKey;
/**
* @return the dbKey
*/
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@Column (name = "DBKEY", precision = 9, nullable = false)
public Long getDbKey() {
return dbKey;
}
/**
* @param dbKey the dbKey to set
*/
public void setDbKey(Long dbKey) {
this.dbKey = dbKey;
}
/**
* @return the children
*/
@OneToMany (mappedBy = "childPk.parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OrderBy
public Set<ChildBO> getChildren() {
return children;
}
/**
* @param children the children to set
*/
public void setChildren(Set<ChildBO> children) {
this.children = children;
}
}
This table has a oneToMany relation with the second table named CHILD.
Here's its colums :
DBKEY NUMBER(9)
SEQNR NUMBER(4)
...
Here's the mapped class :
Code:
@Entity
@Table (name = "CHILD")
public class ChildBO implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6953833174988560128L;
private ChildPk childPk;
/**
* @return the childPk
*/
@EmbeddedId
public TitlePk getChildPk() {
return titlePk;
}
/**
* @param childPk the childPk to set
*/
public void setChildPk(ChildPk childPk) {
this.childPk = childPk;
}
}
And the mapped class for the primary key :
Code:
public class ChildPk implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5758018014541880382L;
private ParentBO parent
private Integer sequenceNumber;
/**
* @return the parentBO
*/
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "DBKEY")
public ParentBO getParent() {
return parent;
}
/**
* @param parentthe parentto set
*/
public void setParent(ParentBO parent) {
this.parent = parent;
}
/**
* @return the sequenceNumber
*/
@GenericGenerator(name="increment", strategy = "increment")
@GeneratedValue(generator="increment")
@Column (name = "SEQNR", precision = 4)
public Integer getSequenceNumber() {
return sequenceNumber;
}
/**
* @param sequenceNumber the sequenceNumber to set
*/
public void setSequenceNumber(Integer sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
}
When I save the ParentBO object containing 2 children, the @GeneratedValue(generator="increment") in the ChildPk class is ignored by hibernate and so the sequenceNumber is null.
As a result, an exception org.hibernate.NonUniqueObjectException is throwed. Indeed, the 2 children objects have a the same DBKEY value and the same SEQNR value, but null ! not good...
Hibernate should select the max id for SEQNR according this way : select max(seqnr) from child where dbkey=<dbkey of parent>.
So my question, can i use a @GeneratedValue in a EmbeddedId
class (composite primary key) ?
Hibernate Version :hibernate-core-3.3.0.SP1, hibernate-annotations-3.4.0.GA
Database :Oracle 10g
Thanks for reading.