Hibernate Association Map (3) 2016-07-12

Source: Internet
Author: User

Next, start a one-to-many-way association. Since many to one one-way association already said in (1), no longer said, the main idea is to use a one-to-many more-to-one a special form, the use of unique= "true" into a special pair to use.

Description: One-to-many still use the person and address examples. In the case of a one-to-many one-way association, a person can have more than one address, so there is a member variable for the Set collection property in the person class, and the collection contains a variable of type address.

One-to-many foreign key unidirectional association

Because a person can have multiple address, it is not possible to add an address to the person table, but instead add a column in the Address table that holds the Preson primary key as the foreign key for address. (This consensus can also be achieved from the uniqueness of the primary key)

It is recommended to paste the two configurations into two txt, comparing

One-to-many foreign key unidirectional affinity configuration (see below)


One-to-many connection table configuration (below)


One-to-many foreign key unidirectional association ******

Similarly, because it is a one-way association, address does not have to pay special attention, is the normal understanding.

On the person table, a line of view, the front are common writing, it is easy to understand, the key is to <set> tags how to understand.

Because there is a set set in the Person class, the Hibernate designer calls the tag set, and when hibernate reads the tag, it knows that the table being configured now conforms to a one-to-many model and is one of the one-to-more. (It is very simple that only one side will store more of the party as a set set)

What hibernate does not know is whether the pair uses a foreign key or a join table, one-way or two-way.

So hibernate continues to read, see Name and table on the general know, in the future I would like to query the table of association, the resulting results are assigned to the set set of variables called addresses.

Now that hibernate knows about the associated query, it's important to use the table's primary key to establish a bridge with the other tables, so hibernate is smart enough to build a column in the table in the table= "" quotation mark, storing the table's primary key value, so all we have to do is give it a name, This will require a column.

The next step is to use the one-to-more tag. In fact, if you do not see the following configuration using the connection table, there may be no doubt about this tag, is a person to use multiple address, so is the one-to-more tag.

Here, I want to say what I found the law: like above I said, hibernate see Set label, just know to correlate query, but do not know whether it is a foreign key query or join table query (you can finish writing set, press alt+/ Look at the hints One-to-more and More-to-more are available). So, to stipulate, because we know I want to use the foreign key way or the way to join the table, so according to our own needs, if it is a foreign key method, use One-to-more, if it is a connection table with More-to-more.

If the write is reversed, although syntactically no problem (I think), but in fact hibernate at runtime can know whether you use the foreign key or the connection table (or I think it will know, at least logically can achieve such a judgment), This is because we have the class attribute in the One-to-more or more-to-more tag, and if Hibernate is based on class, find out whether the class has created a corresponding table, and then see what the table is called, and compare it with the name of the tables in the set tag. If the name is the same, that is the foreign key method, if not the same, it means that the connection table is used.

In short, remember the law is good: if it is a foreign key method, use One-to-more, if the connection table is used More-to-more.

One-to-many-connection-form association ******

With the above basis, the connection table is good to understand, or to set set, the specified table is the name of the connection table, the same key is written in the join table in the table of the primary key column is the name of what is called, according to the rules above, we are the way to join the table, with More-to-more, Because is the connection table way, now the connection list has PersonID, also need corresponding ADDRESSID, this is more-to-more in the meaning of column, unique= "true" More-to-more into One-to-more.

Some thinking outside the *******

Cranky, now a little confused don't look at it * * * *

As a user, you can see here, understand the rules and procedures, at least it does not seem to be confused, and there are a few small rules mastered, write better.

In the first article regrets, build a want to hibernate the same framework is a thinking of the game, as long as their own thoughtful enough to realize the function, self-justification is OK, so in the use of other people's framework when they like to think of these things, Will guess what rules the designer is based on to constrain or guide the program to judge and run.

It's like here (we recommend that you paste the two configurations into two txt, and look at the comparison)

As designers in the design of hibernate, will certainly want to make it better use: For example, you see, I can help you find data, but also can be found in the data based on the entity classes automatically encapsulated into the class of objects, useful. Of course, this has to have a premise, the first class inside to write the Get/set method, second, the table has a corresponding class, the corresponding relationship is embodied in the configuration. So as the designer, I want to know you which entity class and which table is corresponding, I have to force you to write to me the configuration file, but also according to my rules, so that I can analyze it well, you can use the convenience.

So, when it comes to simplifying the relationship between maintenance tables and tables, you have to think about how to design the rules of the configuration file because of the too many involved.

For example, use more than one address for the current person.

The designer, as an experienced programmer, sees the set set and knows either a pair or many-to-many. So the design of the label called Set,name= "addresses" is to tell hibernate to find addresses variables, see if it is set set, there is no corresponding Get/set method. And, either a one-to-many or many-to-many table relationships, you have to look up the table, whether you're looking at the Address table or the connection table, you have to look up the table, that line I put the table property in the set tag.

But by this time, I designed hibernate still can't determine whether you are a more or more, but I don't care about that, you have to establish a connection between two tables to correlate the query ah, so whether you write table name is the Address table or the connection table, You have to use the primary key of the person table as the foreign key to the table represented by the tables, so that you can relate the query. And, I know it must be the primary key of the person table as the foreign key column, so you do a specific label on the key bar, you do not have to write which column as a foreign key column, I know it is the primary key column, you tell me that my hibernate help you put the primary key in the table after the name of the list. So you can just write the column in key.

Now whether it is a one-to-many or many-to-many, your common part I have to specify the rules, such as the table Ah, column name Ah column, now it is necessary to determine whether you are a pair or many more, by One-to-more and More-to-more to distinguish.

If you're writing one-to-more, well, you're using a one-to-many, foreign-key Association (many-to-many can only pass through the join table, and a one-to-many connection table association is a many-to-many exception).

You use a one-to-many foreign key association, then your table= "ADDRESS_1NFK", this ADDRESS_1NFK is the ADDRESS table. And I as the designer, have to consider a variety of situations, such as maybe he configured the wrong table name, did not find the table name, this simple, direct error. Another embarrassment, found ADDRESS_1NFK this table name, but this table is not the address entity class, I as the designer must consider this situation, so I add the class attribute in the One-to-more tag. If can not find table corresponding tables OK, direct error, if found, also have to wait, I have to judge, you this table corresponding entity class is not class= "" class. If this verification is successful, well, I'm sure you are the Address Table I'm looking for, I'll add a column of PersonID, as a foreign key, then I will associate PersonID with your Address table to query.

If you write More-to-more, then my hibernate will know that you are many-to-many, though you may be able to turn More-to-more into an actual one-to-more through the unique= "true" I designed, but it doesn't matter, The key is that you are associating the query with a connection table. As a join table, you have to have the primary key for your person table and the Address table.

So the same reason, with the key tag. However, the primary key of the Address table is set into the connection table, because it is hibernate after you have written the more-to-more tag to know is to use the connection table, before this can not set the connection table, so it is set in the More-to-more tag, There is a column= "Addressid", which is added to the join table in a column. But my hibernate only knew to add a column, and my hibernate already knew is the connection table, certainly the table is the main key of the tables, the only problem is not know addressid this column of data from where. So we have the class attribute. My hibernate finds classes corresponding to class, and then see if the class has created a corresponding table, and some of the table's primary keys are taken to the Addressid column of the join table.

The class here does not have the function that helps hibernate to judge in the above One-to-more, because after using More-to-more, Hibernate knows to use the connection table, will see table= "Join_1ntab" Whether the name is occupied by another table, if any (there are two options, whether to overwrite or select the error), if not, then my hibernate will help you create such a connection table.


Below is the reference material:

http://lavasoft.blog.51cto.com/62575/39398/strongly recommended to see first, Bowen wrote 14 cases of the configuration, I just saw this again combined with other data exploration of the law
Http://blog.sina.com.cn/s/blog_62f0eaa80101bpaf.html
Http://blog.sina.com.cn/s/blog_62f0eaa80101bpah.html
http://blog.csdn.net/sanjy523892105/article/details/7061602
http://blog.csdn.net/linminqin/article/details/6324567
http://blog.csdn.net/jialinqiang/article/details/8704538


Hibernate Association Map (3) 2016-07-12

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.