Ordercolumn

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jpa/ordercolumn/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=OrderColumnBiJoinTestmvn test -Dtest=OrderColumnBiMappedByTestmvn test -Dtest=OrderColumnUniTest

OrderColumnBiJoinTest

This tests and demonstrates the {@link OrderColumn} annotation when used together with a bi-directional parent-child mapping, where the child holds a foreign key to the parent.

<p> This is a variation on the normal oneToMany mapping using two {@link JoinColumn} annotations instead of using the <code>mappedBy</code> attribute.

<p> In this mapping the position each child has in the parent’s list is stored in the child table and fully managed by JPA. This means that this position index does not explicitly show up in the object model.

<p> Example SQL DDL (h2 syntax) <pre> create table Parent ( id bigint generated by default as identity,

primary key (id) );

create table Child ( id bigint generated by default as identity, parent_id bigint, children_ORDER integer,

primary key (id), foreign key (parent_id) references Parent ); </pre>

Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
private static Archive<?> createDeployment() {
    return ShrinkWrap.create(WebArchive.class)
                     .addPackages(true, Child.class.getPackage())
                     .addPackages(true, OrderColumnTesterService.class.getPackage())
                     .addAsResource("META-INF/persistence.xml");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void saveInOneGo() {
    Parent parent = new Parent();

    Child child1 = new Child();
    child1.setParent(parent);

    Child child2 = new Child();
    child2.setParent(parent);

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    // Reload parent fresh from data source again
    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );
}

Saves a parent instance first, then adds two children instances and saves again.

<p> Example sequence of insert/update statements that may be generated to accomplish this: <pre> insert into Parent (id) values (null) insert into Child (id) values (null) insert into Child (id) values (null)

update Child set parent_id = 1, children_ORDER = 0 where id = 1 update Child set parent_id = 1, children_ORDER = 1 where id = 2 </pre>

@Test
public void saveParentSeparatelyFirst() {

    Parent parent = indexColumnTesterService.save(new Parent());

    Child child1 = new Child();
    child1.setParent(parent);

    Child child2 = new Child();
    child2.setParent(parent);

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );

}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void saveParentWithOneChildFirst() {

    Parent parent = new Parent();
    Child child1 = new Child();
    child1.setParent(parent);
    parent.getChildren().add(child1);

    // Save parent with 1 child in one go
    parent = indexColumnTesterService.save(parent);

    Child child2 = new Child();
    child2.setParent(parent);
    parent.getChildren().add(child2);

    // Save parent again with second child
    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void saveParentWithChildrenThenDeleteOned() {

    Parent parent = new Parent();

    Child child1 = new Child();
    child1.setParent(parent);
    parent.getChildren().add(child1);

    Child child2 = new Child();
    child2.setParent(parent);
    parent.getChildren().add(child2);

    Child child3 = new Child();
    child3.setParent(parent);
    parent.getChildren().add(child3);

    // Save parent with 3 children
    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("3 children added to parent and saved, but after re-loading number of chilren different",
        3, savedParent.getChildren().size()
    );

    // Removing child at position 1 and saving again
    savedParent.getChildren().remove(1);

    savedParent = indexColumnTesterService.save(savedParent);
    savedParent = indexColumnTesterService.getParentById(savedParent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );

}

OrderColumnBiMappedByTest

This tests and demonstrates the {@link OrderColumn} annotation when used together with a bi-directional parent-child mapping, where the child holds a foreign key to the parent.

<p> This is the normal oneToMany mapping using the <code>mappedBy</code> attribute. Even though this is the most straightforward mapping, because of a mis-interpretation in the JPA spec it was believed for years that this mapping did not have to be supported (see https://hibernate.atlassian.net/browse/HHH-5732 among others)

<p> In this mapping the position each child has in the parent’s list is stored in the child table and fully managed by JPA. This means that this position index does not explicitly show up in the object model.

<p> Example SQL DDL (h2 syntax) <pre> create table Parent ( id bigint generated by default as identity,

primary key (id) );

create table Child ( id bigint generated by default as identity, parent_id bigint, children_ORDER integer,

primary key (id), foreign key (parent_id) references Parent ); </pre>

Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
private static Archive<?> createDeployment() {
    return ShrinkWrap.create(WebArchive.class)
                     .addPackages(true, Parent.class.getPackage())
                     .addPackages(true, OrderColumnTesterService.class.getPackage())
                     .addAsResource("META-INF/persistence.xml");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void saveInOneGo() {

    Parent parent = new Parent();

    Child child1 = new Child();
    child1.setParent(parent);

    Child child2 = new Child();
    child2.setParent(parent);

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );
}

Saves a parent instance first, then adds two children instances and saves again.

<p> Example sequence of insert/update statements that may be generated to accomplish this: <pre> insert into Parent (id) values (null) insert into Child (id, parent_id) values (null, 1) insert into Child (id, parent_id) values (null, 1)

update Child set children_ORDER = 0 where id = 1 update Child set children_ORDER = 1 where id = 2 </pre>

@Test
public void saveParentSeparatelyFirst() {

    Parent parent = indexColumnTesterService.save(new Parent());

    Child child1 = new Child();
    child1.setParent(parent);

    Child child2 = new Child();
    child2.setParent(parent);

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );

}

OrderColumnUniTest

This tests and demonstrates the {@link OrderColumn} annotation when used together with a uni-directional parent-child mapping, where the child table holds a foreign key to the parent.

<p> In this mapping the position each child has in the parent’s list is stored in the child table and fully managed by JPA. This means that this position index does not explicitly show up in the object model.

<p> Example SQL DDL (h2 syntax) <pre> create table Parent ( id bigint generated by default as identity,

primary key (id) );

create table Child ( id bigint generated by default as identity, children_id bigint, children_ORDER integer,

primary key (id), foreign key (children_id) references Parent ); </pre>

Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
private static Archive<?> createDeployment() {
    return ShrinkWrap.create(WebArchive.class)
                     .addPackages(true, Child.class.getPackage())
                     .addPackages(true, OrderColumnTesterService.class.getPackage())
                     .addAsResource("META-INF/persistence.xml");
}

Saves a parent instance with 2 children in its children collection in one operation.

<p> Example sequence of insert/update statements that may be generated to accomplish this: <pre> insert into Parent (id) values (null) insert into Child (id) values (null) insert into Child (id) values (null)

update Child set children_id = 1, children_ORDER = 0 where id = 1 update Child set children_id = 1, children_ORDER = 1 where id = 2 </pre>

@Test
public void saveInOneGo() {

    Parent parent = new Parent();
    Child child1 = new Child();
    Child child2 = new Child();

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );
}

Saves a parent instance first, then adds two children instances and saves again.

<p> Example sequence of insert/update statements that may be generated to accomplish this: <pre> insert into Parent (id) values (null) insert into Child (id) values (null) insert into Child (id) values (null)

update Child set children_id = 1, children_ORDER = 0 where id = 1 update Child set children_id = 1, children_ORDER = 1 where id = 2 </pre>

@Test
public void saveParentSeparatelyFirst() {

    Parent parent = indexColumnTesterService.save(new Parent());

    Child child1 = new Child();
    Child child2 = new Child();

    parent.getChildren().add(child1);
    parent.getChildren().add(child2);

    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );

}

Saves a parent with one child in its children collection first, then adds another child and saves again.

<p> Example sequence of insert/update statements that may be generated to accomplish this: <pre> insert into Parent (id) values (null) insert into Child (id) values (null) update Child set children_id = 1, children_ORDER = 0 where id = 1

insert into Child (id) values (null) update Child set children_id = 1, children_ORDER = 1 where id = 2 </pre>

@Test
public void saveParentWithOneChildFirst() {

    Parent parent = new Parent();
    Child child1 = new Child();
    parent.getChildren().add(child1);

    // Save parent with 1 child in one go
    parent = indexColumnTesterService.save(parent);

    Child child2 = new Child();
    parent.getChildren().add(child2);

    // Save parent again with second child
    parent = indexColumnTesterService.save(parent);

    Parent savedParent = indexColumnTesterService.getParentById(parent.getId());

    assertEquals("2 children added to parent and saved, but after re-loading number of chilren different",
        2, savedParent.getChildren().size()
    );

}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Oct 05, 2014: Tests for the @ordercolumn annotation by arjan tijms
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jpa/ordercolumn/

Do the changes as you see fit and send a pull request!

Good Luck!