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

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

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

    隨筆-28  評論-15  文章-81  trackbacks-0
     

    1 。建立數(shù)據(jù)庫

    Database:mysql
    drop database if exists SAMPLEDB;
    create database hello;
    use hello;

    create table CUSTOMERS (
      ID int not null primary key,
      NAME varchar(15) not null,
      PASSWORD varchar(8) not null
    );

    2
    。在eclipse 中新建工程hbtest

    3
    。在src中新建package hbm

    4
    。新建pojoCustomer

    package hbm;

    public class Customer {
        private int id;
        private String name;
        private String password;
        /**
         * @return Returns the id.
         */
        public int getId() {
            return id;
        }
        /**
         * @param id
         *            The id to set.
         */
        public void setId(int id) {
            this.id = id;
        }
        /**
         * @return Returns the name.
         */
        public String getName() {
            return name;
        }
        /**
         * @param name
         *            The name to set.
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return Returns the password.
         */
        public String getPassword() {
            return password;
        }
        /**
         * @param password
         *            The password to set.
         */
        public void setPassword(String password) {
            this.password = password;
        }
        //constructor
        public Customer() {
        }

    }


    5
    。使用myeclipse插件建立hibernate.cfg.xml   位于src目錄下
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>

    <session-factory> 

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">
    jdbc:mysql://127.0.0.1:3306/hello</property>
        <property name="myeclipse.connection.profile">
    hbmysql</property>

    <property name="connection.username">root</property>
        <property name="connection.password">
    123</property>
        <mapping resource="hbm/Customer.hbm.xml" />

    </session-factory>

    </hibernate-configuration>


    6
    。在hbm package內(nèi)新建Customer.hbm.xml
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-mapping>
    <class name="hbm.Customer" table="CUSTOMERS">
    <id name="id" column="ID" type="int">
    <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string" not-null="true"/>
    <property name="password" column="PASSWORD" type="string" not-null="true"/>
    </class>

    </hibernate-mapping>


    7
    。使用hibernate操作數(shù)據(jù)庫
    package hbm;

    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import java.util.Iterator;
    import java.util.List;

    public class Hbmain {

        public static SessionFactory sessionFactory;//
    數(shù)據(jù)存儲源
        static {
            try {
                Configuration config = new Configuration().configure();
                sessionFactory = config.buildSessionFactory();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /*
         *
    將一個customer對象存入database
         * @Customer customer Object
         */
        public void saveCustomer(Customer ct) {
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                session.save(ct);
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
                e.printStackTrace();
            } finally {
                session.close();
            }
        }

        /*
         *
    查找所有的customer object
         */
        public void findAll() {
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                List customers = session.createQuery(
                        "from Customer as c order by c.name asc").list();
                Iterator it = customers.iterator();
                System.out.println("append:"+customers.size());
                while(it.hasNext())
                {
                    Customer c = (Customer)it.next();
                    System.out.println("ID:" + c.getId());
                    System.out.println("Name:" + c.getName());
                    System.out.println("Pass:" + c.getPassword());
                }
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
            } finally {
                session.close();
            }

        }

        /*
         *
    修改customer Name
         * @name
         */
        public void loadUpdate(int customer_id, String name) {
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                Customer c = (Customer) session.load(Customer.class, customer_id);
                c.setName(name);
                tx.commit();

            } catch (Exception e) {
                if (tx != null) {
                    tx.rollback();
                }
                e.printStackTrace();

            } finally {
                session.close();
            }

        }
      
        /*
         *
    測試以上的方法
         * save() find() update()
         */
       
        public void test()
        {
            Customer ct = new Customer();
            //ct.setId(5);
            ct.setName("buaa");
            ct.setPassword("5768");
            this.saveCustomer(ct);
            this.findAll();
            this.loadUpdate(ct.getId(),"phop");
        }
       
       
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Hbmain hb = new Hbmain();
            hb.test();
            sessionFactory.close();

        }

    }

    初學(xué)hibernate遇到的幾個問題的解決

    參考書是孫衛(wèi)琴的<精通Hibernate>

    hibernate 3 + mysql 5.0

    書上的例子第2章 初始化 hibernate Configuration實例

    代碼如下:

    Configuration config = new Configuration();

    config.addClass("Customer.class");

    SessionFactory sessionfactory = config.buildSessionFactory();

    配置文件是 hibernate.properties

    運行出錯 :提示為

    org.hibernate.HibernateException: database product name cannot be null

    顯然是配置文件的問題

    我用的配置文件是 hibernate.cfg.xml

    修改為以下代碼 成功運行

    Configuration config = new Configuration().configure();
    SessionFactory sessionFactory = config.buildSessionFactory();

    原因:使用了xm作為配置文件,而沒有選擇properties文件,應(yīng)該使用new Configuration().config();

    書上例子第六章一對多映射

    customer.hbm.xml

    原代碼為

    <hibernate-mapping>
        <class name="demo1.Customer" table="CUSTOMERS">
            <id name="id" type="long" column="ID">
                <generator class="increment" />
            </id>
            <property name="name" type="string">
                <column name="NAME" length="15" />
            </property>
        </class>
    </hibernate-mapping>

    此時執(zhí)行其Business.java時

    Customer customer = (Customer) session.load(Customer.class,
                        new Long(customer_id));

    出現(xiàn)如下錯誤

    1 Exception in thread "main" java.lang.NullPointerException
    2  at org.hibernate.tuple.AbstractTuplizer.createProxy(AbstractTuplizer.java:249)
    3  at org.hibernate.persister.entity.BasicEntityPersister.createProxy(BasicEntityPersister.java:2831)
    4  at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:218)
    5  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:163)
    6  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
    7  at org.hibernate.impl.SessionImpl.load(SessionImpl.java:603)
    8  at org.hibernate.impl.SessionImpl.load(SessionImpl.java:596)

    修改為

    <hibernate-mapping default-lazy="false">
    <class name="demo1.Customer" table="CUSTOMERS">
            <id name="id" type="long" column="ID">
                <generator class="increment" />
            </id>
            <property name="name" type="string">
                <column name="NAME" length="15" />
            </property>
        </class>
    </hibernate-mapping>

    問題解決

    原因在于: Hibernate 3.0 與Hibernate2.1的源代碼是不兼容的
    在Hibernate2.1中,lazy屬性的默認值為“false”,而在Hibernate3.0中,lazy屬性的默認值為“true”。

    posted on 2007-10-10 20:52 譚明 閱讀(1628) 評論(0)  編輯  收藏 所屬分類: Hibernate

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲午夜久久久精品影院| 污污污视频在线免费观看| 国产又大又长又粗又硬的免费视频| 日韩毛片一区视频免费| 亚洲成人在线网站| 永久免费看mv网站入口| 中文在线日本免费永久18近| 亚洲综合在线成人一区| 免费成人黄色大片| 67194国产精品免费观看| 黄色免费网址在线观看| 亚洲理论精品午夜电影| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲国产精品自产在线播放| 久久精品成人免费网站| 亚洲黄色网址在线观看| 免费乱码中文字幕网站| 4虎永免费最新永久免费地址| rh男男车车的车车免费网站| 亚洲av乱码一区二区三区香蕉| 亚洲一区二区视频在线观看| 亚洲免费福利在线视频| 中文字幕无线码免费人妻| 最新国产精品亚洲| 久久久亚洲欧洲日产国码aⅴ | 亚洲AV无码专区日韩| 成人免费观看一区二区| 亚欧乱色国产精品免费视频| 国产成人精品日本亚洲专一区| 亚洲精品中文字幕乱码三区| 国产免费无遮挡精品视频 | 精品国产免费观看| 久久久久av无码免费网| 最新亚洲成av人免费看| 国产av无码专区亚洲av毛片搜| 国产精品亚洲精品观看不卡| 亚洲人成在线影院| 久久精品国产亚洲AV麻豆不卡| 免费在线观看的黄色网址| 我想看一级毛片免费的| 国产精品成人观看视频免费|