hibernate 联合主键

Hibernate 联合主键的配置方法实例 收藏 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: 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

你可能感兴趣的:(Hibernate,String,session,null,Integer,Class)