Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird query issue on hibernate
    primarykey
    data
    text
    <p>I met a weird problem with updating &amp; displaying data in hibernate. Can anyone help me please!?</p> <p>I am using hibernate, spring with mysql.</p> <p>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.</p> <p>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.</p> <p>Say, if I change post content for multiple times, all changes can be stored in database. But If I continuously refresh page to display changed data, it displays all previous changes randomly.</p> <p>I have tried different ways to load data in getPost method, but still face same problem:</p> <ol> <li><p>tried session.clear, and session.flush</p></li> <li><p>close second level cache as :</p> <pre><code>&lt;prop key="hibernate.cache.use_second_level_cache"&gt;false&lt;/prop&gt; &lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt; &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt; &lt;prop key="hibernate.cache.use_structured_entries"&gt;false&lt;/prop&gt; </code></pre></li> <li><p>different way to load data: session.load, session.get, hibernate query, Criteria, all have same issue.</p></li> <li><p>In getPost method of postDAO: I tried to load data by native SQL first, and wanted to compare with result of hibernate query. both queries return old data.</p></li> </ol> <p>Code:</p> <pre><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("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+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&lt;Post&gt; posts = crit.list(); Post post = null; if(posts!=null) post = posts.get(0); System.out.println("hibernate::::::::::::::::::"+post.getContent()); return post; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload