@Override public T findById(String id, boolean lazy) { T result = null; String className = getSimpleClassName(); LOG.debug("Searching {} entity by id [{}] ", className, id); if (isNotBlank(id)) { Session session = getSession(); if (lazy) { result = (T) session.load(getEntityClass(), Long.parseLong(id)); } else { result = (T) session.get(getEntityClass(), Long.parseLong(id)); } } if (LOG.isTraceEnabled()) { LOG.trace("[{}] Search result: {}.", id, result); } else { LOG.debug("[{}] Search result: {}.", id, result != null); } return result; }
public void addPersonToEvent(Long personId, Long eventId) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Person aPerson = (Person)session.load(Person.class, personId); Event anEvent = (Event)session.load(Event.class, eventId); aPerson.getEvents().add(anEvent); session.getTransaction().commit(); }
Session s = sf.openSession(); s.getTransaction().begin(); EntityWithConvertibleField entity = new EntityWithConvertibleField(); s.close(); s = sf.openSession(); s.beginTransaction(); entity = (EntityWithConvertibleField) s.load( EntityWithConvertibleField.class, entityID ); assertEquals( ConvertibleEnum.VALUE, entity.getConvertibleEnum() ); s.getTransaction().commit(); assertEquals( "'VALUE'", outcome ); s = sf.openSession(); s.beginTransaction(); s.createQuery( "FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE" ) .list(); s = sf.openSession(); s.beginTransaction(); s.delete( entity ); s.getTransaction().commit();
@Override public Connection getConnectionByToken(String token) { Session session = this.sessionFactory.getSessionFactory().openSession(); return (Connection) session.load(Connection.class, UUID.fromString(token)); } }
public Account getAccount(Long accountId) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Account account = (Account)session.load(Account.class, accountId); session.getTransaction().commit(); return account; }
public static void delete(SessionFactory sessions, Class<?> entityClass, Serializable... ids) { final Session session = sessions.openSession(); Transaction transaction = session.beginTransaction(); for ( Serializable id : ids ) { session.delete( session.load( entityClass, id ) ); } transaction.commit(); session.close(); }
logger.debug("Opening session."); final Session saveSession = sessionFactory.openSession(); logger.debug("Saving the object."); final Session loadSession = sessionFactory.openSession(); logger.debug("Loading the object."); final Object loadedObject = loadSession.load(object.getClass(), id); logger.debug("Closing the session."); loadSession.close();
public void addPersonToEvent(Long personId, Long eventId) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Person aPerson = (Person)session.load(Person.class, personId); Event anEvent = (Event)session.load(Event.class, eventId); aPerson.getEvents().add(anEvent); session.getTransaction().commit(); }
/** * Removes the mutations that have a result from the given list of * mutations. * * @param mutations * the list of mutations to be filtered. */ private static void filterMutationsWithResult(List<Mutation> mutations) { if (mutations != null) { // make sure that we have not got any mutations that have already an // result Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); List<Mutation> toRemove = new ArrayList<Mutation>(); for (Mutation m : mutations) { session.load(m, m.getId()); if (m.getMutationResult() != null) { logger.debug("Found mutation that already has a mutation result " + m); toRemove.add(m); } } mutations.removeAll(toRemove); tx.commit(); session.close(); } }
@Test @TestForIssue(jiraKey = "HHH-10677") public void testLocalQueryInvalidatedImmediatelly() { Session s1 = sf1.openSession(); Person person = new Person("John", "Smith", 29); s1.persist(person); Session s2 = qsf.openSession(); List<Person> list1 = s2.createCriteria(Person.class).setCacheable(true).add(Restrictions.le("age", 29)).list(); assertEquals(1, list1.size()); s2.close(); Session s3 = qsf.openSession(); Person p2 = s3.load(Person.class, person.getName()); p2.setAge(30); s3.persist(p2);
private void addAuthority() { User user1 = (User) getSession().load(User.class, user1Id); UserDto userDto1 = mapperFacade.map(user1, UserDto.class); log.debug("\n\n old userTO = " + userDto1); Authority authority = (Authority) getSession().load(Authority.class, adminAuthotityId); AuthorityDto authorityDto = mapperFacade.map(authority, AuthorityDto.class); assertFalse(userDto1.getAuthorities().contains(authorityDto)); userDto1.getAuthorities().add(authorityDto); Assert.assertNotNull(user1); mapperFacade.map(userDto1, user1, TypeFactory.valueOf(UserDto.class), TypeFactory.valueOf(User.class)); assertTrue(user1.getAuthorities().contains(authority)); }
public Account getAccount(Long accountId) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Account account = (Account)session.load(Account.class, accountId); session.getTransaction().commit(); return account; }
Transaction tx = null; try{ session = dao.sessionFactory.openSession(); //.getCurrentSession(); it has to be managed manually tx = session.beginTransaction(); switch(operation){ case CREATE: session.delete(entity); }else{ entity = (E) session.load(dao.clazz, id); session.delete(entity);
public void testEntityResurrection() { final Long id = 5L; Session session = getSessionFactory().openSession(); session.getTransaction().begin(); Object loadedDocument = session.load( AlternateDocument.class, id ); session.delete( loadedDocument ); session.getTransaction().commit();
private void destroyTestBaseData() { Session session = openSession(); Transaction txn = session.beginTransaction(); for ( Long createdAnimalId : createdAnimalIds ) { Animal animal = session.load( Animal.class, createdAnimalId ); session.delete( animal ); } txn.commit(); session.close(); createdAnimalIds.clear(); }
Session session = sf.openSession(); Transaction t = session.beginTransaction(); session.close(); session = sf.openSession(); t = session.beginTransaction(); session.close(); session = sf.openSession(); t = session.beginTransaction(); order = (Orders) session.load(Orders.class, Integer.valueOf(1) ); Assert.assertFalse(Hibernate.isInitialized(order) ); Assert.assertFalse(Hibernate.isInitialized(order.getItemsForOrderId() ) );
ut.begin(); try { Session session = sessionFactory.openSession(); assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus()); item = new Item("anItem", "An item owned by someone"); ut.begin(); try { Session session = sessionFactory.openSession(); assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus()); Item found = (Item) session.load(Item.class, item.getId()); Statistics stats = session.getSessionFactory().getStatistics(); log.info(stats.toString()); ut.begin(); try { Session session = sessionFactory.openSession(); assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus()); assertNull(session.get(Item.class, item.getId()));