Hibernate operation dual primary key + dual foreign key

Source: Internet
Author: User

For the role personnel table above, how can we use hibernate to implement ing, because the two fields in this table are the primary keys of the other two tables, in this table, even if the primary key is a foreign key, the following describes how to operate this table.
Step 1: first generate the employee information table and the javaBean and. hbm. xml files of the role management table. This is a single table, which is not mentioned here;
Com. insigma. hr. eduj. ygxx. model. Ygxx. java,
Com. insigma. hr. eduj. ygxx. model. Ygxx. hbm. xml
Com. insigma. hr. eduj. jsgl. model. Jsgl. java,
Com. insigma. hr. eduj. jsgl. model. Jsgl. hbm. xml
Ensure that the employee information table and role management table can operate databases (ADD, repair, delete, and query );
Step 2: generate the ing file corresponding to the role personnel table as follows:
1) the content of the Jsry. hbm. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<Hibernate-mapping>
<Class name = "com. insigma. hr. eduj. jsry. model. Jsry" table = "jsry" catalog = "hr">
<Composite-id name = "id" class = "com. insigma. hr. eduj. jsry. model. JsryId">
<Key-example-to-one name = "jsgl" class = "com. insigma. hr. eduj. jsgl. model. Jsgl">
<Column name = "JSBH"/>
</Key-extract-to-one>
<Key-secret-to-one name = "ygxx" class = "com. insigma. hr. eduj. ygxx. model. Ygxx">
<Column name = "YGBH" length = "20"/>
</Key-extract-to-one>
</Composite-id>
</Class>
</Hibernate-mapping>

2). The JsryId. java javabean contains the javabean object corresponding to the employee information and role management table. The content is as follows:
Public class JsryId implements java. io. Serializable {
Private Jsgl jsgl;
Private Ygxx ygxx;
Public Jsgl getJsgl (){
Return jsgl;
}
Public void setJsgl (Jsgl jsgl ){
This. jsgl = jsgl;
}
Public Ygxx getYgxx (){
Return ygxx;
}
Public void setYgxx (Ygxx ygxx ){
This. ygxx = ygxx;
}
}

3). The Jsry. java javabean has only one attribute, which is the JsryId class object we just created. The content is as follows:
Public class Jsry implements java. io. Serializable {

// Constructors
Private JsryId id;

/** Default constructor */
Public Jsry (){
}

/** Full constructor */
Public Jsry (JsryId id ){
This. id = id;
}

Public JsryId getId (){
Return id;
}

Public void setId (JsryId id ){
This. id = id;
}
}

Step 3: The method in dao is as follows:
Public void save (Jsry jsry ){
Log. debug ("saving Jsry instance ");
Try {
GetHibernateTemplate (). saveOrUpdate (jsry );
Log. debug ("save successful ");
} Catch (RuntimeException re ){
Log. error ("save failed", re );
Throw re;
}
}

Public void delete (Jsry jsry ){
Log. debug ("deleting Jsry instance ");
Try {
GetHibernateTemplate (). delete (jsry );
Log. debug ("delete successful ");
} Catch (RuntimeException re ){
Log. error ("delete failed", re );
Throw re;
}
}

Public Jsry findById (com. insigma. hr. eduj. jsry. model. JsryId id ){
Log. debug ("getting Jsry instance with id:" + id );
Try {
Jsry instance = (Jsry) getHibernateTemplate (). get (
"Com. insigma. hr. eduj. jsry. model. Jsry", id );
Return instance;
} Catch (RuntimeException re ){
Log. error ("get failed", re );
Throw re;
}
}
 
Public List getJsgllist (String SQL ){
String sqls = "select t. * from Jsgl t where 1 = 1" + SQL;
SQLQuery query = getHibernateTemplate (). getSessionFactory ()
. GetCurrentSession (). createSQLQuery (sqls );
Query. addEntity ("t", Jsry. class );
List topList = query. list ();
Return topList;
}


Step 4: Test
Public static void main (String [] args ){
// TODO Auto-generated method stub
ApplicationContext appContext = new FileSystemXmlApplicationContext ("/src/applicationContext. xml ");
JsryService jsryService = (JsryService) appContext. getBean ("jsryService ");

Jsry jsry = new Jsry ();
JsryId id = new JsryId ();
Jsgl jsgl = new Jsgl ();
Jsgl. setJsbh (1 );
 
Ygxx ygxx = new Ygxx ();
Ygxx. setYgbh ("1 ");
Id. setYgxx (ygxx );
Id. setJsgl (jsgl );
 
Jsry. setId (id );
JsryService. save (jsry );
}

 

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.