【Hibernate】---【註解】一對一

來源:互聯網
上載者:User

標籤:.sh   mapping   doctype   sql   doc   pack   open   static   t_sql   

一、核心設定檔

<?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"><hibernate-configuration>    <session-factory>        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.password">root</property>        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>        <property name="hibernate.connection.username">root</property>                <property name="hibernate.show_sql">true</property>        <property name="hibernate.format_sql">true</property>        <property name="hibernate.hbm2ddl.auto">update</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>                <property name="current_session_context_class">thread</property>        <!--         <mapping resource="com/chinasofti/entity/User.hbm.xml"/>        <mapping resource="com/chinasofti/entity/Role.hbm.xml"/>         -->                <mapping class="com.chinasofti.entity.Person"/>        <mapping class="com.chinasofti.entity.Card"/>            </session-factory></hibernate-configuration>

二、實體類

  person

package com.chinasofti.entity;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name="T_Person")public class Person {    @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer pid;    private String pname;    @OneToOne(cascade=CascadeType.ALL,mappedBy="person")    @JoinColumn(name="pid")    private Card card;    public Integer getPid() {        return pid;    }    public void setPid(Integer pid) {        this.pid = pid;    }    public String getPname() {        return pname;    }    public void setPname(String pname) {        this.pname = pname;    }    public Card getCard() {        return card;    }    public void setCard(Card card) {        this.card = card;    }    @Override    public String toString() {        return "Person [pid=" + pid + ", pname=" + pname + ", card=" + card                + "]";    }    }

Card

package com.chinasofti.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name="T_Card")public class Card {    @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer cid;    private String cnum;        @OneToOne    @JoinColumn(name="pid")    private Person person;    public Integer getCid() {        return cid;    }    public void setCid(Integer cid) {        this.cid = cid;    }    public String getCnum() {        return cnum;    }    public void setCnum(String cnum) {        this.cnum = cnum;    }    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }    @Override    public String toString() {        return "Card [cid=" + cid + ", cnum=" + cnum + ", person=" + person                + "]";    }}

三、封裝類

package com.chinasofti.entity;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {    static Configuration CONFIGURATION = null;    static SessionFactory SESSION_FACTORY = null;    static Session SESSION = null;    static{        CONFIGURATION = new Configuration();        CONFIGURATION.configure();        SESSION_FACTORY = CONFIGURATION.buildSessionFactory();        SESSION    = SESSION_FACTORY.openSession();    }    public static SessionFactory getSessionFactory(){        return SESSION_FACTORY;    }    public static Session openSession(){        return SESSION;    }}

四、測試類別

package com.chinasofti.entity;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;public class TestOneOne {    @Test    public void test1to1(){        Session session = HibernateUtil.openSession();        Transaction transaction = session.beginTransaction();        Person person = session.get(Person.class, 1);        session.delete(person);        transaction.commit();        session.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.