I am going to develop which will be web application as well as mobile (android / iOS / windows) application. The database in this application will be managed by Hibernate. Also as it is cross platform application, web service will also be used. What I know so far is:

HIBERNATE:

  • POJO Files (the getter-setter ones which will create database tables)
  • Model (the java class which will interact with database)
  • Controller (basically servlet which will get data from view [jsp], set it in POJO object and pass this object to Model for any of CRUD operation)
  • View (the jsp pages)

REST WEB SERVICE:

  • Web service implementation class, which have web methods, which can be called by URL from client, and it can return JSON or XML format data.

So now my question is:

  • How to integrate these both? Should I put my all POJOs and Model files to web service? If no, than what to do in this situation? If yes, than how to do that (simple example)?
  • What are you using for your web layer? – JimmyJames Jul 20 '16 at 14:32
  • hibernate, servlet and jsp. I want to use REST WS as well but don't know how to do that along with hibernate. Is it possible to write hibernate code (POJOs and Model) in WS? – Jignesh M. Khatri Jul 20 '16 at 15:13
  • try Apache cxf with hibernate? – Richard May 16 '17 at 23:54

You should take a look at the JAX-RS spec and do a tutorial. I like running Jersey but there are many implementations. Do not reinvent the wheel and try to build a RESTful interface using hand-rolled servlets. This will make building your REST interface a snap. Integrating to Hibernate is just writing the code to do your queries. The JAX-RS spec allows you to register MessageBodyReaders and MessageBody writers which do the translation between your objects and any serialized formats you wish to support. There are various tools for translating to and from JSON. GSON is a solid choice but there are others.

  • Okay. I got your idea. But now I am totally lost. Here is exactly what I want to do - I want to develop web app, using hibernate and REST WS, as well as android app for the same. So which code should I put in my WS, so that both (web app as well as android app) can use it? – Jignesh M. Khatri Jul 21 '16 at 2:39
  • What to do if you do not have access to source code (i.e. if you do not have access to class file) when converting from JSON to Java Object, with the help of GSON? I mean in which variable to store the object which we get from JSON? – Jignesh M. Khatri Jul 21 '16 at 3:57
  • I made my own way. Made a jar file containing all the hibernate POJOs and used this jar in both - web app as well as android app, and the communication of android app with web app is through web-service and GSON is also used as you suggested. Thanks! – Jignesh M. Khatri Jul 22 '16 at 3:58

I would advice you to use the Spring MVC framework along with Hibernate

  1. The POJO classes you are mentioning are annotated with the JPA/Hibernate Annotations which creates the Database Tables.

  2. Next comes the Dao Layer which contains the methods to perform the CRUD operations on the Database Tables.

  3. Next comes the Service Layer wherein you can handle the business logic such as dependency injections between multiple entities.

  4. Next Comes the controller layer wherein you can annotate the methods with the @RequestMapping URL which eventually becomes your REST Endpoint.

This way you have a tight binding with your Model-View-Controller.

Here's an Example

With that being said, you can alternatively use only Spring Controllers as only REST Endpoints rather than returning the ModelAndView part. The Response returned by the Spring REST Endpoint (JSON) can be consumed by AngularJs Controller to populate the View.

You can use BootStrap or Semantic UI to give a responsive UI since you mentioned that your application is to be used on Mobiles !

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.