1. The first step it is need to create a simple POJO. An empty constructor is mandatory.
public class Organization {
String name;
String description;
public Organization() {
}
public Organization(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
2. Next step, we open a database and save POJO
public void createNewOrg() {
//Open the database in a thread-safe way i.e. in every thread we must create new db object this way
//A database mydb must exist.
OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
try {
//Register our class in OrientDB Entity manager to work with POJO's
db.getEntityManager().registerEntityClass(Organization.class);
// create a new proxied object and fill its properties
Organization org = db.newInstance(Organization.class);
org.setName("MyOrg");
org.setDescription("This is my good org.");
db.save(org);
} finally {
//ALWAYS CLOSE TO RELEASE CONNECTION POOL RESOURCES
db.close();
}
}
3. Next step we demonstrate a simple query
public void browseAllOrgs() {
// Open a database
OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
try {
//Register the class to work with it with Entity Manager
db.getEntityManager().registerEntityClass(Organization.class);
//Select all objects of class Organization and print their properties
for (Organization orgItem : db.browseClass(Organization.class)) {
System.out.print(orgItem.getName() + ' ');
System.out.println(orgItem.getDescription());
}
System.out.println("-------------------");
//Select all objects of class Organization were description contains word good
// and print their properties
List result = db.query(
new OSQLSynchQuery("select * from Organization where description like '%good%'"));
for (Organization org : result) {
System.out.print(org.getName() + ' ');
System.out.println(org.getDescription());
}
} finally {
//ALWAYS CLOSE
db.close();
}
}
PPS. It is not necessary to create table (class) type of Organization before run this sample. New table (class) with name Organization will be created during the first run.
PPPS. If you add a new property (field) to class Organization then at next run a new object will appear in database with this new field. Older objects will remain unchanged.
public class Organization {
String name;
String description;
public Organization() {
}
public Organization(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
2. Next step, we open a database and save POJO
public void createNewOrg() {
//Open the database in a thread-safe way i.e. in every thread we must create new db object this way
//A database mydb must exist.
OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
try {
//Register our class in OrientDB Entity manager to work with POJO's
db.getEntityManager().registerEntityClass(Organization.class);
// create a new proxied object and fill its properties
Organization org = db.newInstance(Organization.class);
org.setName("MyOrg");
org.setDescription("This is my good org.");
db.save(org);
} finally {
//ALWAYS CLOSE TO RELEASE CONNECTION POOL RESOURCES
db.close();
}
}
3. Next step we demonstrate a simple query
public void browseAllOrgs() {
// Open a database
OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
try {
//Register the class to work with it with Entity Manager
db.getEntityManager().registerEntityClass(Organization.class);
//Select all objects of class Organization and print their properties
for (Organization orgItem : db.browseClass(Organization.class)) {
System.out.print(orgItem.getName() + ' ');
System.out.println(orgItem.getDescription());
}
System.out.println("-------------------");
//Select all objects of class Organization were description contains word good
// and print their properties
List
new OSQLSynchQuery
for (Organization org : result) {
System.out.print(org.getName() + ' ');
System.out.println(org.getDescription());
}
} finally {
//ALWAYS CLOSE
db.close();
}
}
That's all.
PS. OObjectDatabasePool.global().acquire reuses the database connection to avoid to create it every time. db.close() doesn't close the database but release it to the owner pool. It could be reused in the future.
PPS. It is not necessary to create table (class) type of Organization before run this sample. New table (class) with name Organization will be created during the first run.
PPPS. If you add a new property (field) to class Organization then at next run a new object will appear in database with this new field. Older objects will remain unchanged.