Hibernate 一对多关系的实现
实体类User
?
??
?
映射文件 user.hbm.xml
?public class Test { public static void main(String[] args) { Session session=DBFactory.getSession(); Transaction ts=session.beginTransaction(); try{ Order order1=new Order(); order1.setName("Java"); order1.setBalance(99.5); Order order2=new Order(); order2.setName("C++"); order2.setBalance(78.5); Order order3=new Order(); order3.setName("JDBC"); order3.setBalance(96.5); User user=new User(); user.setUsername("zhangsan"); user.setPassword("123456"); Set<Order> orders=new HashSet<Order>(); order1.setUser(user); order2.setUser(user); order3.setUser(user); orders.add(order1); orders.add(order2); orders.add(order3); user.setOrders(orders); session.save(user); ts.commit(); }catch(Exception e){ e.printStackTrace(); ts.rollback(); }finally{ session.close(); } }}?