在类声明一个session private Session session=HibernateSessionFactory.getSession();在保存方法里面保存,更新订单
session.saveOrUpdate(p);//减去数量
session.saveOrUpdate(or);//保存订单
然后读取出来。
public Order get(String uuid){
return (Order)session.get(Order.class,uuid);
}结果数据一下读出来是null,一会又有。有时读不到,有时出现。

解决方案 »

  1.   

    数据应与数据库里的数据同步,加上事务提交语句。在对数据库完成操作以后加上commit
      

  2.   

    session.flush();
    transaction.commit();在最后我加了,我现在还发现,过一段时间就会一直出现数据如果是刚刚保存,就会出现一时有数据,一时没有。
    我现在希望保存了,马上就出现。而且刷新的时候不会一时有,一时无的情况
      

  3.   

    session.flush();去掉试下或者
            transaction.commit();
    完成以后,最后关闭sessionHibernateSessionFactory.close。(具体代码记不清了)
      

  4.   

    这个估计是HIbernate的机制问题吧,不是很清楚,到网上看看有没有这样的优化配置
      

  5.   

    感觉是你的Sessions没有管理好。
    每次你都直接从工厂拿Sessions,也没看见关闭session的代码。这比较危险。也就是问题的根源。你还是老老实实的写一个HibernUtil的类吧,Session与具体线程捆绑就没有问题了。
    HibernateUtil对外提供打开关闭session 的接口。
      

  6.   

    如果使用的都是当前的session,那是有数据的。不管你是否真的存入数据库。你可以在保存的时候使用事务保存。你的事务没配置好引起的问题。只要数据真存入数据库就解决了。
      

  7.   

    现在就是数据库里面有。好像session.get()的时候没有去读取mysql
      

  8.   

    你说得很有道理。而且我现在对hibernate的session应该怎么取得。还要不要每个都关闭。都不是很清楚。你可以发一个hiberntaeutil操作的类给我吗?
      

  9.   


      Configuration   configuration   =   new   Configuration();   
      configuration.configure();     
      SessionFactory   sessionFac   =   configuration.buildSessionFactory();   
      Session   session   =   sessionFac.openSession();   
      Transaction   t   =   session.beginTransaction();   
       ……
      
      t.commit();   
      session.close();   
      sessionFac.close();   
      

  10.   

    我使用的是 myeclipse自动生成的 factory
    public class HibernateSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        } /**
         *  Rebuild hibernate session factory
         *
         */
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    } /**
         *  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();
            }
        } /**
         *  return session factory
         *
         */
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    } /**
         *  return session factory
         *
         * session factory will be rebuilded in the next call
         */
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    } /**
         *  return hibernate configuration
         *
         */
    public static Configuration getConfiguration() {
    return configuration;
    }}
      

  11.   


    想来想去还是你事务提交的问题,是不是事务提交的位置有问题。建议你把“private Session session=HibernateSessionFactory.getSession();”去掉在每个方法里面用session之前写上”private Session session=HibernateSessionFactory.getSession();“,用完之后再关闭session。另外建议不要使用saveOrUpdate()方法,直接用save()或者update()方法。
      

  12.   

    写错了,是在方法里面写”Session session=HibernateSessionFactory.getSession();“
      

  13.   


    我现在更新,保存,删除都是用完session就关闭。
    public static void update(Object obj) throws Exception{
        Session session=null;
        try {
            session=HibernateSessionFactory.getSession();
            session.beginTransaction().begin();
            session.update(obj);
            session.beginTransaction().commit();
            session.flush();
            session.clear();
        } catch (Exception e) {
            session.beginTransaction().rollback();//事务回滚
            throw e;
        }finally{  
         if(session!=null)
            session.close();
        }
    }可是他有时候还是不去访问数据库。这个太麻烦了。我都不知道怎么弄的
      

  14.   


    public static void update(Object obj) throws Exception{
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateSessionFactory.getSession(); //我通常都是一个DAO一个SESSION
            // session.beginTransaction().begin();
            transaction = session.beginTransaction();//我通常是这样写 虽然有人说一个
    //Session只有一个事务 但是我也没看过里面的实现 不知道
    //是否创建的时候也是如此 另外 该事务已经是开始了
            session.update(obj); //更新之前我会先获得这个实体 然后更改实体 然后调用方法
            transaction.commit();
            //session.beginTransaction().commit();
            session.flush(); 
            //session.clear();需要么?
        } catch (Exception e) {
            transaction.rollback();//事务回滚
            throw e;
        }finally{  
            //if(session!=null)
            //session.close(); 一个DAO里应该给一个关闭连接的方法 
        }
    }我通常都是一个DAO在创建的时候分配给他一个SESSION 然后使用结束后关闭SESSION 手写的 可能看起来会不舒服
      

  15.   

    谢谢楼上各位的帮助 。我现在发现了一个问题,就是我在debug中show_sql。结果发现session.get()的时间是没有查询数据库的。
    这个问题怎么解决呢?
      

  16.   


    缓存问题,你设置所有数据都从缓存读了。好像配置文件有个lazy 之类的配置在xml可以指定。
      

  17.   

    <property name="hibernate.cache.use_query_cache">false</property>这个吗?我这里是false
      

  18.   

    学习一下,hibernate忘的差不多了
      

  19.   

    结了。问题就是没有commit()查询的时候也需要session.commit()然后关闭。