Debugging Java with Spring,JPA API and Apache Derby DB

This post highlights the usage of Spring with JPA API and basic configuration aspects for database interactions with Apache Derby database. The CHECK segments list resolution to some of the obstacles which I faced while trying to achieve the solution

Once the object model is ready with you, you can create JPA Entities from tables.

CHECK: Make sure the @Table annotation contains correct schema and table name against all Java entity beans. Otherwise the API won't hit the correct database, without even giving any error in the console, confusing the user even more.

J.1.1 Autowiring components and persistence unit

J.1.2 JPA Data repository for DB interaction

We are using JPA Data repositories (one for each entity in the current design) for performing CRUD operations. JPA repositories are traditionally interfaces which supply CRUD methods: save(Entity T), findOne(Long Id) etc.

The implementation (Service Implementation, which can also be exposed as a REST service) should contain an autowired instance of repository and the EntityManager(instance of EntityManagerFactory)

CHECK: The call to the repository save method supplied by the JPA Data repository performs the actual merge. And there is no explicit need to call the commit method from the entity manager.

J.1.3 Transactional Service

If you want the CRUD operations to be transaction managed, then you'll have to incorporate TransactionManager (Section J.1.1) to wrap around the Entity Manager factory. Also, make appropriate use of @Transactional annotation as below:

No comments:

Post a Comment