Solve the relationship between hibernate tables and tables, and understand the attributes such as cascade, inverse, and lazy.

Source: Internet
Author: User

Two classes: Book and booktype)

Public class book {
Private long ID;
Private booktype type;
}

Public class booktype {
Private long ID;
}

Okay, let's start.

Scenario 1

They are not correlated.

Case 2

/**
* @ Hibernate. Convert-to-one column = "col_booktype_id"
*/
Public booktype GetType (){
Return type;
}

That is to say, a class has multiple books. For example, many books are historical books and many others are mathematical books.

So:

Booktype = new booktype ();

Book book1 = New Book ();
Book1.settype (booktype );

Book book2 = New Book ();
Book2.settype (booktype );

Bookservice. Save (book1 );
Bookservice. Save (book2 );

Create an example of "books" and create two more books. They all belong to this book. Save the two books and the results are incorrect. Why? Because booktpye is not saved, the category fields of the two books cannot be saved. Cascade is now available --
Cascade has two levels: cascade = "Save-Update" cascade = "delete". Let's first look at Cascade = "Save-Update"

/**
* @ Hibernate. Upload-to-one column = "col_booktype_id" cascade = "Save-Update"
*/
Public booktype GetType (){
Return type;
}

At this time, the booktype can be automatically saved when book1 is saved. When book2 is saved, it is found that booktype already exists, so the booktype is updated. In short, it must be cascade.

So, in fact, this function is a relatively lazy function.

Now let's take a look at Cascade = "delete"

If all the above are saved, delete book1, OK ...... Then delete book2. At this time, it will first Delete booktype and then delete book2. This is what it means. Hibernate will think that you have no history books. Why should I use the history books? So they are all deleted.

Case 3

Now we have a new requirement. We need to get the book through booktype, such as getbooks ("History ")
Returns a set of books. In the absence of hibernate frameworks, we must first
In the booktype table, find the historical ID, and then use this ID to go to the book and find these books. Of course, with hibernate, this is actually done internally, only
It greatly simplifies our coding workload. Good! So of course this belongs to one-to-one, and we set it in booktype.

<Set name = "books" lazy = "false" inverse = "true" cascade = "all">
<Key column = "col_booktype_id"> </key>
<One-to-learn class = "HDU. Management. library528.entity. Book"/>
</Set>

Here we will explain the attributes one by one. The set here does not actually correspond to any tables and fields in the database. First of all, we need to clarify this.
When lazy is set to true, we get a history class that does not read all the books in this class, but reads the books only when it is used.
When it is set to false, we get a history class, which has encapsulated books and read them all.
That's easy to understand, right? Lazy means laziness. Note that when we use spring for transaction operations, lazy = "true" is prone to errors. Solution:
1. if efficiency is not considered, you can simply set lazy to "false"
2. Refer to the spring interceptor opensessioninview.

Okay, let's focus on it. First, let's look at Cascade = "all"
This is not really difficult to understand. It is the same as objective-to-one. In our understanding, which class is Cascade written in? A: booktype. Which class is it playing?
Ming? A: Here we declare the book. Well, this means that when we delete the booktype and update the addition, we need to operate the book without having.
For example, in Case 2, we cannot delete the booktype because there is a foreign key associated with it, so here we set one-to-least, OK, when we delete the booktype, we first delete all the related books and then delete the booktype.

Finally, let's talk about inverse = "true". When I first came into contact with hibernate, I had the biggest headache with inverse and cascade. Now cascade has understood it very clearly? So I will summarize the inverse
1. Concept: specify whether or not to maintain the relationship on your own. (You don't need to understand it)
2. Set inverse = "true" in one-to-second mode.
Third, set inverse = "true" at any end when copying-to-sequence, and set inverse = "false" in another place"
Fourth, don't ask me: "I can't set it like this. Please try another way to set it." cancel the thought and return to articles 2 and 3.

Case 4

Now we have new requirements.
There are many books under a book category. In reality, a book may belong to many categories. For example, it belongs to both historical and geographical categories. In this case, use Upload--
Stopped. In the previous relationship, we used two tables to maintain the relationship between them. When there are many to many tables, we need to build a table to maintain it.
Here we construct a book-book class table with two fields, book id and book class ID, which are automatically converted to the Union primary key when mapped to Oracle, no duplicates.

/***//**
* @ Hibernate. Set table = "lib_book_booktype" lazy = "false" cascade = "all" inverse = "true"
*
* @ Hibernate. Collection-key column = "col_book_id"
* @ Hibernate. Collection-ignore-to-duplicate column = "col_booktype_id" class = "HDU. Management. library528.entity. booktype"
*/

Public set getbooktypes ()...{
Return booktypes;
}

/***//**
* @ Hibernate. Set table = "lib_book_booktype" lazy = "false" cascade = "all"
*
* @ Hibernate. Collection-key column = "col_booktype_id"
* @ Hibernate. Collection-ignore-to-duplicate column = "col_book_id" class = "HDU. Management. library528.entity. Book"
*/
Public set getbooks ()...{
Return books;
}
Note: Do not forget private set books = new hashset ();

The setting is the same as the above. The only difference is that there is an extra table, and the column will naturally point to that table, in fact, the two tables book and booktype have a peaceful chart, and no foreign keys are available, but their Relational Tables are controlling them.

We continue to address the problem of inverse in many-to-many scenarios. Here we set inverse = true booktype to inverse = false when we do this operation.

Book1.getbooktypes (). Add (booktype1 );
Book1.getbooktypes (). Add (booktype2 );
Book1.getbooktypes (). Add (booktype3 );

Bookservice. saveorupdate (book1 );

Book1 is saved, and booktype1, 2, and 3 are saved, but their relationships are not saved. Why? Because book
The link inverse = true is kicked to others for processing, so it ignores the link processing. Therefore, we need
The booktype operation can process the relationship. Of course, if we set both ends
If inverse is set to false, all operations can be performed. For efficiency considerations, please wait ...... Don't worry about it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.