Skip to content

Tutorial 7: CreationPlan in action

Kumar Rohit edited this page Apr 15, 2016 · 2 revisions

Manipulation of Entity Values

At this point of time, we have not persisted the data, we will just be manipulating their values in memory before persisting. This way we reduce number of database calls and overall performance

You are provided a handle to all the entities which are going to be created. In the previous section we have created two different Employees referring to single Person

Plan plan = Plan.create()
            .add(Entity.of(Employee.class, 2).with(Employee_.Country, INDIA))
            .add(Entity.of(Person.class).with(Person_.gender, "Male");
CreationPlan creationPlan = jpaContext.create(plan);

/* printed creationPlan
└── *ROOT*
    └── com.github.kuros.entity.Person|0
        ├── com.github.kuros.entity.Employee|0
        └── com.github.kuros.entity.Employee|1
*/

We can set custom values to the objects getting created, here we are setting different names to Employees

creationPlan.set(Employee_.name, "Rohit");
creationPlan.set(1, Employee_.name, "Jon");

Deleting in-memory nodes

We have a scenario where we have two Managers, one with 2 Employee and other having just 1 Employee under him. Lets create it.

Plan plan = Plan.create()
                .add(Entity.of(Manager.class, 2))
                .add(Entity.of(Employee.class, 2));
CreationPlan creationPlan = jpaContext.create(plan);

/* printed creationPlan
└── *ROOT*
    ├── com.github.kuros.entity.Manager|0
    │   ├── com.github.kuros.entity.Employee|0
    │   └── com.github.kuros.entity.Employee|1
    └── com.github.kuros.entity.Manager|1
        ├── com.github.kuros.entity.Employee|2
        └── com.github.kuros.entity.Employee|3
*/

As per our requirement, we need first manager with 2 employees & second manager with 1 employee. To achieve it, we will just delete the entity from this plan.

creationPlan.deleteItem(Employee.class, 3);

/* printed hierarchy of persisted entities (details in the next section) 
    
    └── *ROOT*
        ├── com.github.kuros.entity.Manager|0 [managerId: 1]
        │   ├── com.github.kuros.entity.Employee|0 [employeeId: 1]
        │   └── com.github.kuros.entity.Employee|1 [employeeId: 2]
        └── com.github.kuros.entity.Manager|1 [managerId: 2]
            └── com.github.kuros.entity.Employee|2 [employeeId: 3]
*/
Clone this wiki locally