@Override public void update(RoomVO roomVO) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); session.saveOrUpdate(roomVO); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); throw ex; } }
@Override public RoomVO findByPrimaryKey(Integer roomID) { RoomVO roomVO = null; Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); roomVO = (RoomVO) session.get(RoomVO.class, roomID); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); throw ex; } return roomVO; }
@Override public ScoreVO findByPrimaryKey(Integer score_id) { ScoreVO scoreVO = null; Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); scoreVO = (ScoreVO) session.get(ScoreVO.class, score_id); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); throw ex; } return scoreVO; }
@Override public void delete(Integer score_id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); Query query = session.createQuery("delete ScoreVO where score_id=?"); query.setParameter(0, score_id); System.out.println("刪除的筆數=" + query.executeUpdate()); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); throw ex; } }
@Override public List<RoomVO> getAll() { List<RoomVO> list = null; Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); Query query = session.createQuery(GET_ALL_STMT); list = query.list(); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); throw ex; } return list; }
@Override public List<ScoreVO> getEmpScore(Integer eid) { List<ScoreVO> list = null; Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); Query query = session.createQuery("from ScoreVO where eid=? order by scoreDate "); query.setParameter(0, eid); list = query.list(); session.getTransaction().commit(); } catch (RuntimeException ex) { session.getTransaction().rollback(); } return list; }
/** {@inheritDoc} */ @Override public void txEnd(GridCacheTx tx, boolean commit) throws GridException { init(); Session ses = tx.removeMeta(ATTR_SES); if (ses != null) { Transaction hTx = ses.getTransaction(); if (hTx != null) { try { if (commit) { ses.flush(); hTx.commit(); } else hTx.rollback(); if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); } catch (HibernateException e) { throw new GridException( "Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { ses.close(); } } } }
@AfterClass public static void tearDownAfterClass() throws Exception { final Transaction transaction = session.getTransaction(); if (transaction.isActive()) transaction.commit(); if (session != null) session.close(); }
private void rollbackTransaction(Session session, boolean readOnly) { if (!readOnly) { final Transaction txn = session.getTransaction(); if (txn != null && txn.isActive()) { txn.rollback(); } } }
private void commitTransaction(Session session, UnitOfWork uow) { if (uow.transactional()) { Transaction txn = session.getTransaction(); if (txn != null && txn.isActive()) { txn.commit(); } } }
private List listEvents() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List result = session.createQuery("from Event").list(); session.getTransaction().commit(); return result; }
/** * Ends hibernate session. * * @param ses Hibernate session. * @param tx Cache ongoing transaction. */ private void end(Session ses, GridCacheTx tx) { // Commit only if there is no cache transaction, // otherwise txEnd() will do all required work. if (tx == null) { Transaction hTx = ses.getTransaction(); if (hTx != null && hTx.isActive()) hTx.commit(); ses.close(); } }
private void addEmailToPerson(Long personId, String emailAddress) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person aPerson = (Person) session.load(Person.class, personId); // The getEmailAddresses() might trigger a lazy load of the collection aPerson.getEmailAddresses().add(emailAddress); session.getTransaction().commit(); }
private void addPersonToEvent(Long personId, Long eventId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person aPerson = (Person) session .createQuery("select p from Person p left join fetch p.events where p.id = :pid") .setParameter("pid", personId) .uniqueResult(); // Eager fetch the collection so we can use it detached Event anEvent = (Event) session.load(Event.class, eventId); // If we want to handle it bidirectional and detached, we also need to load this // collection with an eager outer-join fetch, this time with Criteria and not HQL: /* Event anEvent = (Event) session .createCriteria(Event.class).setFetchMode("participants", FetchMode.JOIN) .add( Expression.eq("id", eventId) ) .uniqueResult(); // Eager fetch the colleciton so we can use it detached */ session.getTransaction().commit(); // End of first unit of work aPerson.getEvents().add(anEvent); // aPerson is detached // or bidirectional safety method, setting both sides: aPerson.addToEvent(anEvent); // Begin second unit of work Session session2 = HibernateUtil.getSessionFactory().getCurrentSession(); session2.beginTransaction(); session2.update(aPerson); // Reattachment of aPerson session2.getTransaction().commit(); }
private Long createAndStorePerson(String firstname, String lastname) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person thePerson = new Person(); thePerson.setFirstname(firstname); thePerson.setLastname(lastname); session.save(thePerson); session.getTransaction().commit(); return thePerson.getId(); }
private Long createAndStoreEvent(String title, Date theDate) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit(); return theEvent.getId(); }
/** {@inheritDoc} */ @Override public void onSessionEnd(CacheStoreSession ses, boolean commit) { Session hibSes = ses.attach(null); if (hibSes != null) { try { Transaction tx = hibSes.getTransaction(); if (commit) { hibSes.flush(); if (tx.isActive()) tx.commit(); } else if (tx.isActive()) tx.rollback(); } catch (HibernateException e) { throw new CacheWriterException( "Failed to end store session [tx=" + ses.transaction() + ']', e); } finally { hibSes.close(); } } }
@Test public void test6() { Configuration configuration = new Configuration() // 数据库连接信息 .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver") .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate") .setProperty("hibernate.connection.username", "root") .setProperty("hibernate.connection.password", "root") // 方言 .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect") .setProperty("hibernate.hbm2ddl.auto", "update") // 注册ORM .addAnnotatedClass(User.class); Properties props = configuration.getProperties(); for (Map.Entry entry : props.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println(key); System.out.println(value); } SessionFactory sf = configuration.buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setName("foo"); user.setAge(25); session.save(user); session.getTransaction().commit(); session.close(); }