Use OJB as a storage layer in the application (6)

zhaozj2021-02-12  145

Product mapping:

Now we have to begin to explain the mapping from the Product class to Database Table Product. Usually this mapping is not passed

Programming implementation, but by defining an XML file. The DescriptorRepository class provides some ways to read

XML files and initialization. As a result, it can be used in future programming (or by pure programming).

We have written our mapping relationship, files are placed in SRC / TEST / OJB / Repository.xml, XML

The documents are as follows:

Use this file as a template for building your own mapings ->

]>

isolation-level = "read-uncommitted">

Specify ITS OWN JDBC Connection,

The Connection Specified Here Will BE Used. ->

Platform = "hsqldb"

JDBC-Level = "2.0"

Driver = "org.hsqldb.jdbcdriver"

Protocol = "JDBC"

Subprotocol = "HSQLDB"

DBALIAS = "/ Samples / HSQL / OJB"

UserName = "sa"

PASSWORD = ""

/>

& user;

& junit;

& inf;

The file contains a lot of information:

1. The XML file verifies its validity via repository.dtd. This guarantees the correctness of the XML file syntax.

Be sure to put the DTD file in the same directory as the XML file, otherwise the XML parser will report an error that cannot be found DTD

2. The mapping contains a default JDBCConnectDescriptor. This description contains the JDBC connection used in the storage operation. Use this description for classes that do not define special JDBCConnectionDescriptors.

The default JDBC connection is described.

In our example, the description has declared all the operations that drive by JSQLDB JDBC, the corresponding database is placed

The corresponding directory is below.

3. OJB mapping regression test. By containing XML Entity & Junit in repository_junit.xml. This entry

ITY is defined in an included file:

4. OJB's mapping. OJB requires some internal table to perform Acta, such as maintaining database locks, automatically increase

Long, ODMG Collectes and Maps. The corresponding mapping is also achieved here. They all work correctly

Use, it should not be modified.

These internal mappings are defined by XML Entity & INTERNAL in repository_internal.xml

5. User-defined mapping. User-defined mappings contain all objects / relationship mapping information made by the storage class. default

The mapping of the instance program is defined here. These mappings are through XML Entit, including repository_user.xml

Y & User is defined.

Now let's take a look at the Product class, I put the corresponding map in the repository_user.xml so that we are very

Capacity

Find. From the beginning of the file we can see a class description for the Product class:

Class = "org.apache.ojb.broker.product"

Table = "Product"

>

Name = "id"

COLUMN = "ID"

JDBC-TYPE = "ingeger"

PRIMARYKEY = "True"

AutoIncrement = "True"

/>

Name = "name"

COLUMN = "Name"

JDBC-TYPE = "varchar"

/>

Name = "price"

COLUMN = "price"

JDBC-TYPE = "double"

/>

Name = "stock"

COLUMN = "stock"

JDBC-TYPE = "ingeger"

/>

Class description gives a class name that maps the RDBMS table.

For the properties of each storage class, we define a field-descriptor, each corresponding to each of the database

Column. For the primary key, we have a special mark: primarykey = "true". The primary key is also set to Autoi

ncrement = "true", which tells OJB to assign this property to this property a unique ID. These are all implemented through a SequenceManager tool class.

Field-Descriptor can declare the Primitive Data type or declare the corresponding package class.

If you contain an instance of other storage classes in your storage class, you can define it via REFERENCE-DESCRIPTOR.

The relationship between them. If you have Array- or Collection-Properties in the store, you can use Collection-

Descriptor properties. You can find some examples in the org.apache.ojb.broker package, the corresponding description is in R

Epository.xml.

Tool support:

Generally speaking, there are three ways to establish O / R mapping:

1. Forward Engineering. Generate SQL DDL and mapping relationships by giving a Java class (or UML model)

. We can use a tool to generate a database from repository.xml. Execute build [.sh] forword

-db can see the work process of Forward Engineering.

2. Reverse engineering. Give the SQL DDL or database to generate Java classes and mappings. Execute build [

.sh] Reverse-DB can see the work process of Reverse Engineering.

3. Mapping Proper. Give the Java class and SQL DDL to generate mapping. We have not tested yet.

But it has been included in the next plan.

in conclusion:

In this article, we have learned how to create an OJB object / relational map to a simple class, pass on the instance of the class.

The OJB PersistenceBroker API is stored.

Then there are three documents that introduce how to use OJB ODMG API, OJB JDO API and advanced O / R mapping (including

1-1, 1-N mapping, proxy mechanism, support polymorphism and mapping inheritance)

We hope that the document will help you, welcome to suggest advice.

转载请注明原文地址:https://www.9cbs.com/read-6250.html

New Post(0)