Inconvenience caused by hibernate's primary key combination)

Source: Internet
Author: User

Inconvenience caused by hibernate joint primary key (transfer) and hibernate

Note the following when using the hibernate joint primary key:
1. First, write the Union primary key in the ing file,
Example:

Java code
  1. <Composite-id name = "sid" class = "cn. test. po. IpAndSubphoneId">
  2. <Key-property name = "ip" type = "java. lang. String">
  3. <Column name = "IP" length = "50"/>
  4. </Key-property>
  5. <Key-property name = "subphone" type = "java. lang. String">
  6. <Column name = "SUBPHONE" length = "50"/>
  7. </Key-property>
  8. </Composite-id>
The <key-property> label in the Union primary key is similar to the <property> label, but must be written in the <composite-id> label.

2. Fields in the Union primary key should not have null values.
In actual development, I found that if a field in the joint primary key is null, the query results of the Union primary key will be null, I don't know whether it is a hibernate bug or a problem generated when spring is integrated with hibernate. In short, it is best to ensure that the fields used as the joint primary key have values in actual use.
3. Primary keys are prone to conflicts.
Another drawback of hibernate's primary key Union is that there is a hidden risk of duplicate primary keys. If several fields are used as the primary key Union, these fields may be duplicated after being updated, however, no error is reported in the database (there are other different fields). However, hibernate reports the primary key repetition error.
4. Difficult to use
Sometimes you only need to query a field, but you need to query it by using the joint primary key containing N fields. This is not a headache for the people.

We recommend that you use less federated primary keys to avoid unnecessary troubles, unless you cannot find a method without federated primary keys.

This article

Related Article

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.