<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    將實體對象中的邏輯組成稱為component。在配置文件中,使用component節點對組件進行聲明。

    例如,一個用戶有兩個部分組成,姓名和聯系方式。姓名和聯系方式都可以作為用戶的組成部分。

    1.表結構如下
    use?sample;
    DROP?TABLE?t_user;

    CREATE?TABLE?t_user?(
    ???????id?
    INT?NOT?NULL?AUTO_INCREMENT
    ?????,?age?
    INT
    ?????,?firstname?
    VARCHAR(50)
    ?????,?lastname?
    VARCHAR(50)
    ?????,?address?
    VARCHAR(200)
    ?????,?zipcode?
    VARCHAR(10)
    ?????,?tel?
    VARCHAR(20)
    ?????,?
    PRIMARY?KEY?(id)
    );

    2.配置文件
    TUser.hbm.xml
    <?xml?version="1.0"?>
    <!DOCTYPE?hibernate-mapping?PUBLIC?
    "-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <!--?
    ????????Auto-generated?mapping?file?from
    ????????the?hibernate.org?cfg2hbm?engine
    -->
    ????<class?name
    ="cn.blogjava.component.TUser"?table="t_user"?catalog="sample">
    ????????<id?name
    ="id"?type="integer">
    ????????????<column?name
    ="id"?/>
    ????????????<generator?class
    ="native"?/>
    ????????</id>
    ????????<property?name
    ="age"?type="integer">
    ????????????<column?name
    ="age"?/>
    ????????</property>
    ????????<component?name
    ="name"?class="cn.blogjava.component.Name">
    ????????????<property?name
    ="firstname"?type="string">
    ????????????????<column?name
    ="firstname"?length="50"?/>
    ????????????</property>
    ????????????<property?name
    ="lastname"?type="string">
    ????????????????<column?name
    ="lastname"?length="50"?/>
    ????????????</property>
    ????????</component>
    ????????<component?name
    ="contact"?class="cn.blogjava.component.Contact">
    ????????????<property?name
    ="address"?type="string">
    ????????????????<column?name
    ="address"?length="200"?/>
    ????????????</property>
    ????????????<property?name
    ="zipcodes"?type="string">
    ????????????????<column?name
    ="zipcode"?length="10"?/>
    ????????????</property>
    ????????????<property?name
    ="tel"?type="string">
    ????????????????<column?name
    ="tel"?length="20"?/>
    ????????????</property>
    ????????</component>
    ????</class>
    </hibernate-mapping>
    將TUser.hbm.xml配置文件加入到hibernate.cfg.xml中去

    3.POJO類
    Contact.java
    package?cn.blogjava.component;

    import?java.io.Serializable;

    public?class?Contact?implements?Serializable?{
    ????
    ????
    private?String?address;
    ????
    private?String?zipcodes;
    ????
    private?String?tel;
    ????
    ????
    public?String?getAddress()?{
    ????????
    return?address;
    ????}
    ????
    public?void?setAddress(String?address)?{
    ????????
    this.address?=?address;
    ????}
    ????
    public?String?getTel()?{
    ????????
    return?tel;
    ????}
    ????
    public?void?setTel(String?tel)?{
    ????????
    this.tel?=?tel;
    ????}
    ????
    public?String?getZipcodes()?{
    ????????
    return?zipcodes;
    ????}
    ????
    public?void?setZipcodes(String?zipcodes)?{
    ????????
    this.zipcodes?=?zipcodes;
    ????}
    ????
    ????
    }

    Name.java
    package?cn.blogjava.component;

    import?java.io.Serializable;

    public?class?Name?implements?Serializable?{
    ????
    ????
    private?String?firstname;
    ????
    private?String?lastname;
    ????
    ????
    public?String?getFirstname()?{
    ????????
    return?firstname;
    ????}
    ????
    public?void?setFirstname(String?firstname)?{
    ????????
    this.firstname?=?firstname;
    ????}
    ????
    public?String?getLastname()?{
    ????????
    return?lastname;
    ????}
    ????
    public?void?setLastname(String?lastname)?{
    ????????
    this.lastname?=?lastname;
    ????}
    ????
    ????
    }

    TUser.java
    package?cn.blogjava.component;


    public?class?TUser??implements?java.io.Serializable?{
    ????
    private?Integer?id;
    ????
    private?Integer?age;
    ????
    private?Name?name;
    ????
    private?Contact?contact;
    ????
    ????
    public?Integer?getAge()?{
    ????????
    return?age;
    ????}
    ????
    public?void?setAge(Integer?age)?{
    ????????
    this.age?=?age;
    ????}
    ????
    public?Contact?getContact()?{
    ????????
    return?contact;
    ????}
    ????
    public?void?setContact(Contact?contact)?{
    ????????
    this.contact?=?contact;
    ????}
    ????
    public?Integer?getId()?{
    ????????
    return?id;
    ????}
    ????
    public?void?setId(Integer?id)?{
    ????????
    this.id?=?id;
    ????}
    ????
    public?Name?getName()?{
    ????????
    return?name;
    ????}
    ????
    public?void?setName(Name?name)?{
    ????????
    this.name?=?name;
    ????}
    ?????
    }

    4.測試類
    package?cn.blogjava.component;

    import?java.util.List;

    import?org.hibernate.HibernateException;
    import?org.hibernate.Query;
    import?org.hibernate.Session;
    import?org.hibernate.SessionFactory;
    import?org.hibernate.Transaction;
    import?org.hibernate.cfg.Configuration;

    import?junit.framework.TestCase;
    import?junit.framework.Assert;

    public?class?HibernateTest?extends?TestCase?{
    ????
    ????Session?session?
    =?null;
    ????
    ????
    protected?void?setUp(){
    ????????
    try?{
    ????????????Configuration?config?
    =?new?Configuration().configure();
    ????????????SessionFactory?sessionFactory?
    =?config.buildSessionFactory();
    ????????????session?
    =?sessionFactory.openSession();
    //????????????delete();
    ????????}?catch?(HibernateException?e)?{
    ????????????
    //?TODO:?handle?exception
    ????????????e.printStackTrace();
    ????????}
    ????}
    ????
    ????
    protected?void?tearDown()?{
    ????????
    try?{
    ????????????session.close();
    ????????}?
    catch?(HibernateException?e)?{
    ????????????
    //?TODO:?handle?exception
    ????????????e.printStackTrace();????????????
    ????????}
    ????}
    ????
    ????
    public?void?delete(){
    ????????Transaction?tran?
    =?null;
    ????????String?hql?
    =?"?delete?TUser";
    ????????
    try?{
    ????????????tran?
    =?session.beginTransaction();
    ????????????Query?query?
    =?session.createQuery(hql);
    ????????????
    int?ret?=?query.executeUpdate();
    ????????????System.out.println(
    "Delete?records?=>?"?+?ret);
    ????????????tran.commit();
    ????????}?
    catch?(HibernateException?e)?{
    ????????????
    //?TODO:?handle?exception
    ????????????e.printStackTrace();
    ????????????Assert.fail(e.getMessage());
    ????????????
    if(tran?!=?null)?{
    ????????????????
    try?{
    ????????????????????tran.rollback();
    ????????????????}?
    catch(HibernateException?e2)?{
    ????????????????????e2.printStackTrace();
    ????????????????}
    ????????????}
    ????????}
    ????}
    ????
    ????
    public?void?testInsert(){
    ????????Transaction?tran?
    =?null;
    ????????
    try?{
    ????????????System.out.println(
    "session?is?"?+?session);
    ????????????tran?
    =?session.beginTransaction();
    ????????????
    ????????????
    //user1
    ????????????TUser?user?=?new?TUser();
    ????????????user.setAge(
    25);
    ????????????Name?name?
    =?new?Name();
    ????????????name.setFirstname(
    "yu");
    ????????????name.setLastname(
    "yy");
    ????????????user.setName(name);
    ????????????Contact?contact?
    =?new?Contact();
    ????????????contact.setAddress(
    "dalian");
    ????????????contact.setTel(
    "42689334");
    ????????????contact.setZipcodes(
    "116023");
    ????????????user.setContact(contact);
    ????????????
    ????????????session.save(user);
    ????????????
    ????????????
    //user2
    ????????????TUser?user2?=?new?TUser();
    ????????????user2.setAge(
    26);
    ????????????Name?name2?
    =?new?Name();
    ????????????name2.setFirstname(
    "bai");
    ????????????name2.setLastname(
    "yf");
    ????????????user2.setName(name2);
    ????????????Contact?contact2?
    =?new?Contact();
    ????????????contact2.setAddress(
    "beijing");
    ????????????contact2.setTel(
    "12345678");
    ????????????contact2.setZipcodes(
    "100010");
    ????????????user2.setContact(contact2);????????????
    ????????????
    ????????????session.save(user2);
    ????????????
    ????????????session.flush();
    ????????????tran.commit();
    ????????????Assert.assertEquals(user.getId().intValue()?
    >?0,?true);
    ????????}?
    catch?(Exception?e)?{
    ????????????
    //?TODO:?handle?exception
    ????????????e.printStackTrace();
    ????????????Assert.fail(e.getMessage());
    ????????????
    if(tran?!=?null)?{
    ????????????????
    try?{
    ????????????????????tran.rollback();
    ????????????????}?
    catch(HibernateException?e2)?{
    ????????????????????e2.printStackTrace();
    ????????????????}
    ????????????}
    ????????}
    ????}
    ????
    ????
    public?void?testSelect(){
    ????????String?hql?
    =?"?from?TUser?where?age=25?";
    ????????
    try?{
    ????????????List?userList?
    =?session.createQuery(hql).list();
    ????????????TUser?user?
    =?(TUser)userList.get(0);
    ????????????Contact?contact?
    =?user.getContact();
    ????????????Assert.assertEquals(contact.getAddress(),?
    "dalian");
    ????????????Assert.assertEquals(user.getName().getFirstname(),?
    "yu");
    ????????}?
    catch?(HibernateException?e)?{
    ????????????
    //?TODO:?handle?exception
    ????????????e.printStackTrace();
    ????????????Assert.fail(e.getMessage());
    ????????}
    ????}
    }
    posted on 2006-07-05 14:55 knowhow 閱讀(286) 評論(0)  編輯  收藏 所屬分類: ORM:Hibernate及其他
    主站蜘蛛池模板: 91免费人成网站在线观看18| 免费女人高潮流视频在线观看| 亚洲中文无码永久免费| 亚洲AV美女一区二区三区| 人妻在线日韩免费视频| 亚洲成a人片77777老司机| 久久国产精品2020免费m3u8| 久久精品国产亚洲av四虎| 久9久9精品免费观看| 亚洲综合激情六月婷婷在线观看| 最近2019免费中文字幕视频三| 亚洲国产精品综合福利专区| 一个人免费观看在线视频www| 亚洲人成电影网站久久| 国产一区二区三区免费看| 一级毛片视频免费| 亚洲综合一区二区国产精品| 国产精品视频免费| 久久精品熟女亚洲av麻豆| 亚洲精品99久久久久中文字幕| 最好免费观看高清在线| 亚洲大片免费观看| 四虎国产精品免费视| 99久久免费国产精精品| 亚洲欧洲精品视频在线观看| 日韩高清在线免费看| 中文字幕版免费电影网站| 337p日本欧洲亚洲大胆精品555588 | 在线观看免费为成年视频| 美女裸体无遮挡免费视频网站| 中文字幕亚洲无线码| 青娱乐免费视频在线观看| 老司机午夜在线视频免费观| 久久亚洲国产视频| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 在线免费观看亚洲| 麻豆69堂免费视频| 亚洲一区二区成人| 国产女高清在线看免费观看| 未满十八18禁止免费无码网站 | 一级午夜a毛片免费视频|