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

Source: Internet
Author: User

from previous articles, we learned that @table, @Column, @Id implemented a single-table mapping, and the book Sword has a unique field specified by @id. Sometimes our data tables may be composed of multiple primary key unions, so for the primary key of a single table map, there are two types of federated primary key mappings available.
Federated PRIMARY Key: Use @idclass to specify an external primary key
Federated PRIMARY KEY: Embed external primary key using @embeddedid

The following is an example of how these two primary keys are developed.
Demo: Design a family table family data structure

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

(i) Federated primary key: Use @idclass to specify external primary key

steps:
1. Establish a primary key class: The primary key field corresponds to the class
2. Reference the class in the entity bean through the @idclass annotation character

To implement a reference to the external primary key.


1) new external primary key class Familypk.java
Because family sets the federated primary key man and woman, the external primary key class FAMILYPK also needs to define two identical variables, add a constructor that takes both variables as input, and add the Getter/setter function.
as an external primary key:
1. The serializable interface must be implemented
2. There must be a default public parameterless construction method
3. The Equals () and hashcode () methods must be overridden. The
Equals () method is used to determine whether two objects are the same, and Entitymanager finds the entity through the Find () method, judging by the return value of equals (). In this case, return true only if the object's man and woman values are exactly the same or belong to the same object, otherwise false. The
Hashcode () method returns the hash code for the current object. The smaller the probability of generating hashcode () the better, the algorithm can be optimized.
Package Com.tgb.itoo.exam.entity;import java.io.Serializable, @SuppressWarnings ("Serial") public class FAMILYPK Implements Serializable {private string man;//husband private string woman;//wife public string Getman () {return man;} public void Setman (String mans) {This.man = man;} Public String Getwoman () {return woman;} public void Setwoman (String woman) {This.woman = woman;} Public familypk () {}public familypk (string man, String woman) {This.man = Man;this.woman = woman;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + (man = null)? 0:man.has Hcode ()); result = Prime * result + ((woman = = null)? 0:woman.hashcode ()); return result; @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; FAMILYPK other = (FAMILYPK) obj;if (mans = null) {if (Other.man! = null) return false;} else if (!man.equals (Other.man)) ret Urn False;if (woman = = null) {if (Other.woman! = NULL) RETUrn false;} else if (!woman.equals (Other.woman)) return False;return true;}}

2) Use @idclass to specify the external primary key in the Entity Bean Class Family.java.
Sets the mapping relationship with the table and the field by using the annotation character.
Note that you need to label the Federated Primary Key in this entity:
1. Add the @id comment before the getter function of man and woman, which means the primary key
2, in the class name money using @idclass reference external primary key class
package Com.tgb.itoo.exam.entity;import Java.io.serializable;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.id;import Javax.persistence.idclass;import Javax.persistence.Table, @SuppressWarnings ("serial") @Entity @table (name= "Family") @IdClass (Familypk.class) public Class Family implements Serializable {private string man;//husband private string woman;//pawn private string address;//address @idpub Lic String Getman () {return man;} public void Setman (String mans) {This.man = man;} @Idpublic String Getwoman () {return woman;} public void Setwoman (String woman) {This.woman = woman;} @Column (name= "Address", length=100) public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}} 

3) New Remote interface class Familydaoremote.java
Define two interfaces: New, query based on primary key
Package Com.tgb.itoo.exam.service;import Javax.ejb.remote;import com.tgb.itoo.exam.entity.Family; @Remotepublic Interface Familydaoremote {//New public boolean insert (Family Family);//Inserting Public Family selectbypk (String man, String Woma n);}

4) Develop and implement class Familydao.java
1. First construct a primary key object familypk
2. Call the Find () method to query against the primary key object
Package Com.tgb.itoo.exam.papermanage.serviceimpl;import Javax.ejb.stateless;import Javax.persistence.entitymanager;import Com.tgb.itoo.exam.entity.family;import Com.tgb.itoo.exam.entity.FamilyPK; Import Com.tgb.itoo.exam.service.FamilyDAORemote; @Statelesspublic class Familydao implements Familydaoremote { protected Entitymanager em; @Overridepublic boolean insert (Family Family) {try {em.persist (Family);} catch (Exception e) { E.printstacktrace (); return false;} return true;} @Overridepublic Family selectbypk (String man, String woman) {familypk EPK = new FAMILYPK (man, woman); return Em.find (Family . class, EPK);}}

5) Test: Client Call

Package Com.tgb.itoo.exam.papermanage.serviceimpl;import Java.util.properties;import Javax.naming.InitialContext; Import Javax.naming.namingexception;import Com.tgb.itoo.exam.entity.family;import Com.tgb.itoo.exam.service.familydaoremote;public class Familydaoclient {public static void main (string[] args) throws namingexception {//..... InitialContext ctx=new InitialContext (); Familydaoremote familydao= (familydaoremote) ctx.lookup ("Familydao/remote");//Add Family Family=new family (); Family.setman ("husband"), Family.setwoman ("wife"), Family.setaddress ("Address"), Familydao.insert (family);//Query Family family2= FAMILYDAO.SELECTBYPK ("Husband's name", "wife's name"); System.out.println (Family2.getaddress ());}}

The following demo shows the Federated primary key: Using @embeddedid to embed the external primary key


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

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.