hibernate3学习笔记(二十)|关系映射:多对多 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-19
3学习笔记(二十)|关系映射:多对多(3)
时间:2011-02-03
User.hbm.xml 1.<?xml version="1.0" encoding="UTF-8"?>2. 3.<!DOCTYPE hibernate-mapping PUBLIC4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping>8. 9. <class name="com.hb3.pack_19.model.User" table="user"> 10.11. <id name="id" column="id" type="java.lang.Integer">12. <generator class="native"/>13. </id>14. 15. <property name="name" column="name" type="java.lang.String"/>16. 17. <set name="servers"18. table="user_server"19. cascade="save-update">20.21. <key column="user_id"/>22. <many-to-many class="com.hb3.pack_19.model.Server"23. column="server_id"/>24. </set>25. </class>26. 27.</hibernate-mapping> 测试代码: 1.package com.hb3.pack_19;2.3.import java.io.IOException;4.import java.sql.SQLException;5.import java.util.HashSet;6. 7.import org.hibernate.Session;8.import org.hibernate.SessionFactory;9.import org.hibernate.Transaction;10.import org.hibernate.cfg.Configuration;11. 12.import com.hb3.pack_19.model.Server;13.import com.hb3.pack_19.model.User;14. 15.public class BusinessService {16. 17. public static void main(String[] args) throws IOException, SQLException {18.19. Configuration config = new Configuration().configure();20. SessionFactory sessionFactory = config.buildSessionFactory();21. Session session = sessionFactory.openSession();22. 23.24. Server server1 = new Server();25. server1.setAddress("PC-219");26. server1.setUsers(new HashSet<User>());27. 28. Server server2 = new Server();29. server2.setAddress("PC-220");30. server2.setUsers(new HashSet<User>());31.32. Server server3 = new Server();33. server3.setAddress("PC-221");34. server3.setUsers(new HashSet<User>());35.36. User user1 = new User();37. user1.setName("shenbin");38. user1.setServers(new HashSet<Server>());39.40. User user2 = new User();41. user2.setName("chenyan");42. user2.setServers(new HashSet<Server>());43. 44. user1.getServers().add(server1);45. user1.getServers().add(server2);46. user1.getServers().add(server3);47. server1.getUsers().add(user1);48. server2.getUsers().add(user1);49. server3.getUsers().add(user1);50. 51. user2.getServers().add(server1);52. user2.getServers().add(server3);53. server1.getUsers().add(user2);54. server3.getUsers().add(user2) |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于hibernate3学习笔记(二十)|关系映射:多对多 - 编程入门网的所有评论