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

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

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

    Dict.CN 在線詞典, 英語學習, 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統計

    留言簿(23)

    積分與排名

    優秀學習網站

    友情連接

    閱讀排行榜

    評論排行榜

    基于XDoclet的Hibernate3企業級開發教程 one2one映射類型(轉)

    摘要:此為我給公司內部新員工培訓的實戰演示例子,傻瓜級教程,講述了開發中的注意要點和常見錯誤,目的主要是讓他們適應企業級快速流水作業。由于是面對面講解,所以沒有詳細的文檔,現在簡單整理如下,希望對入門者有幫助。

    培訓的目標:對下面的開發過程和模式快速理解和應用。基于我的UML架構-----Java POJOs代碼------〉在pojos中做xdoclet標識-------〉基于ant生成*.hbm.xml文件(借助于eclipse可以自動化配置)------〉生成database schma和數據庫sql語句。逐步可以讓新員工過渡到java5annotation來開發EJB3 .

     

    基于主鍵的一對一關聯映射

    說明:下面是我們用到的例子,學生和自己學籍上的照片是一對一關系。下面是使用IBM RSA得到的一個模型,途中用的是兩個用途,表示的意思比較泛,最佳表示應該是一條無方向的線段,兩邊各標上1,代表一對一關系。(現在時間比較緊,所以沒來的急修改,這也是IBM反向工程的一個弱點,就好比目標是北京,但是現在目標指向了中國,不夠精確,但是可以反映這個意思)

    Model1.jpg

    下面是生成的數據庫E-R圖:

    one2one2主鍵映射-1.jpg

    問題:初學者總是對pojos對象中的字段和數據庫中的字段做對比,對pojos對象中多出的propertity感到不解,其實這也是hibernate的關鍵所在,在那個表增加字段,增加外鍵,都是有規律的,也是完全符合數據庫的規則的,詳細看代碼.

    初學者應該注意點,需要自己進行實踐的操作:

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

    schemamysql數據庫來說,是數據庫名字,這點注意

    2,  你可以在關聯屬性上都定義外鍵,你看一下最后那邊的設置在數據庫sql語句中體現了,那個沒體現,我想你會有收獲的。

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

     

    Image

     

    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

    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;
      }

    }

    下面是實際的操作類
    BusinessService類用于增加和刪除數據操作

    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配置類

    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) 評論(2)  編輯  收藏 所屬分類: Hibernate/ORM

    評論

    # re: 基于XDoclet的Hibernate3企業級開發教程 one2one映射類型(轉) 2006-09-06 10:12 er

    什么公司  回復  更多評論   

    # re: 基于XDoclet的Hibernate3企業級開發教程 one2one映射類型(轉) 2009-08-17 08:00 lighting

    能不能在詳細一點啊  回復  更多評論   

    主站蜘蛛池模板: 欧美男同gv免费网站观看| 无码国产精品一区二区免费模式| 在线免费观看一级片| 亚洲中文字幕乱码一区| 毛片a级毛片免费观看免下载| 亚洲电影在线播放| 曰批全过程免费视频在线观看| 成人区精品一区二区不卡亚洲| 啦啦啦手机完整免费高清观看| 亚洲av无码专区国产不乱码| 免费A级毛片无码久久版| 九九九精品视频免费| 亚洲成AV人片在线观看无码| 9420免费高清在线视频| 亚洲日产乱码一二三区别| 成人免费视频国产| 精品免费久久久久国产一区| 亚洲综合在线视频| 在线a毛片免费视频观看| 一级毛片成人免费看a| 久久久久久久久亚洲| 野花高清在线观看免费完整版中文 | 国内精品免费麻豆网站91麻豆 | 亚洲av片不卡无码久久| 国产精品成人免费综合| 黄色网址在线免费观看| 亚洲国产精品无码专区| 久久久久久国产a免费观看黄色大片 | 亚洲经典在线观看| 国产嫩草影院精品免费网址| 永久免费无码网站在线观看个| 久久免费精品一区二区| 亚洲精品一二三区| 亚洲av日韩av无码黑人| va亚洲va日韩不卡在线观看| 波多野结衣免费在线| 国产成人无码区免费内射一片色欲| 亚洲gay片在线gv网站| 亚洲国产综合人成综合网站00| 亚洲色WWW成人永久网址| 免费国产美女爽到喷出水来视频|