Hibernate---Federated primary Key Association

Source: Internet
Author: User

The dominant party wife has two primary keys:

Package Com.bjsxt.hibernate;import Javax.persistence.entity;import Javax.persistence.id;import Javax.persistence.IdClass; @Entity @idclass (wifepk.class) public class Wife {private int id;private String name;private int age;public int Getage () {return age;} public void Setage (int.) {this.age = age;} @Idpublic int getId () {return ID;} public void setId (int id) {this.id = ID;} @Idpublic String GetName () {return name;} public void SetName (String name) {this.name = name;}}

Wifepk.java:

Package Com.bjsxt.hibernate;import Java.io.serializable;public class WIFEPK implements Serializable {private int id; Private String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}

Leading party Husband.java:

Package Com.bjsxt.hibernate;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.id;import Javax.persistence.joincolumn;import Javax.persistence.joincolumns;import Javax.persistence.OneToOne; @Entitypublic class Husband {private int id;private String name;private Wife wife;@ id@generatedvaluepublic int getId () {return ID;} Public String GetName () {return name;} @OneToOne @joincolumns ({@JoinColumn (name= "Wifeid", referencedcolumnname= "id"), @JoinColumn (name= "Wifename", Referencedcolumnname= "name")}) public Wife Getwife () {return Wife;} public void setId (int id) {this.id = ID;} public void SetName (String name) {this.name = name;} public void Setwife (Wife Wife) {this.wife = Wife;}}

Xml:

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate.                 Sourceforge.net/hibernate-configuration-3.0.dtd ">

UnitTest:

Package Com.bjsxt.hibernate;import Java.util.date;import Org.hibernate.query;import org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.tool.hbm2ddl.schemaexport;import Org.junit.afterclass;import Org.junit.beforeclass;import Org.junit.test;public class Hibernateormappingtest {private static sessionfactory sessionfactory;//@ Beforeclasspublic static void Beforeclass () {sessionfactory = new annotationconfiguration (). Configure (). Buildsessionfactory ();} @AfterClasspublic static void Afterclass () {sessionfactory.close ();} @Testpublic void Testschemaexport () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (False, True) ;} public static void Main (string[] args) {beforeclass ();}}

  

  

Hibernate---Federated primary Key Association

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.