Hibernate depth adventure!

xiaoxiao2021-04-02  248

I. Hibernate 1) Hibernate in a real OOAD, our design is first to do UML build, and finally put a system involved all objects (this thing is not so simple) use class diagrams to reflect a complete design, We will finally get these types: Control business logic class, save business data class module (bean class), secondary class or more (specific analysis of specific issues, but the business required data is more suitable for a class MODULE Layered). When the database is low-level implementation, in order to obtain data or store data, you have to add a control logic for the database, here, your perfect design estimate will pay great efforts to this, because you see the business The data layer is a complex module, even from the object-oriented point of view, our UML class diagram, the service data layer is just a data module. Hibernate has helped us solve the underlying implementation of this very complex module of the business data. Now, we can only wrap the class representing the data in the outer layer. II) The difference between the object model and the relational database model is written before I preliminarily feel the feelings of Hibernate, I think it is necessary to write this section. It is far more than a curiosity research. Problem: Relational database is the best choice for storing data, but as OO technology is growing, in the Persisitent Layer, the design system and OO system is inconsistent, you can imagine, when you are full of brains, what do you think of how you think about OOAD? What painful expression is that the SQL statement that is full of flying. Regardless of your business layer design, when you really save data or loading data, you face it is nothing more than a lot of packaged data, which has completely lost objects in JDBC (here the object is called business objects) Perhaps more exactly), your overall OOAD is here. Why do you cause this? The reason is the difference between the object model and the fundamental design system of the relational database model. Object model and relational database model Theoretical starting point is different: the theoretical system of object models can be simply concatenated to these two points: 1) look at the world with an object. 2) Inter-object relationship (inheritance, association, aggregation, combination) is maintained in whole. The only starting point of the relational database model is effective storage data. Key is a key technology of the database. The relationship is only the association between the key of the KEY of each data table. I think it should be called the association of the data. The meaning of expression is far away. The relationship between the absence of the object is so deep. So, what is the most concerned about how hibernate uses the relationship database of data table KEY associations to express the relationship between objects? Before entering the formal study hbernate, we can think that the problem seems to be simple and seemingly complex contradiction. All of our design representative data must be perfectly reflected in the data table. Can summarize this: class-àtable class1- (relationship) --- Class2 ------> Table1 --- (Relation) ----- Table2 problem Solution seems to be very simple, especially for JavaBean architectures, more It is simple (it is simple !!!). Imagine a simple Javabean class:

Java code:

public

Class SimpleBean

{

protected

Int ID;

protected

String name;

public

Int getID

(

)

{

Return ID;

}

public

Void setID

(

INT ID

)

{

THIS.

ID = ID;

}

public

String getname

(

)

{

Return Name;

}

public

Void setName

(

String name

)

{

This, Name = Name;

}

}

We can perform this Name mapping like this: classname-àtablename property àcolumnname A class instance is a line of Table. This problem is very simple to solve. Further, consider the following simple one-on-one association:

Java code:

public

Class class1

{

PUBLIC CLASS2 CLASS2;

Public class2 getclass2

(

) ...

public

Void setClass2

(Class2 Class2

) ...

}

public

Class class2

{

Public class1 clas1;

Public class1 getclass1

(

). .

. . .

}

This relationship is obviously two-way, can get Class2 from Class1, in turn, can also get Class1 from class 2, is it reflected in the data table? First, you can affirm class1àtable1, class2àtable2; it is clear that both Table1 and Table2 add more than one column to save each other's Key. These simple relationships have been well supported in the association of the database table, but is a little more complicated? Such a class:

Java code:

public

Class S

{

ArrayList Data;

Public

List getDataS

(

)..

Public

Void setDataS

(

List Datas

)..

.

}

Here, if the PropertyName-àcolumnname analyzed above is obviously not possible, this collection makes a bean property we have been very well reflected in the data table? If these collections are just a simple String collection, how is it expressed in the database table? If these collections are saved, it seems to be converted to a pair of relationships with a database table? On the other hand, how does the inheritance system are reflected in the data block table? How to express the relationship between the relationship of the inheritance? How to reflect the dynamic class recognition involved in the database? I want to think about it, for one action:

Java code:

public

Class bookstore

{

SET books;

Public

Set getboos

(

) ..

Public

Void setBooks

(

SET BOOS

) ...

Public

Void Addbook

(

Book book

) ...

public

Class

Book

{

Public bookstore bookstore;

Public Parent GetbookStore

(

) ..

.

}

In business logic, we will write code this:

Java code:

Book book =

New

Book

(

);

.

Bookstore.

Addbooks

(BOOK

);

The above two lines of code have clearly established the relationship between Child and Parent. Relatively, the data in the database should also establish data and establish this association according to these lines of code. How does data in memory at this time are consistent with the data in the database?

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

New Post(0)