JPA | Hibernate | Spring boot and Spring data JPA

Its all about connecting to database and persists the data in to database

1. JDBC way

Write SQL statement, manually , iterate over resultSet , cumbersome



2. Spring JDBC, just provides templates but still write queries , mapping of data in to query little easy, using row mapper etc



Provider easy query result mapper




MyBatis:
Its not really ORM, it actually maps query to object, done like

Here myBatis will pull "user,desc," etc from todo argument.

------> Above all approaches are based on writing queries <--------------

Problem : Query changing and maintaining was problem. --> Hence JPA

----------------------------------------JPA ---------------------------------------------------------------------------

Map classes to table, using EntityManager
JPAQL --> complex search,criteria API
Entiry = Table




                                                    




                                                                   JPA Vs Hibernate



Interface between Entity and Database 

JPA/Hibernate = EntityManager.save(Entity e);
Spring = org.springframework.stereotype.Repository


PersistenceContext and Transnational 

If EntityManager is an interface in "PersistenceContext", so if you make any changes to "user" after .persist, it will not be updated.
So you have to declare the @Annotation at when you declare the EntityManager instance and attach PersistanceCntext with that instace
s all activities.

Similarly , to make an transaction. you need to put .persist in "trasaction.begin and transaction.end", rather you can declare the same at class level annotation, so all methods in that class will be treated as transnational.


Launch the application

1. Either create web service and let the webservice wait like a thread , till its actually invoked.
2. Or start it as main method
3. Or user "spring.boot.CommandRunner"
   All the classes which implements "CommandRunner" Interface, it's run method is called, when     application(Java application from eclipse) is launched.



public interface AlarmRepository extends CrudRepository<Alarm, Integer> {

}

This basically replaced "DAOService" class, which you had to create as an interface between 
your classes and database and you would use "EntityManager.insert(Alarm).


Now "AlarmRepository", we told ,we will send "Alarm" object to persist and primaryKey is "Integer"
type.


Now we have to just do 

alarmRepository.save(alarm);



 














References : @in28minutes












Comments

Popular posts from this blog

Bean loading StackTrace