JBoss-IDE 1.2.2 簡介 (Hibernate) 3

类别:Java 点击:0 评论:0 推荐:

JBoss-IDE 1.2.2 簡介 (Hibernate) 3:

 

接著是 compute 方法:

 

    /**

    * @param number

    * @return

    *

    * @ejb.interface-method view-type = "remote"

    */

    public double[] compute(int number) {

        if (number < 0) {

            throw new EJBException("Argument should be positive");

        }

       

        double[] suite = new double[number + 1];

        suite[0] = 0;

        if (number == 0) {

            return suite;

        }

       

        // hibernate section

        try {

            Session session = HibernateUtil.currentSession();

            Transaction tx = session.beginTransaction();

           

            // compare the input and db is same result

            int max = ( (Integer) session.iterate("select count(num.value) from tutorial.hibernate.Num num").next() ).intValue();

           

            if (max == number) {    // results already in db

                Query query = session.createQuery("select num from tutorial.hibernate.Num num order by num.value asc");

               

                int i = 1;

                for (Iterator it = query.iterate(); it.hasNext();) {

                    Num num = (Num) it.next();

                    suite[i] = num.getValue();

                    i++;

                }

            }

            else {  // need to calc again

                // clear the table first

                int r = session.delete("from tutorial.hibernate.Num");

               

                Num n = new Num();

               

                suite[1] = 1;

                n.setValue(suite[1]);

                session.save(n);

               

                for (int i = 2; i <= number; i++) {

                    n = new Num();

                   

                    suite[i] = suite[i - 1] + suite[i - 2];

                    n.setValue(suite[i]);

                    session.save(n);

                }

            }

           

            tx.commit();

            HibernateUtil.closeSession();

        }

        catch (HibernateException he){

            he.printStackTrace();

        }

 

        return suite;

}

 

完成後在 Package Explorer 應可看到此圖

最後 compile 一下 project, 右擊 tutorial project -> 'Build Project'

本文地址:http://com.8s8s.com/it/it16715.htm