Hi everyone,
I've got a problem with composite Ids and hiberante docs don't help me further.
I have two tables that
both have composite Ids and
Many-to-One relationship between each other. Furthermore the foreign key consists of only one identifier, the database model is following:
-------------------------------------------------
Table InAttribute:att_id
pkver_id
pkobj_id
fk //many-to-one
floatvalue
....
-------------------------------------------------
Table InObjectobj_id
pkver_id
pk...
-------------------------------------------------
As you see, ATTRIBUTES are linkes unidirectionally Many-to-One to OBJECTS through obj_id foreign key (but not through both obj_id and ver_id). Everything that I tries before, didn't work. So I implemented composite id for InObject and InAttribute that way:
Code:
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "objId", column = @Column(name = "OBJ_ID", nullable = false)),
@AttributeOverride(name = "verId", column = @Column(name = "VER_ID", nullable = false)) })
public InObjectId getId() {
return this.id;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "attId", column = @Column(name = "ATT_ID", nullable = false)),
@AttributeOverride(name = "verId", column = @Column(name = "VER_ID", nullable = false)) })
public InAttributeId getId() {
return this.id;
}
}
InAttribute has following reference to InObject (ManyToOne):
Code:
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="OBJ_ID")
public InObject getInObject(){
return inObject;
}
but I've got an error from hebrnate, because the number of foreign keys should be 2.
The reference in InObject looks like this:
Code:
@OneToMany(mappedBy="inObject", fetch=FetchType.LAZY)
public Collection<InAttribute> getInAttributes(){
return inAttributes;
}
I can't find information about such situations because hibernate documentation handles tables where number of foreign keys is equal to the number of primary keys.
Thanks in advance!
Yuriy