1

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:

  1. Firstly we need Entity class (POJO) with @Entity, @Table, @Column and etc. annotations.
  2. Secondly we make Service class which will make physical changes in single database's table using SessionFactory which 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.
  3. The last thing we need is controller which will have Service class 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.

Ilona
  • 357
  • 3
  • 10
Minutis
  • 1,193
  • 1
  • 17
  • 47

2 Answers2

2

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...

@Service
public class UserServiceImpl implements UserService {


private SessionFactory sessionFactory;

public UserServiceImpl() {
    sessionFactory = new Configuration().configure().buildSessionFactory();

}


@Override
public Collection<Person> getAllUsers() {
    Collection<Person> peoples = new ArrayList<Person>();
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery("from Person").setMaxResults(500);
    peoples.addAll(query.list());
    session.close();
    return peoples;
}
}

@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.

jholder
  • 493
  • 1
  • 4
  • 11
  • Thanks for respond, I finally got time to watch over it. And I have few questions. Why in your example you need to implement other class? I thought implementations are only required if you have few similar classes which can be summarized in implemented class. For example pizza,pasta,soup(these classes implement food) is food(interface). Another question: is it enough to have three classes to use hibernate, one is `@Entity`, another is `@Service` and last one is `@Controller`? – Minutis Nov 21 '11 at 13:06
  • 1
    Sorry for the delay. The only reason I implemented that interfaces (in this example) was that I was cutting and pasting from another example. A thorough discussion of interfaces won't fit in the comments, but there is a great discussion at http://stackoverflow.com/questions/2659366/java-interfaces-methodology-should-every-class-implement-an-interface . As far as the number of classes....i think *technically*, if you have the @Entity, and the configuration files in place, you could write a unit test to make the query. – jholder Dec 08 '11 at 00:52
1

You are on the right track. Here's a good walkthrough of Spring and Hibernate.

You could annotate your Service with @Service instead of @Autowired, but both work. You'll need to add a section to your application-context.xml to tell Spring which packages to look in for annotations.

You are right that a Service is a DAO, well, that's the common usage but you can write a Service that does more than offer CRUD and other getters for one entity if you wish. An entity represents one database table, a Controller handles one (or more) URLs and the Services are the classes in between those two that make getting and setting data from the database easier by offering CRUD and other methods that are called by multiple controllers.

If you run through the example in the link then it will give you a good idea about how the different parts of Spring work.

Steve Claridge
  • 10,650
  • 8
  • 33
  • 35
  • Hi, nice example. Is it possible to connect hibernate-context.xml and hibernate.cfg.xml in one file (would be nice that configs in hibernate.cfg.xml file could be added into hibernate-context.xml and hibernate.cfg.xml will be deleted). – Minutis Nov 21 '11 at 14:13