Chapter 7. Collection mapping
// getter/setter ... }
Example 7.34. One to many unidirectional mapping files
Parent-Child
relationship using
<hibernate-mapping> <class name="Parent"> <id name="id"> <generator class="sequence"/> </id> <set name="children"> <key column="parent_id"/> <one-to-many class="Child"/> </set> </class> <class name="Child"> <id name="id"> <generator class="sequence"/> </id> <property name="name"/> </class> </hibernate-mapping>
This maps to the following table definitions:
Example 7.35. Table definitions for unidirectional Parent-Child relationship
create table parent ( id bigint not null primary key ) create table child ( id bigint not null primary key, name varchar(255), parent_id bigint ) alter table child add constraint childfk0 (parent_id) references parent
If the parent is required, use a bidirectional one-to-many association:
Example 7.36. One to many bidirectional annotations
public class Parent { @Id @GeneratedValue private long id; @OneToMany(mappedBy="parent")
180
Parent-Child
relationship using