hibernate3学习笔记(十六)|容器映射 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-19
hibernate3学习笔记(十六)|容器映射时间:2011-02-03 csdn博客 沈斌1.Set映射 DDL: 1.CREATE TABLE user (2. id INT(11) NOT NULL auto_increment PRIMARY KEY,3. name VARCHAR(100) NOT NULL default ''''4. );5.6.CREATE TABLE email (7. id INT(11) NOT NULL,8. address VARCHAR(100) NOT NULL9.); User.java 1.package com.hb3.pack_11.model;2.3.import java.util.Set;4. 5.public class User {6. 7. private Integer id;8. private String name;9. private Set<String> emails;10. 11. public User() {12. }13. 14. public Integer getId() {15. return id;16. }17. public void setId(Integer id) {18. this.id = id;19. }20. public String getName() {21. return name;22. }23. public void setName(String name) {24. this.name = name;25. }26. public Set<String> getEmails() {27. return emails;28. }29. public void setEmails(Set<String> emails) {30. this.emails = emails;31. }32. public void addEmail(String email) {33. this.emails.add(email);34. }35. public void removeEmail(String email) {36. this.emails.remove(email);37. }38.} hibernate3学习笔记(十六)|容器映射(2)时间:2011-02-03 csdn博客 沈斌User.hbm.xml: 1.<?xml version="1.0" encoding="utf-8"?>2.<!DOCTYPE hibernate-mapping3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5. 6.<hibernate-mapping> 7. 8. <class name="com.hb3.pack_11.model.User" table="user"> 9.10. <id name="id" column="id" type="java.lang.Integer"> 11. <generator class="native" /> 12. </id> 13. 14. <property name="name" column="name" type="java.lang.String" /> 15.16. <set name="emails" table="email"> 17. <key column="id" /> 18. <element type="java.lang.String" column="address" /> 19. </set> 20. </class> 21.</hibernate-mapping> 测试代码: 1.package com.hb3.pack_11;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_11.model.User;13. 14.public class BusinessService {15. 16. public static void main(String[] args) throws IOException, SQLException {17.18. Configuration config = new Configuration().configure();19. SessionFactory sessionFactory = config.buildSessionFactory();20. Session session = sessionFactory.openSession();21. 22.23. User user1 = new User();24. user1.setEmails(new HashSet<String>());25. user1.setName("shenbin");26. |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于hibernate3学习笔记(十六)|容器映射 - 编程入门网的所有评论