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

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

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

    Dict.CN 在線詞典, 英語(yǔ)學(xué)習(xí), 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統(tǒng)計(jì)

    留言簿(23)

    積分與排名

    優(yōu)秀學(xué)習(xí)網(wǎng)站

    友情連接

    閱讀排行榜

    評(píng)論排行榜

    基于XDoclet的Hibernate3企業(yè)級(jí)開(kāi)發(fā)教程 one2one映射類(lèi)型(轉(zhuǎn))

    摘要:此為我給公司內(nèi)部新員工培訓(xùn)的實(shí)戰(zhàn)演示例子,傻瓜級(jí)教程,講述了開(kāi)發(fā)中的注意要點(diǎn)和常見(jiàn)錯(cuò)誤,目的主要是讓他們適應(yīng)企業(yè)級(jí)快速流水作業(yè)。由于是面對(duì)面講解,所以沒(méi)有詳細(xì)的文檔,現(xiàn)在簡(jiǎn)單整理如下,希望對(duì)入門(mén)者有幫助。

    培訓(xùn)的目標(biāo):對(duì)下面的開(kāi)發(fā)過(guò)程和模式快速理解和應(yīng)用。基于我的UML架構(gòu)-----Java POJOs代碼------〉在pojos中做xdoclet標(biāo)識(shí)-------〉基于ant生成*.hbm.xml文件(借助于eclipse可以自動(dòng)化配置)------〉生成database schma和數(shù)據(jù)庫(kù)sql語(yǔ)句。逐步可以讓新員工過(guò)渡到java5annotation來(lái)開(kāi)發(fā)EJB3 .

     

    基于主鍵的一對(duì)一關(guān)聯(lián)映射

    說(shuō)明:下面是我們用到的例子,學(xué)生和自己學(xué)籍上的照片是一對(duì)一關(guān)系。下面是使用IBM RSA得到的一個(gè)模型,途中用的是兩個(gè)用途,表示的意思比較泛,最佳表示應(yīng)該是一條無(wú)方向的線段,兩邊各標(biāo)上1,代表一對(duì)一關(guān)系。(現(xiàn)在時(shí)間比較緊,所以沒(méi)來(lái)的急修改,這也是IBM反向工程的一個(gè)弱點(diǎn),就好比目標(biāo)是北京,但是現(xiàn)在目標(biāo)指向了中國(guó),不夠精確,但是可以反映這個(gè)意思)

    Model1.jpg

    下面是生成的數(shù)據(jù)庫(kù)E-R圖:

    one2one2主鍵映射-1.jpg

    問(wèn)題:初學(xué)者總是對(duì)pojos對(duì)象中的字段和數(shù)據(jù)庫(kù)中的字段做對(duì)比,對(duì)pojos對(duì)象中多出的propertity感到不解,其實(shí)這也是hibernate的關(guān)鍵所在,在那個(gè)表增加字段,增加外鍵,都是有規(guī)律的,也是完全符合數(shù)據(jù)庫(kù)的規(guī)則的,詳細(xì)看代碼.

    初學(xué)者應(yīng)該注意點(diǎn),需要自己進(jìn)行實(shí)踐的操作:

    1,  /**
     * @hibernate.class table = "image" schema = "hibernate_tutorial" 
     @author Administrator
     *
     */

    schema對(duì)mysql數(shù)據(jù)庫(kù)來(lái)說(shuō),是數(shù)據(jù)庫(kù)名字,這點(diǎn)注意

    2,  你可以在關(guān)聯(lián)屬性上都定義外鍵,你看一下最后那邊的設(shè)置在數(shù)據(jù)庫(kù)sql語(yǔ)句中體現(xiàn)了,那個(gè)沒(méi)體現(xiàn),我想你會(huì)有收獲的。

    3,    /**
       * @hibernate.one-to-one class = "com.javawebside.one2one.p1.Image" 
       * constrained = "false" outer-join = "true" cascade="all"
       @return Returns the image.
       */
    注意cascade的設(shè)置,如果沒(méi)有級(jí)聯(lián),那么你可以最后save  student,或者save image,或者,兩者都保存,你看一下數(shù)據(jù)庫(kù)中實(shí)際存入的數(shù)據(jù),你一定會(huì)對(duì)cascade有更加深入的認(rèn)識(shí)。

     

    Image類(lèi)

     

    package com.javawebside.one2one.p1;

    import java.sql.Blob;
    /**
     * @hibernate.class table = "image" schema = "hibernate_tutorial" 
     @author Administrator
     *
     */
    public class Image {

      private Long id;

      private String name;

      private Blob value = null;
      
      
      /**
       @link association
       */
      
      private Student student;
      
      public Image(){}
      public Image(Long id, String name, Blob value) {
        super();
        this.id = id;
        this.name = name;
        this.value = value;
      }

      /**
       * @hibernate.id column = "id" generator-class = "foreign"
       * @hibernate.generator-param name = "property" value = "student"
       @return Returns the id.
       */
      public Long getId() {
        return id;
      }

      /**
       @param id
       *            The id to set.
       */
      public void setId(Long id) {
        this.id = id;
      }

      /**
       * @hibernate.property column = "name" not-null = "false" unique = "false"
       @return Returns the name.
       */
      public String getName() {
        return name;
      }

      /**
       @param name
       *            The name to set.
       */
      public void setName(String name) {
        this.name = name;
      }

      /**
       * @hibernate.property column = "value" not-null = "false" unique = "false"
       @return Returns the value.
       */
      public Blob getValue() {
        return value;
      }

      /**
       @param value
       *            The value to set.
       */
      public void setValue(Blob value) {
        this.value = value;
      }

      /**
       * @hibernate.one-to-one class = "com.javawebside.one2one.p1.Student" constrained = "true"
       * foreign-key="forein_key_name"
       @return
       */
      public Student getStudent() {
        return student;
      }

      public void setStudent(Student student) {
        this.student = student;
      }

    }

    Student類(lèi)

    package com.javawebside.one2one.p1;

    /**
     
     * @hibernate.class table = "student" schema = "hibernate_tutorial"
     @author Administrator
     
     */
    public class Student {

      private Long id;

      private String name;

      private String intro;

      private Image image;

      
      
      public Student() {
      }

      public Student(Long id, String name, String intro, Image image) {
        super();
        this.id = id;
        this.name = name;
        this.intro = intro;
        this.image = image;
      }

      /**
       * @hibernate.id column = "id" generator-class = "native"
       @return Returns the id.
       */
      public Long getId() {
        return id;
      }

      /**
       @param id
       *            The id to set.
       */
      public void setId(Long id) {
        this.id = id;
      }

      /**
       * @hibernate.property column = "intro" not-null = "false" unique = "false"
       @return Returns the intro.
       */
      public String getIntro() {
        return intro;
      }

      /**
       @param intro
       *            The intro to set.
       */
      public void setIntro(String intro) {
        this.intro = intro;
      }

      /**
       * @hibernate.property column = "name" not-null = "false" unique = "false"
       @return Returns the name.
       */
      public String getName() {
        return name;
      }

      /**
       @param name
       *            The name to set.
       */
      public void setName(String name) {
        this.name = name;
      }

      /**
       * @hibernate.one-to-one class = "com.javawebside.one2one.p1.Image" 
       * constrained = "false" outer-join = "true" cascade="all"
       @return Returns the image.
       */
      public Image getImage() {
        return image;
      }

      /**
       @param image
       *            The image to set.
       */
      public void setImage(Image image) {
        this.image = image;
      }

    }

    下面是實(shí)際的操作類(lèi)
    BusinessService類(lèi)用于增加和刪除數(shù)據(jù)操作

    package com.javawebside.one2one.p1;

    import java.sql.Blob;
    import org.hibernate.*;
    //import org.hibernate.cfg.Configuration;

    //import java.util.*;

    public class BusinessService {
      private  Session session;

      public  void setSession(Session se) {
        session = se;
      }

      public  Session getSession() {
        return session;
      }

      public void saveStudent(Student Student) throws Exception {
        Transaction tx = null;
        try {
          tx = session.beginTransaction();
          session.save(Student);
          tx.commit();

        catch (Exception e) {
          if (tx != null) {
            tx.rollback();
          }
          throw e;
        finally {
          // No matter what, close the session
          session.close();
        }
      }

      public Student loadStudent(Long id) throws Exception {
        Transaction tx = null;
        try {
          tx = session.beginTransaction();
          Student Student = (Student) session.load(Student.class, id);
          tx.commit();

          return Student;

        catch (Exception e) {
          if (tx != null) {
            tx.rollback();
          }
          throw e;
        finally {
          // No matter what, close the session
          session.close();
        }
      }

      public void printStudent(Student Student) throws Exception {
        Image image = Student.getImage();
        System.out.println("Image of " + Student.getName() + " is: "
            + image.getValue());

        if (image.getStudent() == null)
          System.out.println("Can not naviagte from Image to Student.");
      }

      public void test() throws Exception {

        Student student = new Student();
        Image image = new Image();
        image.setName("
    王東照片");

        student.setName("Tom");
        student.setIntro("
    王東");
        
        image.setStudent(student);
        student.setImage(image);
        
        saveStudent(student);

        // student=loadStudent(student.getId());
        // printStudent(student);

      }

      public static void main(String args[]) throws Exception {
        BusinessService bs = new BusinessService();
        bs.setSession(HibernateSessionFactory.currentSession());
        if (bs.getSession() == null)
          System.out.println("Session is null");
        bs.test();
        //bs.getSession().close();
      }
    }

     

    hibernate配置類(lèi)

    package com.javawebside.one2one.p1;

    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;

    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html}.
     */
    public class HibernateSessionFactory {

        /** 
         * Location of hibernate.cfg.xml file.
         * NOTICE: Location should be on the classpath as Hibernate uses
         * #resourceAsStream style lookup for its configuration file. That
         * is place the config file in a Java package - the default location
         * is the default Java package.<br><br>
         * Examples: <br>
         <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
         */
        private static String CONFIG_FILE_LOCATION = "com/javawebside/one2one/p1/hibernate.cfg.xml";

        /** Holds a single instance of Session */
      private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

        /** The single instance of hibernate configuration */
        private static final Configuration cfg = new Configuration();

        /** The single instance of hibernate SessionFactory */
        private static org.hibernate.SessionFactory sessionFactory;

        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
          if (sessionFactory == null) {
            try {
              cfg.configure(CONFIG_FILE_LOCATION);
              sessionFactory = cfg.buildSessionFactory();
            catch (Exception e) {
              System.err
                  .println("%%%% Error Creating SessionFactory %%%%");
              e.printStackTrace();
            }
          }
          session = (sessionFactory != null) ? sessionFactory.openSession()
              null;
          threadLocal.set(session);
        }

            return session;
        }

        /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);

            if (session != null) {
                session.close();
            }
        }

        /**
         * Default constructor.
         */
        private HibernateSessionFactory() {
        }

    }

     

    posted on 2006-03-11 09:05 都市淘沙者 閱讀(1436) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Hibernate/ORM

    評(píng)論

    # re: 基于XDoclet的Hibernate3企業(yè)級(jí)開(kāi)發(fā)教程 one2one映射類(lèi)型(轉(zhuǎn)) 2006-09-06 10:12 er

    什么公司  回復(fù)  更多評(píng)論   

    # re: 基于XDoclet的Hibernate3企業(yè)級(jí)開(kāi)發(fā)教程 one2one映射類(lèi)型(轉(zhuǎn)) 2009-08-17 08:00 lighting

    能不能在詳細(xì)一點(diǎn)啊  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 亚洲福利视频一区二区三区| 成全高清在线观看免费| 999久久久免费精品播放 | 亚洲国产成人久久精品app | 韩国18福利视频免费观看| 亚洲日韩小电影在线观看| 亚洲国产日韩视频观看| 国产成人免费网站| 久久精品国产亚洲AV蜜臀色欲| www免费插插视频| 亚洲色欲色欲www在线丝| 野花香高清在线观看视频播放免费| 在线播放高清国语自产拍免费 | 成人免费a级毛片无码网站入口| 亚洲精品午夜无码电影网| 国产免费无码AV片在线观看不卡 | 亚洲精品无码日韩国产不卡?V| 亚洲国产成人久久综合一区| 99久久99久久精品免费看蜜桃| 国产成人无码免费视频97 | 亚洲桃色AV无码| 国产午夜成人免费看片无遮挡| 性做久久久久免费观看| 深夜A级毛片视频免费| 3d动漫精品啪啪一区二区免费| 亚洲精品无码99在线观看| 亚洲啪AV永久无码精品放毛片| 最近中文字幕完整免费视频ww| 亚洲欧洲日产国码无码网站| 午夜影院免费观看| 亚洲中文字幕久久久一区| 亚洲第一页综合图片自拍| 99久久国产免费中文无字幕| 亚洲色偷偷偷综合网| 亚洲国产成人精品无码久久久久久综合| 亚洲乱色伦图片区小说| 成人爽A毛片免费看| 精品无码国产污污污免费网站国产| 亚洲Aⅴ无码一区二区二三区软件| 亚洲欧美国产精品专区久久| 中文字幕亚洲综合久久菠萝蜜|