• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are we not hardcoding here with annotation

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm slightly confused with the whole migration to annotation based configuration. Take the following article as an example. The author has actually hard coded the database credentials in the code!
http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/

Are we serious? How do we enable the users to set their own password?

Also annotation based config is offered by Hibernate. So what does Spring Data really offer us here?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Don't look at online samples as production code. That online sample is meant to show you how to use Spring Data. It's not something you can just copy paste into your code and have it work without any other work. You can load many of those hardcoded elements through an external properties file Look at this page for how you can load properties in an XML less configuration

2) Hibernate, Spring JPA and Spring data provide differrent functionality. All of them uses annotations, but that doesn;t mean all of them do the same thing. Each of them brings it's own value.

Hibernate provides an ORM layer. It basically provides a library that among other things, primarily allows you to map database entities to Java classes. Hibernate doesn;t provide declarative transaction and session management. Usually, if you are going straight to Hibernate, you will be implementing a DAO class in which you will make the appropriate class to create a Hibernate session, start/stop transactions and call hibernate to do CRUD operations on your data. Hibernate doesn't eliminate the need to write a DAO layer. Your DAO layer becomes smaller (and database independent) compared to straight JDBC.

JPA and Spring's support for JPA provides declarative transaction management, and autowiring of the JPA entity manager. You are gain not eliminating the DAO layer completely. You are reducing the amount of boiler plate code in the DAO layer as compared to a DAO implemented with Hibernate. You will declare your EntityManager in your configuration and Spring IoC will do the job of "giving" the EntityManager to your DAO. ALso, you can put Transactional annotations on your DAO/service methods, and Spring will do the work of "weaving" session/transaction management code into your code. This allows your DAO to focus on your entities instead of being filled with boiler plate code. All your DAO has is the calls to EM to do CRUD operations with your entities.

Spring Data, OTH, almost eliminates the need to implement a DAO layer. ALl you do is write your entity classes, write JQL and declare some interfaces. For most part, you don't need to write a line of DAO code. As an application developer, you can focus on doing the database design and focus on the service layer that contains the business logic
 
Mike Cheung
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks.
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic