- Spring jpa jars as dependecies in pom.xml
- Create an interface ParticipantRepository extends JPARepository
- Add
@Entity @Table @EntityListner - Advantages JPA spring data
- No need to write queries or biolerplate code.. like resultset, connection, transaction
- Pagination
Q1: Create common methods for repositories
1. @NoRepositoryBean
public interface BaseRepository <T, ID extends Serializable>
extends JpaRepository<T, ID>
2. public class BaseRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID>; implements BaseRepository<T, ID> {
private JpaEntityInformation entityInformation;
private EntityManager entityManger;
3.
@Overridepublic List
Query query = this.entityManger.createQuery("select e from " + this.entityInformation.getEntityName()
+ " e where e." + this.entityInformation.getIdAttribute().getName() + " in :ids");
query.setParameter("ids", Arrays.asList(ids));
return (List
}
@Configuration @EnableTransactionManagement @EnableJpaRepositories( basePackages = "${entityManager.packagesToScan:com.mycompany.entity}", repositoryBaseClass = BaseRepositoryImpl.class ) public class SpringConfiguration {
Q2: Auditable fields1. Add @EntityListeners(AuditingEntityListener.class)
2.@CreatedBy
private String createdBy;
@LastModifiedBy
private String lastModifiedBy;
@CreatedDate
private Date createdDate;
@LastModifiedDate
private Date lastModifiedDate;
Reference
https://www.youtube.com/watch?v=9Nopu8hRKq4&t=319s
Q3: JPA Key components
-
JPA Provider : The vendor product which contains JPA flavor (javax.persistence). For example Eclipselink, Toplink, Hibernate, etc.
-
Mapping file : The mapping file (ORM.xml) contains mapping configuration between the data in a POJO class and data in a relational database.
-
JPA Loader : The JPA loader works like cache memory, which can load the relational grid data. It works like a copy of database to interact with service classes for POJO data (Attributes of POJO class).
-
Object Grid : The Object grid is a temporary location which can store the copy of relational data, i.e. like a cache memory. All queries against the database is first effected on the data in the object grid. Only after it is committed, it effects the main database.
@Entity
@EntityListeners(AuditableListener.class)
@Table(name = "ADDRESS")
public class Address implements Auditable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ADDRESS_ID", insertable = false, updatable = false)
private Long addressId;
@NotNull
@Column(name="PARTICIPANT_ID")
private Long participantId;
Q4: Advantages of JPA
- 1. Database Independent: You can quickly switch your JPA implementation, as long as you’re not using any proprietary features.
- 2. Developer productivity: No need to write boiler plate code
- 3. ORM: Object Relational mapping.. you just deal with objects and it internally reflects
- 4. Writing queries with method names:
- 5. Avoid Unnecessary Queries:
The write-behind optimization is one of several performance optimizations you get with JPA. The basic idea is to delay all write operations as long as possible so that multiple update statements can be combined into one. Your JPA implementation, therefore, stores all entities that were used within one transaction in the first level cache.Due to this, the following code snippet requires only one SQLUPDATE
statement, even though the entity gets changed in different methods within the application. This reduces the number of SQL statements massively, especially in complex, modularized applications.
- 5. Caching:
https://www.sitepoint.com/5-reasons-to-use-jpa-hibernate/
No comments:
Post a Comment