Using JPA ( Transactions , Inheritance .. )

JPA and Interitance

  • By default, the EclipseLink persistence provider assumes that all the classes in a hierarchy are mapped to a single table
  • The default Inheritance Type = InheritanceType.SINGLE_TABLE
  • The Default Column named used to differentiated our Objects is named DTYPE

Overview Inheritance Type: SINGLE_TABLE

SINGLE_TABLE – all the classes in a hierarchy are mapped to a single table. The table has a discriminator column (@DiscriminatorColumn) whose value (@DiscriminatorValue) identifies the specific subclass to which the instance that is represented by the row belongs.

Note: This option provides the best support for both polymorphic relationships between entities and queries that range over the class hierarchy. The disadvantages of this option include the need to make nullable columns that should be NOT NULL.

JPA and Bulk operations

JPA and Transactions

Using EJB and Transactions

  • EJB provides container managed transactions
  • An EJB (or to be more precise: container) managed transaction is not a database transaction – it can be more
  • Each business method starts/joins an active transaction
  • Container Managed Transaction starts on method Entry and commits on method Exit by using Interceptors

EJB Transaction Sample

@Stateless
public class MyFirstEjbBean implements MyFirstEjb {
   public void helloWorld(){ // transaction starts here
    
    System.out.println("Hello world!");
  } // transaction ends here
}

Reference

Using JAVA EE 7 @Transactions-Interceptor with CDI Beans

  • The javax.transaction.Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.

Sample for JAVA EE 7 method level annotation

import javax.transaction.Transactional;
..
 @Transactional
    public void removeAccount() {
        Account currentAccount = securityService.getCurrentAccount();
        accountEAO.remove(currentAccount);
    }