user3056052
user3056052

Reputation: 1467

Can hibernate persistent objects contain a collection of other persistent objects?

I don't know if there is a feature for this or if I am making sense, but how this would work in my head is the persistent object would create a built in relationship between the persistent objects. The type of relationship would be annotation defined. Is there existing syntax and functionality for this hidden in the advanced features of hibernate that I haven't come across yet?

Upvotes: 0

Views: 39

Answers (1)

mscoon
mscoon

Reputation: 88

I believe what you are looking for is the @OneToMany annotation.

E.g.

class Detail {

  Long id;

  @ManyToOne @JoinColumn(name="parent_id", updatable=false)
  Parent parent;
}

class Parent {

  Long id;

  @OneToMany(mappedBy="parent")
  Set<Detail> details;

}

The collection property can be a different collection type (e.g. List). Hibernate will implement the appropriate semantics (e.g. unique children for the set, order for a list, etc).

Upvotes: 1

Related Questions