📜 ⬆️ ⬇️

ObjectDB - database management system for Java applications

ObjectDB is an object-oriented Java-written DBMS, which, with all its impressive speed tests and used (as follows from advertising on the official website) by organizations like HP and Novell, is not familiar to many programmers ( I myself found out about this database a month ago , and used it only once in the framework of the educational project, and my teacher learned about it just from my project ). For the continuation I ask under the cat.


For a start, the unpleasant news is that ObjectDB is a commercial DBMS and you have to pay for its use, though if the number of object records exceeds 1,000,000 units and the number of classes is 10 units per database file, which means that you can store them in one file. up to a million objects of 10 different classes without acquiring any licenses, including for commercial use.

Physical limitations:

Now about the pleasant:
Key features and key features:
and many other tools, such as: connection management, database restoration, several caching mechanisms, simple and composite indexes, support for JPQL and JDOQL, etc.
')
As the name implies, ObjectDB works with objects, that is, it does not store tables in itself, but java-objects as a whole, and operations with objects in the database are performed by familiar CRUD operations. A complete list of types of objects available for storage can be viewed on the official website.

How it works:

Consider the use of DBMS on the example of a very simple user data storage database.
To work, we need to create 2 classes: a class representing the user and a class performing operations on it.

Class user.java
This class will be stored in our database.

 package db; import java.io.Serializable; import javax.jdo.annotations.Unique; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; public User() { } public User(String name, String pass) { this.name = name; this.pass = pass; } public Long getId() { return id; } @Unique private String name; private String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } } 
package db; import java.io.Serializable; import javax.jdo.annotations.Unique; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; public User() { } public User(String name, String pass) { this.name = name; this.pass = pass; } public Long getId() { return id; } @Unique private String name; private String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }


The class is a completely familiar Java-bean, with the exception of a few annotations, which actually make it possible to use it as an element of the database, let's look at them in more detail.

@Entity
The @Entity annotation indicates that the class is used as a data storage item in the database.
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

The @Id annotation marks the id element as a Primary key, and @GeneratedValue (strategy = GenerationType.AUTO) defines a method for generating values ​​for it. GenerationType.AUTO also means that if the key value is not specified explicitly, it will be created automatically.
@Unique private String name; The @Unique annotation marks the field that should have a unique value, when trying to write an object to the database, whose value of the field marked with this annotation coincides with the previously recorded database object will throw an exception. Thus, we make it impossible to register several users with the same name.

Class UserDBAdapter.java
This class performs user database operations.
 package db; import javax.persistence.*; public class UserDBAdapter { EntityManagerFactory emf; EntityManager em; public UserDBAdapter() { emf = Persistence.createEntityManagerFactory("$objectdb/db/usr.odb"); em = emf.createEntityManager(); } @Override protected void finalize(){ em.close(); emf.close(); } public boolean persistUser(User u) { try { em.getTransaction().begin(); em.persist(u); em.getTransaction().commit(); em.clear(); return true; } catch(PersistenceException e){ System.out.println(e.getMessage()); em.clear(); em.close(); emf.close(); return false; } } public User getUserById(long id) { User u1 = em.find(User.class, id); return u1; } public User getUserByName(String name) { TypedQuery q = em.createQuery("SELECT u FROM User u WHERE u.name = :name", User.class); User u1 = (User) q.setParameter("name", name).getSingleResult(); return u1; } public boolean autentificate(String name, String pass) throws PersistenceException{ TypedQuery q = em.createQuery("SELECT u FROM User u WHERE u.name = :name AND u.pass = :pass", User.class); try { User u1 = (User)q.setParameter("name", name).setParameter("pass", pass).getSingleResult(); return true; } catch(PersistenceException e) { em.clear(); em.close(); emf.close(); throw new PersistenceException("username not found or password is invalid"); } } } 
package db; import javax.persistence.*; public class UserDBAdapter { EntityManagerFactory emf; EntityManager em; public UserDBAdapter() { emf = Persistence.createEntityManagerFactory("$objectdb/db/usr.odb"); em = emf.createEntityManager(); } @Override protected void finalize(){ em.close(); emf.close(); } public boolean persistUser(User u) { try { em.getTransaction().begin(); em.persist(u); em.getTransaction().commit(); em.clear(); return true; } catch(PersistenceException e){ System.out.println(e.getMessage()); em.clear(); em.close(); emf.close(); return false; } } public User getUserById(long id) { User u1 = em.find(User.class, id); return u1; } public User getUserByName(String name) { TypedQuery q = em.createQuery("SELECT u FROM User u WHERE u.name = :name", User.class); User u1 = (User) q.setParameter("name", name).getSingleResult(); return u1; } public boolean autentificate(String name, String pass) throws PersistenceException{ TypedQuery q = em.createQuery("SELECT u FROM User u WHERE u.name = :name AND u.pass = :pass", User.class); try { User u1 = (User)q.setParameter("name", name).setParameter("pass", pass).getSingleResult(); return true; } catch(PersistenceException e) { em.clear(); em.close(); emf.close(); throw new PersistenceException("username not found or password is invalid"); } } }


The class constructor creates a database connection ( if the database file is not found, it will be created ).

The public boolean persistUser(User u) method is used to add a new user to the database. Operations that change the state of objects in the database, such as adding a new object, must be performed inside a transaction that is opened by the em.getTransaction().begin() method, and closed by the em.getTransaction().commit() method ( this is when the object is physically saved in the database ).
Methods that do not change the state of the database, such as read operations from the database, do not require an active transaction.

To retrieve objects from the database, SQL-like JPQL is used in this example method.
public User getUserByName(String name) and public boolean autentificate(String name, String pass)
or the built-in methods of the Entitymanager class, such as em.find(User.class, id) which allows you to find an object of the User class here by its primary key (id).

It was a very simple example of using ObjectDB, which does not reflect all its capabilities, but, I hope, can be useful for people unfamiliar with this DBMS as a first acquaintance.
Detailed information and tutorials are available on the official project website.

Source: https://habr.com/ru/post/145679/


All Articles