Hibernate OneToOne 配置

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.helloworld.entity;

/**
 *
 * @author LiuQing
 */
public class Operation {

    private String id;

    private String opName;

    private UserInfo userInfo;

    /**
     * 注意
     * @param argUserInfo
     */
    public void setUserInfoRelation(UserInfo argUserInfo) {
        this.userInfo = argUserInfo;
        this.userInfo.setOperation(this);
    }

    public String getId() {
        return id;
    }

  
    public void setId(String id) {
        this.id = id;
    }

    public String getOpName() {
        return opName;
    }

    public void setOpName(String opName) {
        this.opName = opName;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

}

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.helloworld.entity;

/**
 *
 * @author LiuQing
 */
public class UserInfo {

    private String id;

    private String name;

   private Operation operation;

   public void setOperationRelation(Operation argOperation) {
       this.operation = argOperation;
       this.operation.setUserInfo(this);
   }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Operation getOperation() {
        return operation;
    }

    public void setOperation(Operation operation) {
        this.operation = operation;
    }

}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2011-12-31 9:56:49 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.helloworld.entity.Operation" table="operation">
       <id name="id" type="string">
            <column name="id" />
            <generator class="uuid" />
       </id>
       <property name="opName" />
       <many-to-one name="userInfo" cascade="all" />
    </class>
</hibernate-mapping>

 

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2011-12-31 9:56:49 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.helloworld.entity.UserInfo" table="userinfo">
       <id name="id" type="string">
            <column name="id" />
            <generator class="uuid" />
       </id>
       <property name="name" />
       <one-to-one name="operation" cascade="all" property-ref="userInfo" />
    </class>
</hibernate-mapping>

 

 第二种使用annotation

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.helloworld.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

/**
 *
 * @author liuqing
 */
@Entity(name="userInfo")
@Table(name="tb_Userinfo")
public class UserInfo implements Serializable {

    @Id
    @GenericGenerator(name="idGenerator", strategy="uuid") //hibernate的注解
    @GeneratedValue(generator="idGenerator")
    private String id;
    
    @Column
    private String name;

    /**
     * 也可以用OneToOne
     */
    @ManyToOne
   private Operations operation;

   public void setOperationRelation(Operations argOperation) {
       this.operation = argOperation;
       this.operation.setUserInfo(this);
   }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Operations getOperation() {
        return operation;
    }

    public void setOperation(Operations operation) {
        this.operation = operation;
    }

}

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.helloworld.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

@Entity(name="Operation")
@Table(name="Operation")
public class Operations implements Serializable {

    /**
     * 定义uuid 32位编码
     */
    @Id
    @GenericGenerator(name="idGenerator", strategy="uuid") //hibernate的注解
    @GeneratedValue(generator="idGenerator")
    private String id;

    @Column
    private String opName;

    /**
     * 一对一
     */
    @OneToOne(mappedBy = "operation")
    private UserInfo userInfo;

    /**
     * 注意
     * @param argUserInfo
     */
    public void setUserInfoRelation(UserInfo argUserInfo) {
        this.userInfo = argUserInfo;
        this.userInfo.setOperation(this);
    }

    public String getId() {
        return id;
    }

  
    public void setId(String id) {
        this.id = id;
    }

    public String getOpName() {
        return opName;
    }

    public void setOpName(String opName) {
        this.opName = opName;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

}

 

//注意在后面版本中(3.2 之后) annotationConfiguration 被去掉了
//统一用Configuration config = new Configuration().configuration;
//不分注解或xml配置方法
Configuration config = new AnnotationConfiguration().configure();
        Session session = config.buildSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

 

 

 

 

 

 

 

 

你可能感兴趣的:(Hibernate)