本文整理汇总了Java中org.hibernate.StatelessSession.beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java StatelessSession.beginTransaction方法的具体用法?Java StatelessSession.beginTransaction怎么用?Java StatelessSession.beginTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.StatelessSession
的用法示例。
在下文中一共展示了StatelessSession.beginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
}
示例2: 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()));
}
}
}
}
示例3: 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();
}
}
}
示例4: 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())));
}
}
}
}
示例5: 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();
}
示例6: 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;
}
示例7: 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();
}
}
示例8: gerar
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
@Path("/gerarTransacoes")
public void gerar() throws IOException {
StatelessSession session = sessionFactory.openStatelessSession();
Random random = new Random();
Transaction transaction = session.beginTransaction();
for (int i = 0; i < 1000000; i++) {
System.out.println("Adicionando transacao " + i);
Transacao transacao = new Transacao();
transacao.setTipoDeTransacao(Tipo.values()[random.nextInt(2)]);
transacao.setData(new GregorianCalendar(random.nextInt(6) + 2006,
random.nextInt(12), random.nextInt(29)));
transacao.setValor(new BigDecimal(random.nextDouble() * 10000));
session.insert(transacao);
}
transaction.commit();
}
示例9: aumentarStatelessSession
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
@Path("/stateless")
public void aumentarStatelessSession(){
long antes = System.currentTimeMillis();
StatelessSession statelessSession = factory.openStatelessSession();
ScrollableResults results = statelessSession.createCriteria(Transacao.class).setFetchSize(QTD_REGISTROS).scroll();
Transaction transaction = statelessSession.beginTransaction();
while(results.next()) {
Transacao transacao = (Transacao) results.get()[0];
transacao.setValor(transacao.getValor().multiply(new BigDecimal("1.1")));
statelessSession.update(transacao);
}
transaction.commit();
long tempo = System.currentTimeMillis()-antes;
System.out.printf("Tempo gasto %dms%n", tempo);
result.include("mensagem", "Atualizacao realizada com sucesso (" + tempo + "ms)");
result.forwardTo(IndexController.class).index();
}
示例10: testStatelessBPCreate
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
@Test
public void testStatelessBPCreate() {
try {
setTestAdminContext();
final StatelessSession session = SessionFactoryController.getInstance().getSessionFactory()
.openStatelessSession();
session.beginTransaction();
for (int i = 0; i < CNT; i++) {
BusinessPartner bp = OBProvider.getInstance().get(BusinessPartner.class);
// Generating random strings for testing
UUID name = UUID.randomUUID();
UUID key = UUID.randomUUID();
bp.setName(name.toString());
bp.setSearchKey(key.toString());
bp.setBusinessPartnerCategory(createReferencedObject(Category.class, TEST_BP_CATEGORY_ID));
// note the following things are currently done in the OBInterceptor
// it is quite easy to add a util method which can do this in a generic
// way for any business object
bp.setOrganization(createReferencedObject(Organization.class, TEST_ORG_ID));
bp.setClient(createReferencedObject(Client.class, TEST_CLIENT_ID));
bp.setCreatedBy(createReferencedObject(User.class, "100"));
bp.setCreationDate(new Date());
bp.setUpdatedBy(createReferencedObject(User.class, "100"));
bp.setUpdated(new Date());
session.insert(BusinessPartner.ENTITY_NAME, bp);
// session.refresh(BusinessPartner.ENTITY_NAME, bp);
Assert.assertTrue(bp.getId() != null);
}
session.getTransaction().commit();
} catch (Exception e) {
throw new OBException(e);
}
}
示例11: testHqlBulk
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testHqlBulk() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Document doc = new Document("blah blah blah", "Blahs");
ss.insert(doc);
Paper paper = new Paper();
paper.setColor( "White" );
ss.insert(paper);
tx.commit();
tx = ss.beginTransaction();
int count = ss.createQuery( "update Document set name = :newName where name = :oldName" )
.setString( "newName", "Foos" )
.setString( "oldName", "Blahs" )
.executeUpdate();
assertEquals( "hql-update on stateless session", 1, count );
count = ss.createQuery( "update Paper set color = :newColor" )
.setString( "newColor", "Goldenrod" )
.executeUpdate();
assertEquals( "hql-update on stateless session", 1, count );
tx.commit();
tx = ss.beginTransaction();
count = ss.createQuery( "delete Document" ).executeUpdate();
assertEquals( "hql-delete on stateless session", 1, count );
count = ss.createQuery( "delete Paper" ).executeUpdate();
assertEquals( "hql-delete on stateless session", 1, count );
tx.commit();
ss.close();
}
示例12: testInitId
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testInitId() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Paper paper = new Paper();
paper.setColor( "White" );
ss.insert(paper);
assertNotNull( paper.getId() );
tx.commit();
tx = ss.beginTransaction();
ss.delete( ss.get( Paper.class, paper.getId() ) );
tx.commit();
ss.close();
}
示例13: saveInventory
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void saveInventory() throws HibernateException, IOException{
File inventoryFile = new File("ghcnm.inv");
BufferedReader br = new BufferedReader(new FileReader(inventoryFile));
String line;
org.hibernate.Session session =
(org.hibernate.Session)GeoInventory.em().getDelegate();
StatelessSession stateless = session.getSessionFactory().openStatelessSession();
try {
stateless.connection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
org.hibernate.Transaction tx = stateless.beginTransaction();
int counter=0;
while ((line = br.readLine()) != null) {
counter++;
GeoInventory inv = new GeoInventory(line);
stateless.insert(inv);
if(counter%1000==0){
tx.commit();
tx = stateless.beginTransaction();
Logger.info("Data Saved: %s", counter);
}
}
tx.commit();
br.close();
stateless.close();
session.clear();
Logger.info("Geoloc Inventory save complete!");
}
示例14: testCreateUpdateReadDelete
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testCreateUpdateReadDelete() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Document doc = new Document("blah blah blah", "Blahs");
ss.insert(doc);
assertNotNull( doc.getName() );
Date initVersion = doc.getLastModified();
assertNotNull( initVersion );
tx.commit();
tx = ss.beginTransaction();
doc.setText("blah blah blah .... blah");
ss.update(doc);
assertNotNull( doc.getLastModified() );
assertNotSame( doc.getLastModified(), initVersion );
tx.commit();
tx = ss.beginTransaction();
doc.setText("blah blah blah .... blah blay");
ss.update(doc);
tx.commit();
Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs");
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
ScrollableResults sr = ss.createQuery("from Document where text is not null")
.scroll(ScrollMode.FORWARD_ONLY);
sr.next();
doc2 = (Document) sr.get(0);
sr.close();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createSQLQuery("select * from Document")
.addEntity(Document.class)
.uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createCriteria(Document.class).uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY);
sr.next();
doc2 = (Document) sr.get(0);
sr.close();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
tx = ss.beginTransaction();
ss.delete(doc);
tx.commit();
ss.close();
}
示例15: onPreUpdate
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
@SuppressWarnings("resource")
@Override
public boolean onPreUpdate(final PreUpdateEvent event)
{
if (log.isDebugEnabled())
{
log.debug("Received pre-update event");
}
final EntityPersister entityPersister = event.getPersister();
final EntityMode entityMode = entityPersister.guessEntityMode(event.getEntity());
final Connection connection = getConnection(entityPersister);
final StatelessSession session = entityPersister
.getFactory()
.openStatelessSession(connection);
session.beginTransaction();
final Serializable entityId = entityPersister.getIdentifier(event.getEntity(),
(SessionImplementor)session);
final Object existingEntity = session.get(event.getEntity().getClass(), entityId);
if (log.isDebugEnabled())
{
log.debug("Loaded existing entity " + existingEntity);
}
boolean updatedExistingEntity = false;
for (int i = 0; i < entityPersister.getPropertyNames().length; i++)
{
final Object oldPropValue = entityPersister.getPropertyValue(existingEntity,
i, entityMode);
if (oldPropValue == null)
{
if (log.isDebugEnabled())
{
log.debug("Found a property in the existing "
+ "entity that was null, checking new entity");
}
final Object newPropValue = entityPersister.getPropertyValue(
event.getEntity(), i, entityMode);
if (!(newPropValue instanceof Collection<?>) && newPropValue != null)
{
if (log.isDebugEnabled())
{
log.debug("The new entity contains a non-null "
+ "value, updating existing entity");
}
entityPersister.setPropertyValue(existingEntity, i, newPropValue,
entityMode);
updatedExistingEntity = true;
}
}
}
if (updatedExistingEntity)
{
entityPersister.getFactory().getCurrentSession().cancelQuery();
session.update(existingEntity);
}
session.getTransaction().commit();
session.close();
return updatedExistingEntity;
}