Persistent API (JPA) series (eight) Entity Relationship Mapping (ORM) Single-table mapping @embeddedid

Source: Internet
Author: User

next to the "Persistent API (JPA) series (vii) Entity Relationship Mapping (ORM) Single-table mapping @idclass"

This article describes the Federated primary key: Using @embeddedid to embed an external primary key

The above is a reference to the external primary key via @idclass, and the Federated primary Key can also be used Embedded PRIMARY Key Alternative.


1) New external primary key class Family2.java

Set the federated primary key man and woman, so the external primary key also needs to define two identical variables, and add a constructor with two variables as input, plus the Getter/setter function.

The primary key class must satisfy:

1. Must implement the Serializable interface, must have the default public parameterless construction method

2. You must override the Equals and Hashcode methods, which are the same requirements as using composite primary keys.

Using the @embeddable callout for the primary key class indicates that this is an embedded class.

Package Com.tgb.itoo.exam.entity;import Java.io.serializable;import javax.persistence.embeddable;@ Suppresswarnings ("Serial") @Embeddablepublic class FAMILY2PK implements Serializable {private String man;//husband private String woman;//Wife public family2pk () {}public family2pk (string man, String woman) {This.man = Man;this.woman = woman;} ....//functions in the same Family.java}

2) Embed the external primary key in the question Bean class Family2.java using @embeddedid

Use the annotation character to set the mapping relationship to the table and field.

This class has the following characteristics:

1. The mapped table name is Family2.java, which is intended to be distinguished from the Family.java in the demo above

2. The instance ID of the external primary key FAMILY2PK is embedded by the @embeddedid tag, indicating that the embedded primary key variable

3. Add a parameterless and parametric constructor, respectively, to an instance of the common ID.

4. In the Getman (), Setman (), Getwoman (), Setwoman () functions, reference the value of the ID, respectively.

Package Com.tgb.itoo.exam.entity;import Java.io.serializable;import Javax.persistence.column;import Javax.persistence.embeddedid;import Javax.persistence.entity;import Javax.persistence.id;import Javax.persistence.idclass;import javax.persistence.Table, @SuppressWarnings ("serial") @Entity @table (name= "Family2 ") public class Family2 implements Serializable {@EmbeddedIdprivate family2pk id;//embedded primary key private String address;//address// Initialize public Family2 () {this.id=new family2pk ();} Public Family2 (String man,string woman) {this.id=new family2pk (Man,woman);} @Column (name= "man") public String Getman () {return This.id.getMan ();} public void Setman (String mans) {This.id.setMan (man);} @Column (name= "woman") public String Getwoman () {return This.id.getWoman ();} public void Setwoman (String woman) {This.id.setWoman (woman);} @Column (name= "Address", length=100) public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

3) New Remote interface class Family2daoremote.java

Define two interfaces: New, query based on primary key

Ibid . article:familydaoremote.java

4) Develop and implement class Family2dao.java

1. First construct a primary key object family2pk
2. Call the Find () method to query against the primary key object

Ibid . article:familydao. Java

5) Test: Client Call

Ibid . article: Family2daoclient.java

============================================================================

Summarize

The main difference between using the @idclass to specify the external primary key and using @embeddedid to embed the external primary key is the two demo, which is the key differences between the two files: Familypk.java primary key class and Family.java, followed by a comparison:




Persistent API (JPA) series (eight) Entity Relationship Mapping (ORM) Single-table mapping @embeddedid

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.