Package ejava.examples.orm.map
Class OneManyMapTest
- java.lang.Object
-
- ejava.examples.orm.rel.DemoBase
-
- ejava.examples.orm.map.OneManyMapTest
-
public class OneManyMapTest extends DemoBase
This class demonstrates a One-to-Many relationship represented as a map in the parent. There are two types of parents and children represented in these tests:- OneManyInverseParent to/from OneManyOwningChild - in this case, the foreign key to the parent is supplied in the child table. The child owns the relationship and the parent has a inverse reference back to the child represented as a Map.
- OneManyOwningParent to OneManyChild - in this case, the parent owns the relationship and the child class knows nothing of the relationship. The parent forms a link table to host the 1:1 foreign key to the child and a 1:N relationship to the parent. The child object is referenced by the parent through a Map.
-
-
Constructor Summary
Constructors Constructor Description OneManyMapTest()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
cleanup()
This method will cleanup the database of our OneToMany entities.protected void
postcleanup()
protected void
precleanup()
void
testOneToManyInverseParentMap()
This method tests the case where the child owns the relationship and the parent is the inverse side.void
testOneToManyOwningParentMap()
This method tests the case where the parent owns the relationship.-
Methods inherited from class ejava.examples.orm.rel.DemoBase
setUpBase, setUpDownShared, tearDownBase, tearDownBaseClass
-
-
-
-
Method Detail
-
precleanup
protected void precleanup() throws Exception
- Overrides:
precleanup
in classDemoBase
- Throws:
Exception
-
postcleanup
protected void postcleanup() throws Exception
- Overrides:
postcleanup
in classDemoBase
- Throws:
Exception
-
cleanup
protected void cleanup() throws Exception
This method will cleanup the database of our OneToMany entities. We start with the owning entities, since they own the foreign keys or the link/join tables with the foreign keys. Once the associations have been deleted, all inverse and oblivious entities can be safely removed.- Throws:
Exception
-
testOneToManyInverseParentMap
public void testOneToManyInverseParentMap()
This method tests the case where the child owns the relationship and the parent is the inverse side. If we model the Java form of the relationship without defining a @MapKey, then the following schema gets created,create table ORMMAP_ONEMANY_INVPARENT ( name varchar(255) not null, primary key (name) ); create table ORMMAP_ONEMANY_OWNCHILD ( name varchar(255) not null, oneInverseParent_name varchar(255), mapkey varchar(255), primary key (name) );
But runtime reportsnull index column for collection: ejava.examples.orm.map.annotated.OneManyInverseParent.ownedByChildren
When @MapKey(name="name") is added to the parent definition...
-
testOneToManyOwningParentMap
public void testOneToManyOwningParentMap()
This method tests the case where the parent owns the relationship. Since children in a one-to-many relationship are required by JPA to own the relationship, we can only test this with children that know nothing of the parent. In this case, there is only an owning side. If we model the Java form of the relationship without defining a @MapKey, then the following schema gets created (note that in this class, the name property has been mapped to the @Column(name="ID") within each Entity class).create table ORMMAP_ONEMANY_CHILD ( ID varchar(255) not null, primary key (ID) ); create table ORMMAP_ONEMANY_OWNPARENT ( ID varchar(255) not null, primary key (ID) ); create table ORMMAP_ONEMANY_OWNPARENT_ORMMAP_ONEMANY_CHILD ( ORMMAP_ONEMANY_OWNPARENT_ID varchar(255) not null, ownedChildren_ID varchar(255) not null, mapkey varchar(255), primary key (ORMMAP_ONEMANY_OWNPARENT_ID, mapkey) );
At runtime, we get the following data in the link table.ORMMAP_ONEMANY_OWNPARENT_ID OWNEDCHILDREN_ID MAPKEY --------------------------- ---------------- ------ fred chip chip fred ernie ernie
When @MapKey(name="name") is added to the parent definition...
-
-