JPA application Tip 2: ing of primary key external bonding bodies

Source: Internet
Author: User

Consider two object classes with one-to-one association. In addition to having a primary key, the owner also has a foreign key that references the primary key. If the relationship between the master and the prosecution is the same, in other words, once the one-to-one relationship is established, it cannot be changed, so that both parties can share the same primary key, simplify the structure of the table under prosecution. The following example shows how to use JPA 2.0 to implement this ing.

Staring at the current computer, the landlord thought of a perhaps less appropriate example: the relationship between employees and the company's computer. The employee's primary key is the employee ID. Although the computer may be replaced, the computer entity can use the employee ID as the primary key, only to get rid of the detailed information of the computer configuration. The difficulty here is how to map the primary key field of the computer to an employee at the same time. Please see how the landlord derives it step by step.

At the beginning, it was the most common method:

? 1
2
3
4
5
6 public class Computer implements Serializable {
@ Id
@ OneToOne
Private Employee employee;
// Some rows are omitted here
}

However, according to the specification, only these types can be used as primary keys: Java primitive types (such as int), primitive packaging types (such as Integer), String, java. util. date, java. SQL. date, java. math. bigDecimal and java. math. bigInteger. Therefore, it is impossible to directly use Employee as the primary key. By the way, some JPA implementations may have made their own extensions so that they can directly use the entity class as the primary key, which is beyond the scope of the JPA specification and will not be discussed here.

Direct ing does not work. Is there any indirect method? The poster remembered a special note: EmbeddedId. This annotation is intended to be used for joint primary keys. However, can I wrap the Employee into an embedded primary key and then use the embedded primary key as the primary key of the Computer? With this idea, the landlord has the following code:

? 1
2
3
4
5
6 @ Embeddable
Public class ComputerId implements Serializable {
@ OneToOne
Private Employee employee;
// Some rows are omitted here
}

? 1
2
3
4
5 public class Computer implements Serializable {
@ EmbeddedId
Private ComputerId id;
// Some rows are omitted here
}

Now there is a new problem: JPA does not support association ing defined in the embedded primary key class. Fortunately, there is no path to perfection. The EmbeddedId document directly states that MapsId annotation can be used to indirectly specify the association ing in the embedded primary key class, and an example is provided! So the final finished product is released:

? 1
2
3
4
5 @ Embeddable
Public class ComputerId implements Serializable {
Private String employeeId;
// Some rows are omitted here
}

? 1
2
3
4
5
6
7
8 public class Computer implements Serializable {
@ EmbeddedId
Private Employee employee;
@ MapsId ("employeeId ")
@ OneToOne
Private Employee employee;
// Some rows are omitted here
}

The only pity is that the logic master prosecution Employee has to be under prosecution now. Fortunately, cascade operations can be defined to achieve the same effect:

? 1
2
3
4
5
6
7 public class Employee implements Serializable {
@ Id
Private String id;
@ OneToOne (mappedBy = "employee", cascade = CascadeType. ALL)
Private Computer computer;
// Some rows are omitted here
}

Although it is done, it is really around. I hope that the future version of JPA can directly support the entity class as the primary key. I personally think it is not a technical problem.

Author "magic and hope"
 
 

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.