one-to-many

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();
        }
        
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值