I have to build an web application that integrate Spring and Hibernate in Play Framework 2.1. I integrated Hibernate successfully, but how can I do it with Spring?
It's very easy now in Play 2.1 with the getControllerInstance interceptor. Here is a sample application which has Play 2.1, Spring (with Java Config), and JPA:
https://github.com/jamesward/play2bars/tree/java-spring
I recommend the answer from James Ward. Here is some information for upgrading from a Spring Java 2.0 play app to 2.1.
In 2.0.4 I've used the Spring4Play2 dependency, see https://github.com/scott-phillips/Spring4Play2/blob/master/README.markdown but I could not get this to work when upgrading to play 2.1.1 even though there is a 2.1-SNAPSHOT jar of this module at https://maven.library.tamu.edu/content/repositories/snapshots/play/spring_2.9.1/2.1-SNAPSHOT/
The error I got was "Plugin [play.api.db.BoneCPPlugin] cannot been instantiated." and "java.lang.IncompatibleClassChangeError: Found interface play.api.Application, but class was expected" and googling did not bring any answer.
So I went ahead and looked at the nice example from James Ward, dropped that plugin, and just added
"org.springframework" % "spring-context" % "3.2.1.RELEASE",
to my Build.scala as you can see here https://github.com/jamesward/play2bars/blob/java-spring/project/Build.scala
then created the Global.java class similar to the example, and that was about it.
I've added this answer for people running into the same situation so that googling for the error message could bring up a solution.
I integrated spring (with Hibernate) within Play 2, you can see an example (full working source) and explanation here:
http://serverbabyblog.wordpress.com/2014/03/26/activiti-on-play-2-with-spring-and-hibernate-jpa-example/
Just skip all stuff, relative to "Activiti" framework.
Basically, I was forced to throw out jpa and jdbc plugins of Play!.
Replacing their functionality by known open-source 3-d parties.
It's OK.
Play JPA plugin is too narrow-scoped for my case.
As result transaction managed by Spring transaction manager and entire example works with spring JPA annotations(using Hibernate JPA). It is really cross-platform example, because you can take all the spring stuff and move from play to other framework.
Good luck!
Related
At my company, we're currently rebuilding our core application, and we're considering going with Spring Boot as opposed to just Java-Web, JAX-RS, and Weblogic.
We also built a database lib using hibernate which we use on a lot of our services, but we want to rebuild that too using Spring boot.
Now, what I desperately need is some sort of reverse engineering tool to help with this spring boot + hibernate migration. I know a question like this has been asked: questions on spring boot + hibernate and the answer given was, use a generic DAO.
But I'm working with more than a hundred tables!
Perhaps I'm looking at this the wrong way. Maybe I don't need reverse engineering to handle this massive work. In which case, I need some advice.
Thanks guys!
I already had the same experience. you can managed to generated a reverse engineered annotation based version of your database using hibernate-tools.
to use hibernate-tools you need to find the stable version of the hibernate plugin at http://www.jboss.org/tools/download. find more instruction at:
How to install Hibernate Tools in Eclipse?
Hibernate Tools in Spring Tool Suite 3.6.1
for further information and instruction on how to get to your aim, I propose you to have look at the following links:
http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
How to generate domain objects with annotations using hibernate tools
https://www.youtube.com/watch?v=IU7cwWMDdjU
Notice that once you manage to do a reverse engineering against your DB, hibernate-tools does not offer a configuration with spring boot and you need to provide it in you project.
Please advise me a project where I can see how to use Spring MVC and Hibernate together in the right way. I'm new to these frameworks.
Appfuse has a Maven archetype for this stack.
Strongly suggest you have a peak.
Alternatively the book Spring in Action was a great resource for me in learning spring and hibernate/spring interaction - use Annotation driven transaction management as well and you have a really great start.
see here: http://appfuse.org/display/APF/Using+Spring+MVC
appfuse-basic-spring is the basic project archetype.
Using maven modules is a better practice. ie)
appfuse-modular-spring
If you install m2e (maven 2 eclipse plugin) creating a new maven project will allow you to select the archetype.
better answer:
If I was going to recommend HOW to learn these technologies, I would say learn how to build a hibernate/maven/xml project first by following this:
http://www.mkyong.com/hibernate/quick-start-maven-hibernate-mysql-example/
Then learn how to use spring for dependency injection with maven/hibernate and xml:
http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example/
Take special note of layering of the bo/dao pattern and how you use interfaces but wire in implementations with spring di - this is crucial for writing quality code
Next check hibernate annotations and annotation driven transactions:
http://www.springbyexample.org/examples/hibernate-transaction-annotation-config.html
http://www.springbyexample.org/examples/hibernate-transaction-annotation-config-code-example.html
http://www.mkyong.com/hibernate/maven-hibernate-annonation-mysql-example/
Finally, you can learn your mvc framework of choice. I actually recommend struts2 but it won't matter much. use spring DI by implementing interfaces and wiring implementations at runtime for you actions/controller. If you don't understand this how, you will after the hibernate/spring tutorials. Spring in action will be a good reference to keep on your desk but will take a few weeks to digest as you learn. Good luck! You can do it - I did and am now working as a java dev!
the petclinic app includes these, and many more things
spring-roo can create a skeleton project using these technologies for you.
If you are new to Spring MVC and Hibernate, I advice you to follow the Spring MVC step by step guide. The only problem with this is the version, the guide uses version 2.5 and the current release is 3.1.x, but it's a very good starting point to accumulate knowledge.
Spring MVC quickstart archetype provide simple spring-mvc and hibernate configuration.
I have developed a Spring/JPA application: the service, repository and domain layers are nearing completion.
The only layer that is missing is the web layer. I am considering using Playframework 2.0 for the web layer but I am not sure whether I can inject/use spring beans in my Playframework 2.0 classes.
Is this possible? If so how?
You can. Updated for Play 2.5.x:
https://github.com/remithieblin/play-spring-loader
Alex.p is right there is support for spring in PlayFramework
Have a look here :
http://typesafe.com/blog/announcing-play-framework-21-the-high-velocit
and on example here :
https://github.com/guillaumebort/play20-spring-demo
Cheers
I just tested it on my project it works but on playframework 2.1 in 2.0 it didn't work.
Here is a another way of injecting beans using Spring. Standard annotations are used. No plugin is required.
https://github.com/huntc/play-spring
There's already support for the Spring IoC container in Play. It's just a matter of editing the application.conf file as mentioned in the linked to article.
Ignore the above see comment: it seems that this was dropped in playframework 2.0.
I have started fooling around with Lift and I wanted to use it on a project that interacts with my legacy database. I have already went through the trouble of getting a Hibernate configuration to work with this database.
So I was wondering if it is possible to build a Lift project that uses the Hibernate code that I have already written. Can anyone show me any good examples or tutorials of that?
You can use any ORM with lift, including Hibernate. There is a full example using JPA and hibernate here
If you have some more questions about that example, you may want to try the Lift mailing list , where the developer who wrote can be reached.
What if you don't want to start a separate project for grails but instead sneak it into an existing webapp?
I have to build an admin interface/crud for some new entities and thought it would be a perfect way to learn grails.
I'm trying to make one application with a Grails app and a Spring app.
I've tried to sneak the Grails App into the Spring one, but this is "impossible". It's easier to sneak the Spring app into the Grails app. Grails knows what Spring is, but Spring has no idea of what Grails is.
In this article you can find useful information about how to use your hibernate mapping files or annotations in Grails, so you don't have to remap everything. Also you can use all your java clases (put them into src/java). You can put the beans defined in the ApplicationContext.xml in conf/spring/resources.xml. You can leave them in ApplicationContext, but I've had some problems.
I don't have ended the job (almost) and it looks good.
It would be hard to "sneak it in" unless the existing app has the correct dir structure that maps exactly to how grails likes it - after all, convention over config is where the power of grails comes from.
You can try doing the admin interface as a "seperate" app to the original/existing spring app, and map the existing database to the grails domain objects. though i m not sure how you would run them side by side easily without more information on the existing app. It is possible definitely though.
I agree that building your admin interface is a good exercise to learn Grails, and also agree with the previous answer that Grails is difficult if not impossible to integrate with an existing Spring application. You could probably get it done, but the headache would not be worth it.
Grails is built on top of Hibernate for its ORM, so if you're already using Hibernate with this Spring app you can work this to your advantage. It's not too difficult to configure a Grails app to use pre-existing Hibernate models, and this is explained well in Grails documentation.
So, I'd recommend building up your admin console as an independent Grails app but make use of the Hibernate models you already have, if in fact you've used Hibernate.