-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: weird query issue on hibernate
PostPosted: Thu May 05, 2011 2:52 am 
Beginner
Beginner

Joined: Mon Oct 27, 2008 6:26 am
Posts: 36
Hi All,

I met a weird problem with updating & displaying data in hibernate. Can anyone help me please!?

I am using hibernate, spring with mysql.

The problem here i am facing is, any changes can be applied to database. But if I load updated item on web page, it always returns the old data or new data randomly.
I am sure that it is not a problem of browser cache. I tried to print out return data in getPost method in dao class. It just print out wrong message sometimes.

Say, if I change post content for mutiple times, all changes can be stored in database. But If I continously refresh page to display changed data, it displays all previous changes randomly.

I have tried different ways to load data in getPost method, but still face same problem:

1. tried session.clear, and session.flush
2. close second level cache as :
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_structured_entries">false</prop>
3. different way to load data: session.load, session.get, hibernate query, Criteria, all have same issue.
4. In getPost method of postDAO: I tried to load data by native sql first, and wanted to compare with result of hibernate query. both quries return old data.


Code:

Code:
public class Post implements Cloneable, Serializable {

   private String postID;
   private String content;
}

PostSelectController (controller):

public class PostSelectController extends AbstractController
{
....
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
   {
      String id = request.getParameter("id");
      
      Course course = null;
      Vendor vendor = null;
      Post post = null;
      
      ModelAndView modelAndView = new ModelAndView();

      modelAndView.setViewName(getSuccessView());
      
      post = postService.getPost(id);
      
      modelAndView.addObject("post", post);

      return modelAndView;
   }
}

postService:

@Transactional(propagation=Propagation.SUPPORTS, isolation=Isolation.READ_COMMITTED, readOnly=true)
public class PostService
{
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    public boolean updatePost(Post post) {
      
      System.out.println("service side::::::::::::::::::::::"+(post.getBestAnswer()!=null));
      if(post.getBestAnswer()!=null) System.out.println(">>>>>>>>"+post.getBestAnswer().getPostID());
      System.out.println("service side::::::::::::::::::::::"+(post.getBestAnswer()!=null));;
      
      return this.postDAO.updatePost(post);
   }

    public Post getPost(String postID) {
      return this.postDAO.getPost(postID);
   }
}

postDAO:

public class  PostDAO {
private SessionFactory sessionFactory;
...

public boolean updatePost(Post post) {
      boolean proceed = true;
            
      try {
         
         Session session = sessionFactory.getCurrentSession();
         session.merge(post); //tried session.update, same problem

         session.flush(); //it does not help

      } catch (Exception ex) {
         logger.error(post.getPostID() + " refused :: " + ex.getMessage());
         proceed = false;
      }
      return proceed;
   }


public Post getPost(String postID) {

      Session session = sessionFactory.getCurrentSession();      
      try{
       PreparedStatement st = session.connection()
         .prepareStatement("select content from post where postid='"+postID+"'") ;
         
            ResultSet rs =st.executeQuery();
            while (rs.next()) {
             
             System.out.println("database::::::::::::::::::"+rs.getInt("content"));
            // tried to use native sql to load data from database and compare it with result of hibernate query.
             break;
            }
      }catch(Exception ex){
         
      }
      
      Criteria crit = session.createCriteria(Post.class);
      NaturalIdentifier natId = Restrictions.naturalId();
      natId.set("postID", postID);
      crit.add(natId);
      crit.setCacheable(false);
      
      List<Post> posts = crit.list();
      
      Post post = null;
      if(posts!=null)  post = posts.get(0);
      System.out.println("hibernate::::::::::::::::::"+post.getContent());

      
       return post;
      
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.