Suppose I have 2 JPA entities:
@Entity
public class OwnerEntity {
private List<OwnedEntity> subEntities
// ...
}
@Entity
public class OwnedEntity {
private String quasiUniqueSid;
private OwnerEntity ownerEntity
// ...
}
As you can see they have a many to one relationship: an OwnerEntity can have many OwnedEntitys.
What I want to achieve is to assign each OwnedEntity a unique sid based on its owner. So I can have for example 2 owned entities with the same quasiUniqueSid but they cannot have the same owner. Do Hibernate has some built-in functionality for this kind of problem? I can remember other ORMs (not Java related) which could do this thus my question. I’m using the latest Hibernate version (4.1.8)
It seems you need a composite key, like this:
A full documentation you can find here.
If you wanna explicitly define the column name for ownerEntity, you can use: