When Adopting the first approach, your model probably looks like this(Assuming Parent and Child are AggregateRoot s):
public Class Parent {
private Long id;
}
public Class Child {
private Long id;
private Long parentId; // most orm need this field.
}
Then you can use childRepository.findBy(parentId): List<Child>
to retrieve all children belonging to a Parent and use parentRepository.findBy(child.getParentId()):Parent
to retrieve a child's Parent. But this is not your case since you said "However, an invariant of the model needs to be that this order is well defined". This leads to another approach: Parent and Child as AggregateRoot and a ValueObject to keep the relationships:
public Class Parent {
private Long id;
private List<LineChild> children;
}
public class LineChild {
private int ordinal;
private Long childId;
}
public Class Child {
private Long id;
}
But in my expirences, most orm doesn't support this (the second db schema is used instead). Some "half orm tool" can help, like iBATIS.
<resultMap class="Parent">
<property name="id" column="id" />
<property name="children" column="id" select="findChilren"/>
</resultMap>
<select id="findChilren" parameterClass="long" resultMap="LineChild">
select * from Child where parant_id = #parantId# order by ordinal
</select>
<resultMap id="LineChild" class="LineChild">
<property name="ordinal" column="ordinal" />
<property name="childId" column="id"/>
</resultMap>
But you lost all benefits from an orm framework, such as dynamic update and so on.
Or, consider eventual consistency?
last but quite confused, why "A Child can only belong to one Parent"? I have mom and dad! :P