/** * @param pckg string with package containing Hibernate entities (classes annotated with Hibernate {@code @Entity} * annotation) e. g. {@code com.codahale.fake.db.directory.entities} */ protected ScanningHibernateBundle(String pckg) { this(pckg, new SessionFactoryFactory()); }
/** * @return a new aspect */ public UnitOfWorkAspect newAspect() { return new UnitOfWorkAspect(sessionFactories); }
/** * Creates a new <b>@UnitOfWork</b> aware proxy of a class with the default constructor. * * @param clazz the specified class definition * @param <T> the type of the class * @return a new proxy */ public <T> T create(Class<T> clazz) { return create(clazz, new Class<?>[]{}, new Object[]{}); }
@POST @UnitOfWork public Person createPerson(Person person) { return peopleDAO.create(person); }
@Override public final void run(T configuration, Environment environment) throws Exception { final PooledDataSourceFactory dbConfig = getDataSourceFactory(configuration); this.sessionFactory = requireNonNull(sessionFactoryFactory.build(this, environment, dbConfig, entities, name())); registerUnitOfWorkListenerIfAbsent(environment).registerSessionFactory(name(), sessionFactory); environment.healthChecks().register(name(), new SessionFactoryHealthCheck( environment.getHealthCheckExecutorService(), dbConfig.getValidationQueryTimeout().orElse(Duration.seconds(5)), sessionFactory, dbConfig.getValidationQuery())); }
private void beginTransaction(UnitOfWork unitOfWork, Session session) { if (!unitOfWork.transactional()) { return; } session.beginTransaction(); }
/** * Returns a typed {@link Query<E>} * * @param queryString HQL query * @return typed query */ protected Query<E> query(String queryString) { return currentSession().createQuery(requireNonNull(queryString), getEntityClass()); }
public SessionFactory build(HibernateBundle<?> bundle, Environment environment, PooledDataSourceFactory dbConfig, ManagedDataSource dataSource, List<Class<?>> entities) { final ConnectionProvider provider = buildConnectionProvider(dataSource, dbConfig.getProperties()); final SessionFactory factory = buildSessionFactory(bundle, dbConfig, provider, dbConfig.getProperties(), entities); final SessionFactoryManager managedFactory = new SessionFactoryManager(factory, dataSource); environment.lifecycle().manage(managedFactory); return factory; }
public void onError() { if (unitOfWork == null || session == null) { return; } try { rollbackTransaction(unitOfWork, session); } finally { onFinish(); } }
/** * Construct an application event listener using the given name and session factory. * * <p/> * When using this constructor, the {@link UnitOfWorkApplicationListener} * should be added to a Jersey {@code ResourceConfig} as a singleton. * * @param name a name of a Hibernate bundle * @param sessionFactory a {@link SessionFactory} */ public UnitOfWorkApplicationListener(String name, SessionFactory sessionFactory) { registerSessionFactory(name, sessionFactory); }
public void afterEnd() { if (unitOfWork == null || session == null) { return; } try { commitTransaction(unitOfWork, session); } catch (Exception e) { rollbackTransaction(unitOfWork, session); throw e; } // We should not close the session to let the lazy loading work during serializing a response to the client. // If the response successfully serialized, then the session will be closed by the `onFinish` method }
/** * Returns a named {@link Query}. * * @param queryName the name of the query * @return the named query * @see Session#getNamedQuery(String) */ protected Query<?> namedQuery(String queryName) throws HibernateException { return currentSession().getNamedQuery(requireNonNull(queryName)); }
protected ScanningHibernateBundle(String[] pckgs, SessionFactoryFactory sessionFactoryFactory) { super(findEntityClassesFromDirectory(pckgs), sessionFactoryFactory); }
@Override public RequestEventListener onRequest(RequestEvent event) { return new UnitOfWorkEventListener(methodMap, sessionFactories); }
public SessionFactory build(HibernateBundle<?> bundle, Environment environment, PooledDataSourceFactory dbConfig, List<Class<?>> entities, String name) { final ManagedDataSource dataSource = dbConfig.build(environment.metrics(), name); return build(bundle, environment, dbConfig, dataSource, entities); }
@GET @UnitOfWork public List<Person> listPeople() { return peopleDAO.findAll(); }
/** * Creates a new {@link CriteriaQuery} for {@code <E>}. * * @return a new {@link CriteriaQuery} query */ protected CriteriaQuery<E> criteriaQuery() { return this.currentSession().getCriteriaBuilder().createQuery(getEntityClass()); }
/** * Creates a new <b>@UnitOfWork</b> aware proxy of a class with an one-parameter constructor. * * @param clazz the specified class definition * @param constructorParamType the type of the constructor parameter * @param constructorArguments the argument passed to the constructor * @param <T> the type of the class * @return a new proxy */ public <T> T create(Class<T> clazz, Class<?> constructorParamType, Object constructorArguments) { return create(clazz, new Class<?>[]{constructorParamType}, new Object[]{constructorArguments}); }
UnitOfWorkEventListener(ConcurrentMap<ResourceMethod, Optional<UnitOfWork>> methodMap, Map<String, SessionFactory> sessionFactories) { this.methodMap = methodMap; unitOfWorkAspect = new UnitOfWorkAspect(sessionFactories); }
/** * @return a new aspect * @param sessionFactories */ public UnitOfWorkAspect newAspect(Map<String, SessionFactory> sessionFactories) { return new UnitOfWorkAspect(sessionFactories); } }