Hibernate Example OneToMany with compositeKey
hibernate, java, orm
Solution
I haven't done exactly what you are asking for but this might start you off in the right direction. I think it should work. This page explains these annotations in greater detail.
@Entity
public class Foo {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId(){
}
...
@OneToMany(mappedBy="foo")
public Collection<Bar> getBars() {
}
...
}
@Entity
@IdClass(BarPk.class)
public class Bar implements Serializable {
@ManyToOne
@JoinColumn(name="foo")
@Id
public Foo getFoo() {
return foo;
}
@Column(name="key", length=255)
@Id
public String getKey(){
}
}
@Embeddable
public class BarPk implements Serializable {
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
}
public String getKey(){
...
}
public void setKey(String key){
...
}
//you must include equals() and hashcode()
}
Problem
Can you give me an example of a Hibernate mapping for the following situation: - Parent table(`foo`) with a simple primary key (`foo_id`) child table(`bar`) with a composite key consisting of a) Foreign key to parent table (`foo_id`) b) key(`item`) of type string - There is one parent to many child - The Parent class will have a list of Child objects - When a Parent class is saved, updated, deleted the changes will cascade to the Child