Hibernate學習之路(三)

來源:互聯網
上載者:User

標籤:div   nbsp   ati   log   pen   pack   .config   style   nat   

簡要聊一聊 組合主鍵映射

一:組合主鍵映射(資料庫中某張表有多個主鍵)

  比如有一個學產生績類,有屬性:stuId,subjectId,result. 其中stuId 和 subjectId 對應映射表中的組合主鍵(stuId,subjectId)

  1、編寫組合主鍵的類,該類必須實現Serializable介面

 1 package cn.pojo; 2  3 import java.io.Serializable; 4  5 public class ScoreId implements Serializable{ 6     private int stuId; 7     private int subjectId; 8      9     public int getStuId() {10         return stuId;11     }12     public void setStuId(int stuId) {13         this.stuId = stuId;14     }15     public int getSubjectId() {16         return subjectId;17     }18     public void setSubjectId(int subjectId) {19         this.subjectId = subjectId;20     }21     22 }

  2、在主類中引用該類

 1 package cn.pojo; 2  3 public class Score { 4     private ScoreId scoreId; 5     private int resule; 6      7     public ScoreId getScoreId() { 8         return scoreId; 9     }10     public void setScoreId(ScoreId scoreId) {11         this.scoreId = scoreId;12     }13     public int getResule() {14         return resule;15     }16     public void setResule(int resule) {17         this.resule = resule;18     }19     20 }

  3、編寫對應檔

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">        <hibernate-mapping package="cn.pojo">    <class name="Score">        <composite-id name="scoreId" class="ScoreId">            <key-property name="stuId"></key-property>            <key-property name="subjectId"></key-property>        </composite-id>                <property name="result"></property>    </class></hibernate-mapping>

  4、測試

package cn.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import cn.pojo.Score;import cn.pojo.ScoreId;public class TestHibernate {    public static void main(String[] args) {        //通過Configuration對象建立SessionFactory對象        SessionFactory sf = new Configuration().configure().buildSessionFactory();        //建立Session對象        Session s = sf.openSession();        //開啟事務        s.beginTransaction();                //我們只需要關注這裡的事務。        Score score = new Score();        ScoreId si = new ScoreId();        si.setStuId(1);        si.setSubjectId(3);        score.setScoreId(si);        score.setResult(89);        s.save(score);                //提交事務        s.getTransaction().commit();        //關閉Session        s.close();        //關閉SessionFactory        sf.close();    }}

 

  

Hibernate學習之路(三)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.