hibernate操作联合主键

联合主键配置:一般用MyEclipse中hibernate反向生成工具就可以生成联合主键table的映射文件

Hibernate 联合主键的配置方法实例
建立 join_pkey 表(mysql数据库)
CREATE TABLE `join_pkey` (
  `sid` int(11) NOT NULL default '0',
  `bid` int(11) NOT NULL default '0',
  `name` varchar(20) default NULL,
  PRIMARY KEY  (`sid`,`bid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

JoinPkey Bean:

public class JoinPkey  implements java.io.Serializable {
    private JoinPkeyId id;
    private String name;
    public JoinPkey() {
    }
  public JoinPkey(JoinPkeyId id) {
        this.id = id;
    }
    public JoinPkey(JoinPkeyId id, String name) {
        this.id = id;
        this.name = name;
    }
  public JoinPkeyId getId() {
        return this.id;
    }
    public void setId(JoinPkeyId id) {
        this.id = id;
    }
  public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
JoinPkeyId  Bean:
public class JoinPkeyId  implements java.io.Serializable {
     private Integer sid;
     private Integer bid;
    public JoinPkeyId() {
    }
    public JoinPkeyId(Integer sid, Integer bid) {
        this.sid = sid;
        this.bid = bid;
    }
   public Integer getSid() {
        return this.sid;
    }
    public void setSid(Integer sid) {
        this.sid = sid;
    }
  public Integer getBid() {
        return this.bid;
    }
     public void setBid(Integer bid) {
        this.bid = bid;
    }
}

JoinPkey.hbm.xml:
<hibernate-mapping>
    <class name="com.vo1.JoinPkey" table="join_pkey">
        <composite-id name="id" class="com.vo1.JoinPkeyId">
            <key-property name="sid" type="java.lang.Integer">
                <column name="sid" />
            </key-property>
            <key-property name="bid" type="java.lang.Integer">
                <column name="bid" />
            </key-property>
        </composite-id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
    </class>
</hibernate-mapping>

Test.java:
public class Test {

    SessionFactory sf;
    Session session;
    private Test()
    {
        try
        {
            Configuration cfg = new Configuration();
            sf = cfg.configure().buildSessionFactory();
            session = sf.openSession();
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }
        
    }
    public void addMethod(JoinPkey st)
       {
           Transaction t=null;
           t=session.beginTransaction();
           session.save(st);
           t.commit();
           
       }
    
    public static void main(String[] args) {
        JoinPkey jp=new JoinPkey();
        jp.setName("aaa");
        JoinPkeyId jki=new JoinPkeyId();
        jki.setBid(1);
        jki.setSid(1);
        jp.setId(jki);
        new Test().addMethod(jp);
        
    }

}

以上配置.本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wslyy99/archive/2008/10/15/3078578.aspx

 

操作此对象的时候,首先赋值到ID类JoinPkeyId  ,然后set到JoinPkey.setId(JoinPkeyId这个类的实例)

最后进行操作,调用hibernatetemplate().save(JoinPkey) 就可以持久化这个实例.

以此进行其他增删改查操作.

你可能感兴趣的:(hibernate操作联合主键)