/** {@inheritDoc} */ @SuppressWarnings({"unchecked", "RedundantTypeArguments"}) @Override public V load(@Nullable GridCacheTx tx, K key) throws GridException { init(); if (log.isDebugEnabled()) log.debug("Store load [key=" + key + ", tx=" + tx + ']'); Session ses = session(tx); try { GridCacheHibernateBlobStoreEntry entry = (GridCacheHibernateBlobStoreEntry) ses.get(GridCacheHibernateBlobStoreEntry.class, toBytes(key)); if (entry == null) return null; return fromBytes(entry.getValue()); } catch (HibernateException e) { rollback(ses, tx); throw new GridException("Failed to load value from cache store with key: " + key, e); } finally { end(ses, tx); } }
public void runHibernate() { // ############################################################################ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); tx.commit(); session.close(); // ############################################################################ Session secondSession = HibernateUtil.getSessionFactory().openSession(); Transaction secondTransaction = secondSession.beginTransaction(); List messages = secondSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } secondTransaction.commit(); secondSession.close(); // ############################################################################ Session thirdSession = HibernateUtil.getSessionFactory().openSession(); Transaction thirdTransaction = thirdSession.beginTransaction(); // message.getId() holds the identifier value of the first message Message loadedMessage = (Message) thirdSession.get(Message.class, message.getId()); loadedMessage.setText("Greetings Earthling"); loadedMessage.setNextMessage(new Message("Take me to your leader (please)")); thirdTransaction.commit(); thirdSession.close(); // ############################################################################ // Final unit of work (just repeat the query) // TODO: You can move this query into the thirdSession before the // commit, makes more sense! Session fourthSession = HibernateUtil.getSessionFactory().openSession(); Transaction fourthTransaction = fourthSession.beginTransaction(); messages = fourthSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } fourthTransaction.commit(); fourthSession.close(); // Shutting down the application HibernateUtil.shutdown(); }
public Forum getForumById(int forumId) { // TODO Auto-generated method stub Session session = sessionFactory.openSession(); Transaction tx = null; Forum forum = new Forum(); try { tx = session.beginTransaction(); forum = (Forum) session.get(Forum.class, forumId); // System.out.println("First Name - "+user.getFirstname()); // System.out.println("Last Name - "+user.getLastname()); } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return forum; }
/** {@inheritDoc} */ @SuppressWarnings({"JpaQueryApiInspection", "JpaQlInspection"}) @Override public void remove(@Nullable GridCacheTx tx, K key) throws GridException { init(); if (log.isDebugEnabled()) log.debug("Store remove [key=" + key + ", tx=" + tx + ']'); Session ses = session(tx); try { Object obj = ses.get(GridCacheHibernateBlobStoreEntry.class, toBytes(key)); if (obj != null) ses.delete(obj); } catch (HibernateException e) { rollback(ses, tx); throw new GridException("Failed to remove value from cache store with key: " + key, e); } finally { end(ses, tx); } }
@Override public User getUser(int userId) { // TODO Auto-generated method stub System.out.println("DbHelper->getUser..."); Session session = sessionFactory.openSession(); Transaction tx = null; User user = new User(); try { tx = session.beginTransaction(); user = (User) session.get(User.class, userId); // System.out.println("First Name - "+user.getFirstname()); // System.out.println("Last Name - "+user.getLastname()); } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return user; }
/** * To get a media with the given key stored in the database * * @param session the session to connect to hibernate * @param key the key of the media searched * @return the media looked for */ public Media getByKey(Session session, String key) { return (Media) session.get(Media.class, key); }