Spring Framework (24 Part Series)
1 Spring Configuration
2 External Properties in Spring
… 20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types
To use JPA with Spring we need to implement the next 4 steps.
Define mapping metadata in entities
Use the next annotations:
- Entity: Mandatory. Marks the class as a JPA persistent class
- Table: Specifies the exact table name to use on the DB (would be the class name if unspecified)
- Id: Mandatory. Indicates the field to use as the primary key on the database
- Column: Name of the column on the table (would be the field name if unspecified)
- OneToMany: Identifies the field on the ‘one’ side of a one to many relationship
- JoinColumn: Identifies the column on the ‘many’ table containing the column to be used when joining. Usually a foreign key.
- Transient: Not stored in database
@Entity
@Table(name="T_ACCOUNT")
public class Account {
@Id
@Column(name="ID")
private Long entityId;
@Transient
private String number;
...
}
Enter fullscreen mode Exit fullscreen mode
Define an EntityManagerFactory bean
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(true);
adapter.setGenerateDdl(true); // creating/updating tables (be careful)
adapter.setDatabase(Database.HSQL);
Properties props = new Properties();
props.setProperty("hibernate.format_sql", "true");
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource());
emfb.setPackagesToScan("com");
emfb.setJpaProperties(props);
emfb.setJpaVendorAdapter(adapter);
return emfb;
}
Enter fullscreen mode Exit fullscreen mode
Define Transaction Manager and DataSource beans
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
@Bean
public DataSource dataSource(){
return
(new EmbeddedDatabaseBuilder())
.addScript("classpath:schema.sql")
.addScript("classpath:data.sql")
.build();
}
Enter fullscreen mode Exit fullscreen mode
Define Repository/DAO
@Repository("accountRepository")
public class JpaAccountRepository implements AccountRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public Account findByNumber(String number) {
String jpql = "SELECT a FROM Account a where a.number = :number";
Account account = entityManager.createQuery(jpql, Account.class)
.setParameter("number", number).getSingleResult();
return account;
}
}
Enter fullscreen mode Exit fullscreen mode
Spring Framework (24 Part Series)
1 Spring Configuration
2 External Properties in Spring
… 20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types
原文链接:JPA with Spring
暂无评论内容