Spring Orm - Java

  • Benefits
    • Database management
    • Exception wrapping
    • Transaction management
    • Resource management
  • Spring Core with JPA
    • LocalEntityManagerFactoryBean
    • LocalContainerEntityManagerFactoryBean
    • EntityManagerFactory from JNDI
      • convertBeanToEntity => Used to convert business bean/dao into entity by BeanUtils API
  • Spring Core with JPA with Transaction Manager
    • Automating for tansaction Begin & Commit
    • Methods
      • Declarative
        • Using Annotations
          • PlatformTransactionManager
            • Service provider interface, has inplementations specific to backend
          • Transaction Metadata
            • Metadata about transaction, defiend by TransactionDefinition interface
          • Transactioanl Proxy
            • Object created by spring framework, wrapper over target object, uses PlatformTransactionManager
          • Types of Transactions
            • Global
              • Managed by app container, using Java Transaction API (JTA)
              • Made by using TransactionManager and injecting EntityManager
              • Requires extra container to perform the method
            • Local
              • Resource specific, JDBC connection is an example, extra connection is not required
              • Made by injecting EntityManagerFactory and creating EntityManager local
              • Correctness across multiple types of resources can't be assured
          • @Transactional annotation
            • It can be placed at class level, interface level or method level. Method or class level are more preferred
            • Spring encourages to keep the access specifier of the method as public where this annotation is used otherwise no error is raised but method will not exhibit configured transaction settings
            • This annotation defines the Transaction Metadata, to be given to Transaction Proxy
            • Transactions can be made read only using this annotation, throws exception of persist()
          • @PersistenceContext
            • Handle transactions using Spring Container EntityManager, instead of injecting EntityManagerFactory
            • Container managed EntityManager is created using @PersistenceContext
              • Transactional Scoped Persistence Context
                • Default for container managed EntityManager
                • Persistence Context is created after every method call
              • Extended scope Persistence Context
                • Default for non-container managed EntityManager
                • Used to share state of Entity across method calls
        • Using XML
      • Programatic (Imperative)
  • Transaction Propogation
    • Used for complex transactions
    • Rollback of unchecked exception is triggered by default, otherwise custom configured using rollbackFor="ExceptionName.class" attribute
    • Steps
      • Split the single physical transaction into smaller logical transactions
      • Physical Transaction can be started at the service layer using @Transactional
      • Logical transactions can be started at the database layer
      • Logical transactions can share the same transactional scope with the physical transaction so that, when one of logical transaction is rolled back then it rolls back the physical transaction there by all the other related logical transactions are also rolled back
      • Logical transactions can have the independent transactional scope, so that rollback of the one logical transaction should not hinder any other logical transaction / physical transaction
      • Types
        • Mandatory, Nested, Never, Not_Supported, Required, Requires_New, Supports
  • Spring JPA Data
    • Library that adds extra layer of abstraction on top of JPA provider, automates the DAO layer code
    • A proxy object is created at runtime and later same is used to perform CRUD operations
    • DAOWrapper will perform conversion from entity to bean and vice versa, acting as layer between dao and service
    • Data Repositories
      • Repository abstraction feature
      • Repostories
        • Repository
          • Helps in discovering the other interfaces which implements this interface
        • CrudRepository
          • Provides CRUD operations for Managed entity
        • PaginAndSortingRepository
          • Used to declare the entities that are used to sort and paginate the entities
        • JPARepository
          • It combines all the methods of these other interfaces for the underlying JPA provider
          • Methods => findOne, Iterable, count, delete, exists, etc
      • @RepositoryDefinition
        • Customizin standard repostitory
    • Writing Queries
      • Query method
        • Method signature itself is converted into a query
      • @Query("query")
        • Used to bind query directly to the methods which executes it
        • Can also externalize queries by name attribute and mentioning it in orm.xml
      • @Modifying
        • Used to execute DML queries
Share: