Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing transaction in Spring 3.0 in Spring + JPA + Hibernate
    primarykey
    data
    text
    <p>I currently using Spring + JPA + hibernate in a project. Then i decide my design pattern in model dan DAO implementation. I have a abstract for all my transaction purpose class. Every class( as a service) that perform a single update/create/delete task must inherits from this abstract class.</p> <pre><code>public class GlobalAbstractTransaction { public abstract void validation(DTO param); public abstract void process(DTO param); public void execute(DTO param){ validation(param); process(param); } } </code></pre> <p>Then i have another class (subclass of class above)</p> <pre><code>@Service @Transactional //using spring annotation for transactional support public class SomeTransaction extends GlobalAbstractTransaction implements ITransaction { //implements abstract method public void validation(DTO param){ } //implements abstract method public void process(DTO param){ } } </code></pre> <p>The BusinessTransaction is an interface that have one method </p> <pre><code>public interface ITransaction { public execute(DTO param); } </code></pre> <p>Noted that I put @Transactional (with default Propagation.REQUIRED) at class SomeTransaction want to make sure that this class is in a Transaction.</p> <p>When using in IntegrationTest</p> <pre><code>@Autowired //default by name ITransaction someTransaction; @Test public void testInsert(){ DTO someDTO = new DTO(); //apply value for DTO someTransaction.execute(someDTO); } </code></pre> <p>The result is there is no transaction occured. And the someTransaction.execute which only perform inserting data to database task, was not running well.</p> <p>And then I move the @Transactional annotation from SomeTransaction, to GlobalAbstractTransaction so the code would be like this.</p> <p>Modified <strong>GlobalAbstractTransaction</strong></p> <pre><code>public class GlobalAbstractTransaction { public abstract void validation(DTO param); public abstract void process(DTO param); @Transactional //added here public void execute(DTO param){ validation(param); process(param); } } </code></pre> <p>Modified <strong>SomeTransaction</strong></p> <pre><code>@Service //transactional annotation removed public class SomeTransaction extends GlobalAbstractTransaction implements ITransaction { //implements abstract method public void validation(DTO param){ } //implements abstract method public void process(DTO param){ } } </code></pre> <p>Then, the test work, data is inserted to database. </p> <ol> <li>Anybody could explain why this is happening. I can't add @Transactional at SomeTransaction.execute, because it already defined in GlobalAbstractTransaction. Can I add @Transactional add class level instead ?</li> <li><p>SomeTransaction is planned as Service, inside the service I autowired a DAO implementation using autowiring (I don't put any @Transactional at DAO level), that's what most people recommends. I just annotaed DAO implementation as @Repository, the service class as @Service. (note that in my service only perform one task, InsertingService, UpdatingService, DeletingService will be separated in three classes). I'm planning to add another layer for Service Facade ([http://java.dzone.com/articles/jpa-implementation-patterns-3][1]) see link for reference. So i create a class</p> <p>public class ServiceFacadeBean { public void executeTransaction(String name){</p> <p>} }</p></li> </ol> <p>I'm intended to using in the application like this.</p> <pre><code> @Autowired //injected using Spring autowiring ServiceFacadeBean serviceFacadeBean; serviceFacadeBean.execute("com.mycompany.SomeTransaction"); </code></pre> <p>In the implementation of the execute I want to use reflection using "com.mycompany.SomeTransaction" to create instance. but I always get <strong>NullPointerException</strong></p> <pre><code> Class clazz = Class.forName("com.mycompany.SomeTransaction"); clazz.newInstance() &lt;--- get null </code></pre> <p>And I'm intended that 1 service class, only perform 1 transaction task such as Inserting to database. What to best practices for putting @Transactional, or anyone have good links that really explaining well bout Transaction issue using Spring transaction</p> <p>Anyone have experience on this, or have any better design pattern solutions? Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload