logo

A basic *ToOne example

A basic *ToOne example

This project source code is available on github at https://github.com/deanhiller/playorm.

In a typical RDBMS and ManyToOne relationship, you may have two tables (Many Employee can have one phone number)

EMPLOYEE (table)

 

Column Name Column Name Type
Id Long
FIRSTNAME String
LASTNAME String

PHONE (table)

Column Name Column Name Type
Id String
number Long
EmployeeFK Long

In nosql however, the Phone entity will look like<

Column Name Column Name Type Column Value Type
Id N/A keytype=String
number Long Long
EmployeeFK String Long

Example

Let us take two entities. Employee and Phone where MANY Employee can have ONE Phone. Phone will have a @NoSqlManyToOne annotation over one of its field of type Employee. Therefore, it will look like below (along with two other fields id and number).

@NoSqlEntity

public class Phone {
@NoSqlId
private String id;
@NoSqlIndexed
private Long number;
@NoSqlManyToOne
private Employee employee;
}

While, Employee will look like below:

@NoSqlEntity
public class Employee {
@NoSqlId
private String id;
@NoSqlIndexed
private String firstName;
private String lastName;
}

Then we can have some Employee and Phone as below:

Phone ph1 = new Phone();
ph1.setId("ph1");
ph1.setNumber(999999999);
mgr.put(ph1); //this will work and Employee can be null

Employee emp1 = new Employee();
emp1.setId("emp1");
emp1.setFirstName("First");
emp1.setLastName("Last");
mgr.put(emp1);

Phone ph2 = new Phone();
ph2.setId("ph2");
ph2.setNumber(888888888);
ph2.setEmployee(emp1);
mgr.put(ph2);

Phone ph3 = new Phone();
ph3.setId("ph3");
ph3.setNumber(777777777);
ph3.setEmployee(emp1);
mgr.put(ph3);

mgr.flush();

 

<< Back to Documentation Index