本文整理汇总了Java中com.haulmont.cuba.core.Transaction类的典型用法代码示例。如果您正苦于以下问题:Java Transaction类的具体用法?Java Transaction怎么用?Java Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Transaction类属于com.haulmont.cuba.core包,在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assignLibraryDepartment
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public Collection<BookInstance> assignLibraryDepartment(Collection<BookInstance> bookInstances,
LibraryDepartment libraryDepartment, View bookInstanceView) {
checkPermission(EntityOp.UPDATE);
Collection<BookInstance> mergedInstances = new ArrayList<>();
// Explicit transaction control
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (BookInstance booksInstance : bookInstances) {
BookInstance instance = em.merge(booksInstance, bookInstanceView);
instance.setLibraryDepartment(libraryDepartment);
// Return the updated instance
mergedInstances.add(instance);
}
tx.commit();
}
return mergedInstances;
}
示例2: testOneToOneLazy
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOneToOneLazy() {
System.out.println("===================== BEGIN testOneToOneLazy =====================");
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id);
assertNotNull(oneToOneA);
assertNotNull(oneToOneA.getB());
assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOneLazy =====================");
}
示例3: authenticate
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
@Nonnull
public AuthenticationDetails authenticate(Credentials credentials) throws LoginException {
checkNotNullArgument(credentials, "credentials should not be null");
SecurityContext previousSecurityContext = AppContext.getSecurityContext();
AppContext.setSecurityContext(new SecurityContext(serverSession));
try (Transaction tx = persistence.getTransaction()) {
AuthenticationDetails authenticationDetails = authenticateInternal(credentials);
tx.commit();
userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession());
return authenticationDetails;
} finally {
AppContext.setSecurityContext(previousSecurityContext);
}
}
示例4: testOneToManyLazy
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOneToManyLazy() {
System.out.println("===================== BEGIN testOneToManyLazy =====================");
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
User user = em.find(User.class, userId);
List<UserRole> userRoles = user.getUserRoles();
assertEquals(1, userRoles.size());
for (UserRole ur : userRoles) {
assertNotNull(ur.getRole());
}
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToManyLazy =====================");
}
示例5: setUp
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
User user = new User();
userId = user.getId();
user.setName("testUser");
user.setLogin("testLogin");
user.setPosition("manager");
user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
em.persist(user);
UserRole userRole = new UserRole();
userRoleId = userRole.getId();
userRole.setUser(user);
userRole.setRole(em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93")));
em.persist(userRole);
tx.commit();
} finally {
tx.end();
}
}
示例6: storeAccessToken
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public void storeAccessToken(String tokenValue,
byte[] accessTokenBytes,
String authenticationKey,
byte[] authenticationBytes,
Date tokenExpiry,
String userLogin,
Locale locale,
String refreshTokenValue) {
storeAccessTokenToMemory(tokenValue, accessTokenBytes, authenticationKey, authenticationBytes, tokenExpiry,
userLogin, refreshTokenValue);
if (serverConfig.getRestStoreTokensInDb()) {
try (Transaction tx = persistence.getTransaction()) {
removeAccessTokenFromDatabase(tokenValue);
storeAccessTokenToDatabase(tokenValue, accessTokenBytes, authenticationKey, authenticationBytes,
tokenExpiry, userLogin, locale, refreshTokenValue);
tx.commit();
}
}
clusterManagerAPI.send(new TokenStoreAddAccessTokenMsg(tokenValue, accessTokenBytes, authenticationKey,
authenticationBytes, tokenExpiry, userLogin, refreshTokenValue));
}
示例7: storeAccessTokenToDatabase
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected void storeAccessTokenToDatabase(String tokenValue,
byte[] accessTokenBytes,
String authenticationKey,
byte[] authenticationBytes,
Date tokenExpiry,
String userLogin,
@Nullable Locale locale,
@Nullable String refreshTokenValue) {
try (Transaction tx = persistence.getTransaction()) {
EntityManager em = persistence.getEntityManager();
AccessToken accessToken = metadata.create(AccessToken.class);
accessToken.setCreateTs(timeSource.currentTimestamp());
accessToken.setTokenValue(tokenValue);
accessToken.setTokenBytes(accessTokenBytes);
accessToken.setAuthenticationKey(authenticationKey);
accessToken.setAuthenticationBytes(authenticationBytes);
accessToken.setExpiry(tokenExpiry);
accessToken.setUserLogin(userLogin);
accessToken.setLocale(locale != null ? locale.toString() : null);
accessToken.setRefreshTokenValue(refreshTokenValue);
em.persist(accessToken);
tx.commit();
}
}
示例8: testCreateEntity
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testCreateEntity() throws Exception {
User u = metadata.create(User.class);
u.setLogin("u-" + u.getId());
u.setGroup(companyGroup);
TestBeforeCommitTxListener.test = "testCreateEntity";
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().persist(u);
tx.commit();
} finally {
TestBeforeCommitTxListener.test = null;
}
try (Transaction tx = persistence.createTransaction()) {
User user = persistence.getEntityManager().find(User.class, TestBeforeCommitTxListener.createdEntityId);
assertNotNull(user);
}
}
示例9: migrateAttachmentsBatch
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected int migrateAttachmentsBatch() {
List<SendingAttachment> resultList;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
String qstr = "select a from sys$SendingAttachment a where a.content is not null";
TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
query.setMaxResults(50);
query.setViewName(View.MINIMAL);
resultList = query.getResultList();
tx.commit();
} finally {
tx.end();
}
if (!resultList.isEmpty()) {
emailer.migrateAttachmentsToFileStorage(resultList);
}
return resultList.size();
}
示例10: testCommit
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testCommit() throws Exception {
User u = metadata.create(User.class);
u.setLogin("TxLstnrTst-1-" + u.getId());
u.setGroup(companyGroup);
TestAfterCompleteTxListener.test = "testCommit";
try {
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().persist(u);
tx.commit();
}
} finally {
TestAfterCompleteTxListener.test = null;
}
try (Transaction tx = persistence.createTransaction()) {
User user = persistence.getEntityManager().find(User.class, u.getId());
assertEquals("updated by TestAfterCompleteTxListener", user.getName());
}
}
示例11: testOpenTransaction
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOpenTransaction() throws Exception {
TestingService service = AppBeans.get(TestingService.class);
appender.getMessages().clear();
// programmatic tx without proper completion
Object tx = service.leaveOpenTransaction();
((Transaction) tx).commit();
assertEquals(1, appender.getMessages().stream().filter(s -> s.contains("Open transaction")).count());
appender.getMessages().clear();
// declarative tx
service.declarativeTransaction();
assertEquals(0, appender.getMessages().stream().filter(s -> s.contains("Open transaction")).count());
}
示例12: testUpdateQuery_CleanupMode
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testUpdateQuery_CleanupMode() {
System.out.println("===================== BEGIN testUpdateQuery_CleanupMode =====================");
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
em.setSoftDeletion(false);
Query query = em.createQuery("update sec$Role r set r.description = ?1 where r.name = ?2");
query.setParameter(1, "Updated");
query.setParameter(2, "roleToBeDeleted");
int updated = query.executeUpdate();
assertEquals(1, updated);
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testUpdateQuery_CleanupMode =====================");
}
示例13: loadContentText
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public String loadContentText(SendingMessage sendingMessage) {
SendingMessage msg;
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
msg = em.reload(sendingMessage, "sendingMessage.loadContentText");
tx.commit();
}
Objects.requireNonNull(msg, "Sending message not found: " + sendingMessage.getId());
if (msg.getContentTextFile() != null) {
byte[] bodyContent;
try {
bodyContent = fileStorage.loadFile(msg.getContentTextFile());
} catch (FileStorageException e) {
throw new RuntimeException(e);
}
//noinspection UnnecessaryLocalVariable
String res = bodyTextFromByteArray(bodyContent);
return res;
} else {
return msg.getContentText();
}
}
示例14: persistMessages
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected void persistMessages(List<SendingMessage> sendingMessageList, SendingStatus status) {
MessagePersistingContext context = new MessagePersistingContext();
try {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingMessage message : sendingMessageList) {
message.setStatus(status);
try {
persistSendingMessage(em, message, context);
} catch (FileStorageException e) {
throw new RuntimeException("Failed to store message " + message.getCaption(), e);
}
}
tx.commit();
}
context.finished();
} finally {
removeOrphanFiles(context);
}
}
示例15: computeSecurityState
import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public SecurityState computeSecurityState(Entity entity) {
Preconditions.checkNotNullArgument(entity, "entity is null");
SecurityState state;
String storeName = metadataTools.getStoreName(metadata.getClassNN(entity.getClass()));
Transaction tx = persistence.createTransaction(storeName);
try {
EntityManager em = persistence.getEntityManager(storeName);
Entity managedEntity = em.merge(entity);
support.setupAttributeAccess(managedEntity);
state = BaseEntityInternalAccess.getSecurityState(managedEntity);
// do not commit the transaction
} finally {
tx.end();
}
return state;
}