标签:nsa get 2.4 net llb 主键 查找 语文 use
2.1 创建有复合主键的表
这张表有复合主键
2.2 生成映射文件
Hibernate 对含有复合主键的表生成bean的时候会把复合主键作为单独的bean独立生成。
2.3 分析Score和ScoreId bean
2.4 分析总映射文件
案例一 Test_CompositeId.java
package action; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.Transaction; import bean.Score; import bean.ScoreId; import util.HibernateUtil; public class Test_CompositeId { public static void main(String[] args) { // Test_CompositeId.select(); // Test_CompositeId.save(); Test_CompositeId.update(); } /** * 查找 */ private static void select() { Session session = null; Transaction tran = null; ScoreId id = new ScoreId(); id.setUserid(1); id.setSubject("语文"); try { session = HibernateUtil.getSessionFactory().getCurrentSession(); tran = session.beginTransaction(); Score score = (Score) session.get(Score.class, id); System.out.println("成绩=" + score.getScore()); tran.commit(); } catch (Exception e) { e.printStackTrace(); tran.rollback(); } } private static void save() { Session session = null; Transaction tran = null; ScoreId id = new ScoreId(); id.setSubject("英语"); id.setUserid(1); Score score = new Score(); score.setId(id); score.setScore(100); try { session = HibernateUtil.getSessionFactory().getCurrentSession(); tran = session.beginTransaction(); Serializable primarykey = session.save(score); System.out.println(primarykey); tran.commit(); } catch (Exception e) { e.printStackTrace(); tran.rollback(); } } /** * 执行更新操作 */ private static void update() { Session session = null; Transaction tran = null; ScoreId id=new ScoreId(); id.setUserid(1); id.setSubject("英语"); Score score=new Score(); score.setId(id); score.setScore(12); try { session = HibernateUtil.getSessionFactory().getCurrentSession(); tran = session.beginTransaction(); session.update(score); tran.commit(); } catch (Exception e) { e.printStackTrace(); tran.rollback(); } } }
所有代码在这里 : 链接
标签:nsa get 2.4 net llb 主键 查找 语文 use
原文地址:http://www.cnblogs.com/shyroke/p/6850387.html