As I am new to all this Java EE ocean, there is so much I want to know, and as I read more and more info on the internet I get more confused than anything gets clear. Are my presumptions right:
- Firstly we need
Entityclass (POJO) with@Entity,@Table,@Columnand etc. annotations. - Secondly we make
Serviceclass which will make physical changes in single database’s table usingSessionFactorywhich will be@Autowired. If I’m correct is this so called DAO? And do this class need to implement any other class? Because I saw numerous examples where connection between controller and entity ares like 3 classes long (one of which is interface). And there are some implementations in those classes. - The last thing we need is controller which will have
Serviceclass object which will also be@Autowired.
So to summarize: we have @Controller class which uses @Autowired service class object. Service class object consists of save/delete/select methods that are executed through @Autowired SessionFactory object?
If I’m right what configurations are needed for all this scheme to work? And if I’m wrong please explain how this must be done with as little configuration in XML files as possible.
Thank you.
There are several questions buried in here, so I will try and give you high level answers.
@Entity
This will define your objects, and more importantly (with other annotations you mention) it will allow Hibernate/JPA to successfully map and persist that data to the RDBMS of choice.
The other information you need to make this work (besides of course including the appropriate libraries) is the hibernate configuration file (hibernate.cfg.xml) which will be used to determine the database connection info, other hibernate settings, and classes to be scanned (assuming annotations are in use).
@Service
Is a spring stereotype that indicates that Spring should manage this class and that it should be considered a service (i would consider this an almost marker interface, I believe it is handled almost the same way as @Component). As far as implementing any interface or extending specific classes, I don’t believe so. I believe the big issue is getting hold of the entity manager. Let me pull a short snippet from a hobby project…
@Autowired
Autowired is an annotation Spring uses to indicate that the field annotated will be injected (by type is probably the most common, though certainly not the only way) by spring (as long as the class is managed by spring or marked as @Configurable).
To counter-summarize:
the only XML you’ll need is the hibernate config file. You don’t (necessarily have to even autowire the session factory as long as you have access to it.