Hibernate---注解一对一

Posted Angelye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate---注解一对一相关的知识,希望对你有一定的参考价值。

一、核心配置文件

<?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---注解一对一的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 注解 (Annotations 三)多对一双向注解

Hibernate---注解一对一

hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系

Hibernate多对一(注解)

Hibernate一对一双向关联(注解)

03-hibernate注解-关系映射级别注解