Java tutorial
/* * Copyright 2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vladmihalcea; import com.vladmihalcea.hibernate.model.store.Company; import com.vladmihalcea.hibernate.model.store.Image; import com.vladmihalcea.hibernate.model.store.Product; import com.vladmihalcea.hibernate.model.store.WarehouseProductInfo; import com.vladmihalcea.service.StoreService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring/applicationContext-local-tx-test.xml") @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class HibernateJPATransactionIsolationIntegrationTest { private static final Logger LOG = LoggerFactory .getLogger(HibernateJPATransactionIsolationIntegrationTest.class); @PersistenceContext(unitName = "persistenceUnit") private EntityManager entityManager; @Autowired private TransactionTemplate transactionTemplate; @Autowired private StoreService storeService; @Before public void beforeTest() { CleanDbUtil.cleanStore(transactionTemplate, entityManager); } @Test public void test() { transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { Company company = new Company(); company.setName("TV Company"); entityManager.persist(company); Product product1 = new Product("tvCode"); product1.setName("TV"); product1.setCompany(company); Image frontImage1 = new Image(); frontImage1.setName("front image 1"); frontImage1.setIndex(0); Image sideImage1 = new Image(); sideImage1.setName("side image 1"); sideImage1.setIndex(1); product1.addImage(frontImage1); product1.addImage(sideImage1); WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo(); warehouseProductInfo1.setQuantity(101); product1.addWarehouse(warehouseProductInfo1); entityManager.persist(product1); Product product = entityManager.find(Product.class, 1L); product.setQuantity(10); return null; } }); storeService.purchase(1L); } }