本文整理汇总了Java中org.hibernate.StatelessSession类的典型用法代码示例。如果您正苦于以下问题:Java StatelessSession类的具体用法?Java StatelessSession怎么用?Java StatelessSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StatelessSession类属于org.hibernate包,在下文中一共展示了StatelessSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runStatelessHql
import org.hibernate.StatelessSession; //导入依赖的package包/类
private static void runStatelessHql() throws Exception {
Stopwatch watch = Stopwatch.createStarted();
StatelessSession statelessSession = sessionFactory.openStatelessSession();
try {
statelessSession.getTransaction().begin();
Query query = statelessSession
.createQuery(
" SELECT d.id, d.firstName, d.lastName, c.id, c.make " + " FROM Driver d "
+ " LEFT JOIN d.cars c WHERE index(c) LIKE 'Good'").setFetchSize(0).setReadOnly(true);
ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY);
while (scroll.next()) {
LOG.info("Entry " + scroll.get(0));
}
statelessSession.getTransaction().commit();
} catch (Exception ex) {
statelessSession.getTransaction().rollback();
throw ex;
} finally {
statelessSession.close();
}
LOG.info("StatelessHql:=" + watch.toString());
}
示例2: removeLocalization
import org.hibernate.StatelessSession; //导入依赖的package包/类
private void removeLocalization(EntityPersister persister, Object entity) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
List<LocalizedObjectTypes> objType = obj.getLocalizedObjectTypes();
if (objType.size() > 0) {
long key = obj.getKey();
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
org.hibernate.Query query = session.createQuery(
"DELETE FROM LocalizedResource WHERE objectKey = :objectKey AND objectType IN (:objectType)");
query.setParameter("objectKey", key);
query.setParameterList("objectType", objType);
query.executeUpdate();
tx.commit();
session.close();
}
}
}
示例3: createHistory
import org.hibernate.StatelessSession; //导入依赖的package包/类
private void createHistory(EntityPersister persister, Object entity,
ModificationType type) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
if (obj.hasHistory()) {
final DomainHistoryObject<?> hist = HistoryObjectFactory.create(
obj, type, DataServiceBean.getCurrentHistoryUser());
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
session.insert(hist);
tx.commit();
session.close();
if (logger.isDebugLoggingEnabled()) {
logger.logDebug(String.format("%s %s[%s, v=%s]", type,
obj.getClass().getSimpleName(), obj.getKey(),
hist.getObjVersion()));
}
}
}
}
示例4: update
import org.hibernate.StatelessSession; //导入依赖的package包/类
public int update(final String query) {
final int[] updated = {0};
retry(new Runnable() {
@Override
public void run() {
StatelessSession session = getSession();
try {
updated[0] = session.createQuery(query).executeUpdate();
} finally {
session.close();
}
}
});
return updated[0];
}
示例5: peekTicket
import org.hibernate.StatelessSession; //导入依赖的package包/类
public synchronized Ticket peekTicket() throws BillingException {
final Ticket[] ticket = {null};
hibernateHelper.retry(new Runnable() {
@Override
public void run() {
StatelessSession session = hibernateHelper.getSession();
try {
ticket[0] = (Ticket) session.createCriteria(Ticket.class).setMaxResults(1).uniqueResult();;
if (log.isDebugEnabled()) {
log.debug("(HibernateTicketManager) peekTicket " + ticket[0]);
}
} finally {
session.close();
}
}
});
return ticket[0];
}
示例6: openStatelessSession
import org.hibernate.StatelessSession; //导入依赖的package包/类
/**
* Note method sets user session information in the database and opens a connection for this.
*/
public StatelessSession openStatelessSession(Connection connection) {
// NOTE: workaround for this issue:
// http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
final StatelessSession session = delegateSessionFactory.openStatelessSession(connection);
final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
Connection conn = ((SessionImplementor) session).connection();
SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
.getProperty("bbdd.rdbms"));
SessionInfo.setDBSessionInfo(conn);
} finally {
Thread.currentThread().setContextClassLoader(currentLoader);
}
return session;
}
示例7: removeLocalization
import org.hibernate.StatelessSession; //导入依赖的package包/类
private void removeLocalization(EntityPersister persister, Object entity) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
List<LocalizedObjectTypes> objType = obj.getLocalizedObjectTypes();
if (objType.size() > 0) {
long key = obj.getKey();
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
org.hibernate.Query query = session
.createQuery("DELETE FROM LocalizedResource WHERE objectKey = :objectKey AND objectType IN (:objectType)");
query.setParameter("objectKey", Long.valueOf(key));
query.setParameterList("objectType", objType);
query.executeUpdate();
tx.commit();
session.close();
}
}
}
示例8: createHistory
import org.hibernate.StatelessSession; //导入依赖的package包/类
private void createHistory(EntityPersister persister, Object entity,
ModificationType type) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
if (obj.hasHistory()) {
final DomainHistoryObject<?> hist = HistoryObjectFactory
.create(obj, type,
DataServiceBean.getCurrentHistoryUser());
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
session.insert(hist);
tx.commit();
session.close();
if (logger.isDebugLoggingEnabled()) {
logger.logDebug(String.format("%s %s[%s, v=%s]", type, obj
.getClass().getSimpleName(), Long.valueOf(obj
.getKey()), Long.valueOf(hist.getObjVersion())));
}
}
}
}
示例9: executePartialUpdate
import org.hibernate.StatelessSession; //导入依赖的package包/类
private void executePartialUpdate(StatelessSession session, BulkItem bulkItem) {
if (logger.isTraceEnabled()) {
logger.trace("Partial Update Entry [" + bulkItem.toString() + "]");
}
// filter non mapped properties
final Map<String, Object> itemValues = filterItemValue(bulkItem.getTypeName(), bulkItem.getItemValues());
String hql = getPartialUpdateHQL(bulkItem, itemValues);
Query query = session.createQuery(hql);
for (Map.Entry<String, Object> updateEntry : itemValues.entrySet()) {
query.setParameter(updateEntry.getKey(), updateEntry.getValue());
}
query.setParameter("id_" +bulkItem.getIdPropertyName() ,bulkItem.getIdPropertyValue());
query.executeUpdate();
}
示例10: exists
import org.hibernate.StatelessSession; //导入依赖的package包/类
protected boolean exists(BulkItem bulkItem, StatelessSession session) {
Criteria criteria = null;
switch (bulkItem.getOperation()) {
case BulkItem.REMOVE:
case BulkItem.WRITE:
case BulkItem.UPDATE:
Object entry = bulkItem.getItem();
criteria = session.createCriteria(entry.getClass().getName());
ClassMetadata classMetaData = getSessionFactory().getClassMetadata(entry.getClass());
criteria.add(Restrictions.idEq(classMetaData.getIdentifier(entry)));
criteria.setProjection(Projections.rowCount());
return ((Number) criteria.uniqueResult()).intValue() > 0;
case BulkItem.PARTIAL_UPDATE:
criteria = session.createCriteria(bulkItem.getTypeName());
criteria.add(Restrictions.idEq(bulkItem.getIdPropertyValue()));
criteria.setProjection(Projections.rowCount());
return ((Number) criteria.uniqueResult()).intValue() > 0;
default:
return false;
}
}
示例11: testRefresh
import org.hibernate.StatelessSession; //导入依赖的package包/类
public void testRefresh() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Paper paper = new Paper();
paper.setColor( "whtie" );
ss.insert( paper );
tx.commit();
ss.close();
ss = getSessions().openStatelessSession();
tx = ss.beginTransaction();
Paper p2 = ( Paper ) ss.get( Paper.class, paper.getId() );
p2.setColor( "White" );
ss.update( p2 );
tx.commit();
ss.close();
ss = getSessions().openStatelessSession();
tx = ss.beginTransaction();
assertEquals( "whtie", paper.getColor() );
ss.refresh( paper );
assertEquals( "White", paper.getColor() );
ss.delete( paper );
tx.commit();
ss.close();
}
示例12: usingStatelessSession
import org.hibernate.StatelessSession; //导入依赖的package包/类
static boolean usingStatelessSession(Consumer<StatelessSession> action) {
if (sessionFactory != null) {
StatelessSession session = sessionFactory.openStatelessSession();
try {
Transaction transaction = session.beginTransaction();
action.accept(session);
transaction.commit();
return true;
} catch (Throwable e) {
return false;
} finally {
session.close();
}
}
return false;
}
示例13: findStampByInternalItemUid
import org.hibernate.StatelessSession; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <STAMP_TYPE extends Stamp> STAMP_TYPE findStampByInternalItemUid(String internalItemUid,
Class<STAMP_TYPE> clazz) {
try (StatelessSession session = this.openStatelessSession()){
List<Stamp> stamps = (List<Stamp>) session.createNamedQuery("item.stamps.by.uid")
.setParameter("uid", internalItemUid)
.setHint(AvailableSettings.JPA_SHARED_CACHE_STORE_MODE, null)
.setHint(AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE, null)
.getResultList();
for (Stamp stamp : stamps) {
if (clazz.isInstance(stamp)) {
return clazz.cast(stamp);
}
}
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
return null;
}
示例14: executeTransactional
import org.hibernate.StatelessSession; //导入依赖的package包/类
/**
* 지정한 session으로부터 StatelessSession을 생성한 후 작업을 수행하고, 닫습니다. @param session the session
*
* @param action the action
*/
public static void executeTransactional(Session session, Action1<StatelessSession> action) {
if (log.isDebugEnabled())
log.debug("StatelessSession을 이용하여 Transaction 하에서 특정 작업을 수행합니다.");
StatelessSession stateless = openStatelessSession(session);
Transaction tx = null;
try {
tx = stateless.beginTransaction();
action.perform(stateless);
tx.commit();
} catch (Exception e) {
log.error("StatelessSession을 이용한 작업에 실패했습니다. rollback 합니다.", e);
if (tx != null)
tx.rollback();
throw new RuntimeException(e);
} finally {
stateless.close();
}
}
示例15: execute
import org.hibernate.StatelessSession; //导入依赖的package包/类
/**
* Execute the actions.
*
* @param session the session
* @param actions the actions
*/
public static void execute(Session session, Iterable<Action1<StatelessSession>> actions) {
if (log.isDebugEnabled())
log.debug("StatelessSession을 이용하여 특정 작업을 수행합니다.");
StatelessSession stateless = openStatelessSession(session);
try {
for (Action1<StatelessSession> action : actions)
action.perform(stateless);
} catch (Exception e) {
log.error("StatelessSession에서 작업이 실패했습니다.", e);
throw new RuntimeException(e);
} finally {
stateless.close();
}
}