posts - 10, comments - 9, trackbacks - 0, articles - 17

Hibernate学习笔记1--映射起步

Posted on 2008-12-09 15:10 wesley1987 阅读(131) 评论(0)  编辑  收藏
1,操作对象,将数据库里的表映射为持久化类。
    一个商品类 Good,一个供应商 Offer类,一个Offer可以提供多个商品。
商品表字段: goodId, offerId, goodName, price .
供应商字段:    offerId ,offerName.
Good.hbm.xml
:<hibernate-mapping>
    <class name="com.neusoft.Good" table="GOOD" schema="WULIU">
        <id name="goodid" type="java.lang.Long">
            <column name="GOODID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <many-to-one name="offer" class="com.myweb.Offer" fetch="select">
            <column name="OFFERID" precision="10" scale="0" />
        </many-to-one>

        <property name="goodname" type="java.lang.String">
            <column name="GOODNAME" length="40" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="PRICE" precision="10" />
        </property>

        </property>
    </class>
</hibernate-mapping>

Offer.hbm.xml

<hibernate-mapping>
    <class name="com.neusoft.Offer" table="OFFER" schema="WULIU">
        <id name="offerid" type="java.lang.Long">
            <column name="OFFERID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="offername" type="java.lang.String">
            <column name="OFFERNAME" length="30" />
        </property>
        <set name="goods" inverse="true" lazy="true" cascade="delete" >
            <key>
                <column name="OFFERID" precision="10" scale="0" />
            </key>

            <one-to-many class="com.neusoft.Good" />
<!--java.util.Set : private Set goods = new HashSet(0);-->

        </set>
    </class>
</hibernate-mapping>

lazy="true" 与特定的Offer对象相对应的Good不会像Offer对象一样由同一个SQL查询获取
inverse="true" 表示用此表示两者之间的关系。
cascade="delete" 级联删除

由此配置文件可生成对应的持久化类(即JavaBeans类)  Good.java, Offer.java

2. org.hibernate.cfg.Configuration 创建一个HibernateSessionFactory。
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
          
          private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";//xml中配置了数据库的连接信息。
        private static org.hibernate.SessionFactory sessionFactory;
        
private static String configFile = CONFIG_FILE_LOCATION;

    
static {
        
try {
            configuration.configure(configFile);
            sessionFactory 
= configuration.buildSessionFactory();
        } 
catch (Exception e) {
            System.err
                    .println(
"%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }
    
private HibernateSessionFactory() {
    }

    public static org.hibernate.SessionFactory getSessionFactory() {
          return sessionFactory;
     }

}
(这是用Myeclipse自动生成的代码中一小段)
创建了SessionFactory,就可以用getSession方法来实现操作了,如
public void save(Good transientInstance) {
          log.debug("saving Good instance");
      try {
               getSession().save(transientInstance);
               log.debug("save successful");
      } catch (RuntimeException re) {
               log.error("save failed", re);
               throw re;
      }
 }





只有注册用户登录后才能发表评论。


网站导航: