Objective
In the first part of the DB4O Tour series: Db4o, the author introduces the history and status of Db4o, the Application field, and the comparison with ORM. In this article, the author will describe db4o installation, startup, and three different ways of querying: QBE (Query by Example), SODA (simple Object Database Access), and NQ (Native Queries), respectively The query of two associated objects is implemented through these three different approaches. This paper also demonstrates the db4o implementations of several typical functions that are most commonly used in development.
Download and install db4o
Db4o all the latest versions can be downloaded directly from the official website, into the db4o download page, we can see the latest for Java stable version is 5.5, including JAR, source code, Getting Started document, API and so on the complete package file only 6 mb,db4o also has one object number According to the Library management tool ObjectManager, the current version is 1.8.
Next, create a new Java project in Eclipse and import the Db4o Object Database engine package Db4o-5.5-java5.jar into the project. Because db4o supports multiple versions of the JDK, there is a for JDK 1.1, 1.2-1.4 jar pack to accommodate a variety of environments in addition to Db4o-5.5-java5.jar for JDK 5.0. Compared with Hibernate, IBATIS SQL Maps, db4o is more natural and does not need to refer to a Third-party support library too much.
Open Database
Db4o How to make object persistence? Browsing directories reveals that, like a traditional RDBMS, DB4O has its own database file, and in db4o the suffix name of the database file is "*.yap". Let's take a look at the main package structure of the Db4o object Database engine:
Com.db4o
Com.db4o contains features that are most commonly used when using db4o. The two most important interfaces are com.db4o.Db4o and Com.db4o.ObjectContainer. The Com.db4o.Db4o factory is the starting point for running db4o, a static method in this class that can open a database file, start a server, or connect to an existing server, and then configure the DB4O environment before opening the database. The Com.db4o.ObjectContainer interface is important and is used 99% of the time in the development process, Objectcontainer can be either a database instance in Single-user mode or as a client of a db4o server. Each Objectcontainer instance has its own transaction. All operations have a transaction guarantee. When Objectcontainer is turned on, the transaction is already entered, and the next transaction starts immediately when commit () or rollback (). Each Objectcontainer instance maintains its own managed stored and instantiated objects, and it remains open when objectcontainer is needed, and once closed, objects referenced in the in-memory database are discarded.
Com.db4o.ext
You may wonder why there are only a few ways to see the Objectcontainer, for the following reasons: The Db4o interface provides two paths, respectively, in the com.db4o and Com.db4o.ext packages. This is done first to allow developers to get started quickly, and second, to make it easier for other products to replicate the basic db4o interface, and developers can see from this point that db4o is fairly lightweight. Each Com.db4o.ObjectContainer object is also a Com.db4o.ext.ExtObjectContainer object. can be converted to Extobjectcontainer for more advanced features.
Com.db4o.config
Com.db4o.config contains all the classes required to configure DB4O.
Com.db4o.query
The Com.db4o.query package contains the predicate classes that are required to construct the native query, NQ (Native Queries). NQ is the most important query interface for DB4O.
DB4O provides two modes of operation, namely local mode and server mode. Local mode is to open the Db4o database file directly in the program to operate:
Objectcontainer db = Db4o.openfile ("Auto.yap");
The server mode is where the client accesses the server by IP address, port, and authorization password:
Server side:
ObjectServer server=Db4o.openServer("auto.yap",1212);
server.grantAccess("admin","123456");
Client:
Objectcontainer db=db4o.openclient ("192.168.0.10", 1212, "admin", "123456");
The Objectcontainer instance can be obtained in both ways, in view of the current Java EE application environment, the server mode is more practical and the local mode is more suitable for embedded application. To simplify the demo, this article will use local mode in the following examples.