快速业务通道

hibernate3学习笔记(十六)|容器映射 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-19
oid setName(String name) {27. this.name = name;28. }29. 30. public Set<MailAddress> getEmails() {31. return emails;32. }33. 34. public void setEmails(Set<MailAddress> emails) {35. this.emails = emails;36. }37.38. public void addEmail(MailAddress mailAddress) {39. this.emails.add(mailAddress);40. }41.42. public void removeEmail(MailAddress mailAddress) {43. this.emails.remove(mailAddress);44. }45.}

hibernate3学习笔记(十六)|容器映射(12)

时间: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_15.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.            <composite-element class="com.hb3.pack_15.model.MailAddress">19.                <property name="address" column="address"/>20.            </composite-element>
21.        </set>
22.    </class>
23.</hibernate-mapping>

测试代码:

1.package com.hb3.pack_15;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_15.model.MailAddress;13.import com.hb3.pack_15.model.User;14.
15.
16.public class BusinessService {17.
18.    public static void main(String[] args) throws IOException, SQLException {19.20.        Configuration config = new Configuration().configure();21.        SessionFactory sessionFactory = config.buildSessionFactory();22.        Session session = sessionFactory.openSession();23.
24.25.        User user = new User();26.        user.setName("shenbin");27.28.        user.setEmails(new HashSet<MailAddress>());29.        MailAddress mailAddress = new MailAddress();30.        mailAddress.setAddress("bluelazysb@hotmail.com");31.        user.addEmail(mailAddress);32.33.        mailAddress = new MailAddress();34.        mailAddress.setAddress("bluelazysb@yahoo.com.cn");35.        user.addEmail(mailAddress);36.37.        Transaction tx = session.beginTransaction();38.        session.save(user);39.        tx.commit();40.41.42.        session.close();43.        sessionFactory.close();44.    }

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号