hibernate复合主键映射

<?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="loaderman.d_compositeKey" auto-import="true">
    
    <class name="User">
        
        <!-- 复合主键映射 -->
        <composite-id name="keys">
            <key-property name="userName" type="string"></key-property>
            <key-property name="address" type="string"></key-property>
        </composite-id>
        
        <property name="age" type="int"></property>        
        
    </class>
    

</hibernate-mapping>
package loaderman.d_compositeKey;

import java.io.Serializable;

// 复合主键类
public class CompositeKeys implements Serializable{
    private String userName;
    private String address;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
}
package loaderman.d_compositeKey;

public class User {

    // 名字跟地址,不会重复
    private CompositeKeys keys;
    private int age;
    
    public CompositeKeys getKeys() {
        return keys;
    }
    public void setKeys(CompositeKeys keys) {
        this.keys = keys;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    
    
}
package loaderman.d_compositeKey;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App2 {

    private static SessionFactory sf;
    static  {
        // 创建sf对象
        sf = new Configuration()
                .configure()
                .addClass(User.class)  //(测试) 会自动加载映射文件:Employee.hbm.xml
                .buildSessionFactory();
    }

    //1. 保存对象
    @Test
    public void testSave() throws Exception {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        // 对象
        CompositeKeys keys = new CompositeKeys();
        keys.setAddress("广州棠东");
        keys.setUserName("Jack");
        User user = new User();
        user.setAge(20);
        user.setKeys(keys);

        // 保存
        session.save(user);


        tx.commit();
        session.close();
    }

    @Test
    public void testGet() throws Exception {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        //构建主键再查询
        CompositeKeys keys = new CompositeKeys();
        keys.setAddress("广州棠东");
        keys.setUserName("Jack");

        // 主键查询
        User user = (User) session.get(User.class, keys);
        // 测试输出
        if (user != null){
            System.out.println(user.getKeys().getUserName());
            System.out.println(user.getKeys().getAddress());
            System.out.println(user.getAge());
        }


        tx.commit();
        session.close();
    }
}

 

posted on 2018-11-29 11:01  LoaderMan  阅读(247)  评论(0编辑  收藏  举报

导航