hibernate入门篇之新增特性_3:one-to-many

http://www.hibernate.org.cn/66.html

接上一篇文章hibernate入门篇之新增特性_2:one-to-one。我们再继续进行!(为什么要说再呢?)

one to many时,两端都需要描述。

Publication.java

package com.javamodel.hibernate;
public class Publication {
    
    private String id = null;
    private String bookName = null;
    private String dataTime = null;
    private String authorId = null;
    private Author author = null;
    
    public Publication(){}
    
    /**
     * @return
     */
    public String getBookName() {
        return bookName;
    }

    /**
     * @return
     */
    public String getDataTime() {
        return dataTime;
    }

    /**
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * @param string
     */
    public void setBookName(String string) {
        bookName = string;
    }

    /**
     * @param string
     */
    public void setDataTime(String string) {
        dataTime = string;
    }

    /**
     * @param string
     */
    public void setId(String string) {
        id = string;
    }

    /**
     * @return
     */
    public String getAuthorId() {
        return authorId;
    }

    /**
     * @param string
     */
    public void setAuthorId(String string) {
        authorId = string;
    }

    /**
     * @return
     */
    public Author getAuthor() {
        return author;
    }

    /**
     * @param author
     */
    public void setAuthor(Author author) {
        this.author = author;
    }

}

Publication.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
    <class name="com.javamodel.hibernate.Publication" table="publication">
        <id name="id" column="id">
            <generator class="uuid.hex"/>
        </id>
        <property name="bookName" column="bookname" />
        <property name="dataTime" column="datatime" />
        <!--多的这端也需要一个简单的many-to-one的描述-->
        <many-to-one name="author" column="authorid" />
    </class>
</hibernate-mapping>

Author.java加上

private Set publications = new HashSet();//add get,set

author.hbm.xml加上

<!--
     这里是主的这端,你可以使用set,list,bag等,参照说明文档,inverse="true",默认为false。这个地方
    在描述这两个对应的mapping文件的时候,必须要有一端为:true。yehs220也多次提到过。
-->
        <set name="publications" lazy="true" inverse="true" cascade="all" >
          <key column="authorid"/> 
          <one-to-many class="com.javamodel.hibernate.Publication" />
        </set>

Example.java

package com.javamodel.hibernate;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class Example{
    
    private static SessionFactory _sessions = null;
    private static Properties pops = new Properties();
    
    static{
        try {
            InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
            try {
                pops.load(stream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            
            Configuration cfg = new Configuration();
            cfg.addClass(Person.class);
            cfg.addClass(Author.class);
            cfg.addClass(Publication.class);
            cfg.setProperties(pops);
            _sessions = cfg.buildSessionFactory();
            
        } catch (MappingException e) {
           e.printStackTrace();
        } catch (HibernateException e) {
           e.printStackTrace();
        }
    
    }
    
    public static void main(String[] args) throws HibernateException {
        
        Person person = new Person();
        
        person.setName("HengfeiDo");
        person.setEmail("smallduzi@sohu.com");
        
        Publication publication1 = new Publication();
        publication1.setBookName("AAA");
        publication1.setDataTime("20031224");
        Publication publication2 = new Publication();
        publication2.setBookName("BBB");
        publication2.setDataTime("20031225");
        
        Author author = new Author();

        author.setAlias("smallduzi");
        author.setPerson(person);
        author.getPublications().add(publication1);
        author.getPublications().add(publication2);
        
        publication1.setAuthor(author);
        publication2.setAuthor(author);

        Session session = _sessions.openSession();
        
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            session.save(author);
            
            tx.commit();
            System.out.println("over");
        }catch(HibernateException he){
            if(tx != null) tx.rollback();
            throw he;
        }
        finally{
            session.close();
        }
        
    }
    
}

下一篇,我们介绍many-to-many

posted @ 2004-12-16 12:58  wuxh  阅读(212)  评论(0)    收藏  举报