首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请教有关hibernate3的数据库更新有关问题

2012-03-16 
请问有关hibernate3的数据库更新问题使用eclipse自带的hibernate插件,自动生成java、xml等配置文件结果在使

请问有关hibernate3的数据库更新问题
使用eclipse自带的hibernate插件,自动生成java、xml等配置文件

结果在使用dao进行数据库更新的时候发现数据库数据并未更新。以下为相关java文件,请大大们帮忙看看问题出在哪里。


/**
*     TestVisitor.java
*/

public   class   TestVisitor   {

                public   static   void   main(String[]   args){
                                VisitorDAO   dao=new   VisitorDAO();
                                List   listtop=dao.findByTop(1);
                                for(int   i=0;i <listtop.size();i++){
                                                Visitor   ob=(Visitor)listtop.get(i);
                                                System.out.println(ob.getIp());
                                                ob.setIp( "211.147.72.163 ");
                                                dao.save(ob);
                                                System.out.println(ob.getIp());
                                }
                                System.out.println( "------------------- ");
                }

}


/**
  *   AbstractVisitor   generated   by   MyEclipse   -   Hibernate   Tools
  */

public   abstract   class   AbstractVisitor     implements   java.io.Serializable   {


        //   Fields        

          private   Long   id;
          private   String   ip;

        //   Constructors

        /**   default   constructor   */
        public   AbstractVisitor()   {
        }

                /**   minimal   constructor   */
        public   AbstractVisitor(Long   id)   {
                this.id   =   id;
        }
       
        /**   full   constructor   */
        public   AbstractVisitor(Long   id,String   ip)   {
                this.id   =   id;
                this.ip   =   ip;


        }

     
        //   Property   accessors

        public   Long   getId()   {
                return   this.id;

        }
       
        public   void   setId(Long   id)   {
                this.id   =   id;
        }

        public   String   getIp()   {
                return   this.ip;
        }
       
        public   void   setIp(String   ip)   {
                this.ip   =   ip;
        }
}


/**
  *   Visitor   generated   by   MyEclipse   -   Hibernate   Tools
  */


public   class   Visitor   extends   AbstractVisitor   implements   java.io.Serializable   {

        //   Constructors

        /**   default   constructor   */
        public   Visitor()   {
        }

                /**   minimal   constructor   */
        public   Visitor(Long   id)   {
                super(id);                
        }
       
        /**   full   constructor   */
        public   Visitor(Long   id,String   ip)   {
                super(id,ip);                
        }
     
}


/**
  *   Data   access   object   (DAO)   for   domain   model   class   Visitor.
  *   @see   Visitor
  *   @author   MyEclipse   -   Hibernate   Tools
  */
public   class   VisitorDAO   extends   BaseHibernateDAO   {

        private   static   final   Log   log   =   LogFactory.getLog(VisitorDAO.class);


                //property   constants
                public   static   final   String   IP   =   "ip ";

       
        public   void   save(Visitor   transientInstance)   {
                log.debug( "saving   Visitor   instance ");
                try   {
                        getSession().save(transientInstance);
                        log.debug( "save   successful ");


                }   catch   (RuntimeException   re)   {
                        log.error( "save   failed ",   re);
                        throw   re;
                }
        }
       
                public   void   delete(Visitor   persistentInstance)   {
                log.debug( "deleting   Visitor   instance ");
                try   {
                        getSession().delete(persistentInstance);
                        log.debug( "delete   successful ");
                }   catch   (RuntimeException   re)   {
                        log.error( "delete   failed ",   re);
                        throw   re;
                }
        }
       
        public   Visitor   findById(   java.lang.Long   id)   {
                log.debug( "getting   Visitor   instance   with   id:   "   +   id);
                try   {
                        Visitor   instance   =   (Visitor)   getSession()
                                        .get( "Visitor ",   id);
                        return   instance;
                }   catch   (RuntimeException   re)   {
                        log.error( "get   failed ",   re);
                        throw   re;
                }
        }
       
       
        public   List   findByExample(Visitor   instance)   {
                log.debug( "finding   Visitor   instance   by   example ");
                try   {
                        List   results   =   getSession()

                                        .createCriteria( "Visitor ")
                                        .add(Example.create(instance))


                        .list();
                        log.debug( "find   by   example   successful,   result   size:   "   +   results.size());
                        return   results;
                }   catch   (RuntimeException   re)   {
                        log.error( "find   by   example   failed ",   re);
                        throw   re;
                }

        }        
       
        public   List   findByProperty(String   propertyName,   Object   value)   {
                log.debug( "finding   Visitor   instance   with   property:   "   +   propertyName
                            +   ",   value:   "   +   value);
                try   {
                      String   queryString   =   "from   Visitor   as   model   where   model. "  
                                                                                                                +   propertyName   +   "=   ? ";
                      Query   queryObject   =   getSession().createQuery(queryString);
                                  queryObject.setParameter(0,   value);
                                  return   queryObject.list();
                }   catch   (RuntimeException   re)   {
                      log.error( "find   by   property   name   failed ",   re);
                      throw   re;
                }
                }
       
        public   List   findByOrder()   {
                log.debug( "finding   Visitor   instance   with   order. ");
                try   {
                      String   queryString   =   "from   Visitor   order   by   id   desc ";


                      Query   queryObject   =   getSession().createQuery(queryString);
                                  return   queryObject.list();
                }   catch   (RuntimeException   re)   {
                      log.error( "find   by   order   failed ",   re);
                      throw   re;
                }
                }
       
        public   List   findByTop(java.lang.Integer   n)   {
                log.debug( "finding   Visitor   instance   with   top   "+n+ "   record   order   by   id   desc. ");
                try   {
                      String   queryString   =   "from   Visitor   order   by   id   desc ";
                      Query   queryObject   =   getSession().createQuery(queryString);
                      queryObject.setFirstResult(0);
                      queryObject.setMaxResults(n);
                                  return   queryObject.list();
                }   catch   (RuntimeException   re)   {
                      log.error( "find   by   top   "+   n   + "   failed ",   re);
                      throw   re;
                }
                }

                public   List   findByIp(Object   ip)   {
                                return   findByProperty(IP,   ip);
                }

                public   Visitor   merge(Visitor   detachedInstance)   {
                log.debug( "merging   Visitor   instance ");
                try   {
                        Visitor   result   =   (Visitor)   getSession()
                                        .merge(detachedInstance);
                        log.debug( "merge   successful ");


                        return   result;
                }   catch   (RuntimeException   re)   {
                        log.error( "merge   failed ",   re);
                        throw   re;
                }
        }

        public   void   attachDirty(Visitor   instance)   {
                log.debug( "attaching   dirty   Visitor   instance ");
                try   {
                        getSession().saveOrUpdate(instance);
                        log.debug( "attach   successful ");
                }   catch   (RuntimeException   re)   {
                        log.error( "attach   failed ",   re);
                        throw   re;
                }

        }
       
        public   void   attachClean(Visitor   instance)   {
                log.debug( "attaching   clean   Visitor   instance ");
                try   {
                        getSession().lock(instance,   LockMode.NONE);
                        log.debug( "attach   successful ");
                }   catch   (RuntimeException   re)   {
                        log.error( "attach   failed ",   re);
                        throw   re;
                }
        }
}


//Visitor.hbm.xml

?xml   version= "1.0 "?>
<!DOCTYPE   hibernate-mapping   PUBLIC   "-//Hibernate/Hibernate   Mapping   DTD   3.0//EN "
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<!--  
        Mapping   file   autogenerated   by   MyEclipse   -   Hibernate   Tools
-->
<hibernate-mapping>
        <class   name= "Visitor "   table= "Visitor "   schema= "dbo "   catalog= "Status ">
                <id   name= "id "   type= "java.lang.Long ">
                        <column   name= "Id "   precision= "10 "   scale= "0 "   />


                        <generator   class= "assigned "   />
                </id>
                <property   name= "ip "   type= "java.lang.String ">
                        <column   name= "Ip "   length= "15 "   />
                </property>
        </class>
</hibernate-mapping>


[解决办法]
你的数据库事务处理的代码呢

public void save(Visitor transientInstance) {
log.debug( "saving Visitor instance ");
try {
getSession().save(transientInstance);
log.debug( "save successful ");
} catch (RuntimeException re) {
log.error( "save failed ", re);
throw re;
}
}

这个方法里面没有看到

你是怎么处理事务的

热点排行