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

zhaozj2021-02-12  163

Define object / relationship mapping:

In the source code and database of the example program (running bin / build browse -db will start instantdb data)

After the library browsing window), you may ask: We don't use any code about data storage in the program.

How is OJB Broker to implement the information of the Product class to the Product table? How is OJB know

The NAME column in the database is mapped to the NAME attribute?

The answer is that everything is done in the OJB metabase. The library contains some classes to describe O / R mapping (you can participate

Take the Org.apache.ojb.broker.metadata package). The library contains some universal Java objects, these objects can

Enough is dynamically created and modified. This provides a lot of convenience to those who need to dynamically change mapping. Guarantee

Hold the state map with the following benefits:

1. No processing of Java source code does not require the compilation of the repository.

2. The mapping can be checked, change, allowing to maximize storage behavior or built on OJB.

Human storage layer.

But there is also a disadvantage: efficiency problem. OJB's dynamic approach uses Java reflex mechanism and JavaBean access

Mechanism to check and modify business objects, we must minimize the number of dynamic access.

In the following chapter, we will explain how the instance program uses O / R mapping.

A store: Product

There is only one storage class in our instance program, the Product class, below is its definition:

Package org.apache.ojb.tutorial1;

/ **

* Represents Product Objects in the tutorial system

* /

Public Class Product

{

/ ** Product name * /

Protected string name;

/ ** price per item * /

Protected Double Price;

/ ** stock of currently available items * /

Protected int stock;

...

}

I delete the method definition, they have nothing to do with the O / R mapping process.

Product table in the database:

Let us look at the table now, use SQL DDL to define (I gave instantdb syntax, in different RDBM

Symphics may have different signs):

CREATE TABLE PRODUCT

ID INT Primary Key,

Name char (100),

Price Double,

Stock int

)

You may find that I added a column ID and set it to the primary key. This is hand-added, it does not belong to the Prod in the program.

UCT. If you use manual join, you must add this property in your product class. OJB requires all tables

The primary key column must be mapping in the corresponding Class. This is also one of the few rules that need to be observed in the storage layer, because

These properties are not part of the OOD model.

So we must modify our class:

Public Class Product

{

/ **

* this is the primary key attribute needed by ojb to

* Identify Instances

* /

PRIVATE INT_ID;

/ ** Product name * /

Protected string name;

/ ** price per item * /

Protected Double Price;

/ ** stock of currently available items * /

Protected intount;

There is no other restriction in addition to the main key requirements. There is no need to extend the base class to implement all the interfaces, this

Why do we say that OJB is the cause of transparent storage.

Very important point: The storage class must have a public non-parameter constructor.

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

New Post(0)