Java Reference
In-Depth Information
object-oriented database access from Java
As mentioned, OODBMs store objects rather than relational tuples. Hence, no mapping is needed to
store Java objects into an OODBMS. Although this may seem conceptually appealing at first sight,
the success of OODBMSs in the industry has been limited to niche sectors, such as spatial and sci-
entific applications. One of the reasons often mentioned for this is their intrinsic complexity when
compared to RDBMSs.
In the next exercise, you see how Java can work with ObjectDB, a popular OODBMS, providing
support for all the common database operations (such as transaction management, query processing,
indexing, and so on). This is an example using the JPA and JDO APIs you saw earlier in the chapter.
It is implemented as a single JAR and the database is stored as a single file.
Working with ObjectDB from Java 
try it out
In this example, you will use ObjectDB to see how OODBMSs interact with Java directly.
1.
Start by installing ObjectDB from http://www.objectdb.com/object/db/database/download .
In this example, ObjectDB version 2.5.6_04 was installed in the directory C:\objectdb-2.5.6_04 .
2.
Continue from your previous Java project, Chapter9 , and add the file C:\objectdb-2.5.6_04\
objectdb-2.5.6_04\bin\objectdb.jar to the build path of the project. Remember, you can do
this by right-clicking the project and choosing Build Path Add External Archives.
3.
Make sure the classes Employee.java and Project.java , as defined in the previous activity, are
still available in the current project.
4.
Create a new class called myDBApp3 as follows:
import javax.persistence.*;
import java.util.*;
public class myDBApp3 {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
"C:/objectdb-2.5.6_04/db/employeeadm.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Set<Project> projects=new HashSet<Project>();
projects.add(new Project(1,"Basic ObjectDB Project"));
projects.add(new Project(2, "Advanced ObjectDB Project"));
Employee Myemp=new Employee(1,"Object DB freak", "Male", 1, projects );
em.persist(Myemp);
em.getTransaction().commit();
Query q1 = em.createQuery("SELECT COUNT(emp) FROM Employee emp");
System.out.println("Total Employees: " + q1.getSingleResult());
TypedQuery<Project> query =
em.createQuery("SELECT proj FROM Project proj", Project.class);
List<Project> results = query.getResultList();
for (Project proj : results) {
System.out.print(proj.getProjectID());
System.out.print(" ");;
 
Search WWH ::




Custom Search