Maven web project with JPA, Spring, Hibernate - spring

I am getting started with with JPA. I am not running the project in a JEE container, simply using Tomcat. I want to test my DAOs (even though I read somewhere that DAOs may not be relevant with ORMs today) and have minimal dependencies on Spring and Hibernate.
Gist for relevant codes and configuration.
Problems I am facing are
bean of type UserDao cannot be found and hence cannot be injected resulting in error, even though I can see that it is created as it's constructor runs and I have done syso there, BUT
EntityManager is Null when UserDao is initialized
If I change the test-config.xml to use persistence.xml which is configured for MySQL database, it creates all tables, but same errors. hsqldb-persistence.xml is configured to use in-memory db.
Project Structure as it appears in Eclipse. When I run the TestUserDao it read correctly everything from hsqldb-persistence.xml which is in /src/test/java/resources/WEB-INF/ directory.
Appreciate any help.
Thankyou.

Related

Jetty 8, Weld - Injection isn't working

I'm trying to setup a project using Jetty 8 as the server (jetty-maven-plugin:8.1.11.v20130520), Weld for CDI (weld-servlet:2.0.1.Final), Hibernate for JPA (hibernate-entitymanager:4.2.2.Final, hibernate-jpa-2.0-api:1.0.1.Final) and Jersey for JAX-RS.
It seems that injections isn't working at all.
I tested whether the EntityManager is being injected, and the value was null.
The log doesn't show any errors or warnings while executing the server.
Am I missing something ?
If needed, I will post the log file to gist.
Here is the gist of all relevant files: https://gist.github.com/StalkingKillah/e986474b6885d83c7f8d
This is how I got it working for Jetty 9, and it should be basically the same for Jetty 8.
http://www.eclipse.org/jetty/documentation/current/framework-weld.html
It is easier to have the weld jar outside of the webapp as then you don't have to mess with tweaking the classes exposed to the webapp classloader like you are here (with the -org.eclipse.jetty bit for the ServletContext). I suspect there are more classes that would have to be added to that in order to make it work. You should get a note in the logging that says it is all enabled and active though.

NoSuchBeanDefinitionException for annotated classess alone

Am getting org.springframework.beans.factory.NoSuchBeanDefinitionException
for the Classes for which I have used annotation (#Service)
I have mentioned the package scan correctly
This is a blunder mistake in deed. But just posting the answer because, many might do the same.
I was using Eclipse to prepare the JAR file. Eclipse did not pack the class files properly, so spring could not load the beans

spring persistence xml

What is the difference between:
persistenc.xml can be configured with hibernate properties OR Javax.persistence properties.
What scenarios would you use either one.
I tried using javax.persistence but my spring app would not work.. changed it to hibernate and it started working.
javax.persistence is the standard. Hibernate is an implementation. If you are completely sure that your DB layer will always be handled by Hibernate, go ahead and configure using Hibernate. If not, use javax.persistence, so that you could change your DB layer in the future.

Spring Hibernate Connection through AOP standalone application

I am trying to develop Annotation based Spring Hibernate standalone application to connect to DB. I've gone through the some blogs and wondered like we should not make use of hibernateTemplate becoz coupling your application tightly to the spring framework. For this reason, Spring recommends that HibernateTemplate no longer be used.Further more my requirement is changed to Spring Hibernate with AOP using Declarative Transaction management.I am new to AOP concepts. Can any one please give an example on Spring Hibernate Connection through AOP. That would be a great help to me.
Thanks in advance.
If you are looking for exemples of project structures, you may want to use maven archetypes which provide you an already working Spring + Hibernate or Spring + JPA configuration.
They may provide you also a web layer (or not) but you can remove it if you want.
To try that, install maven and type:
mvn archetype:generate
By the way, I don't think using HibernateTemplate is a big deal. Many people still use it. But you'd better inject the Hibernate session factory and use contextual sessions with getCurrentSession()
I'd use JPA instead of plain Hibernate. You can of course use Hibernate as a provider. I guess that you know how to run Spring container in standalone application. Just follow the steps from documentation here. Use LocalContainerEntityManagerFactoryBean. Then read about transaction management.
There is a new feature that lets you start JPA without persistence.xml file. Read here.
If you still want to use plain Hibernate follow the docs.

Eclipse RCP, Spring, Hibernate Class Loading Issue

I’m developing a RCP based app that uses a data access layer implemented using Spring Data JPA backed by hibernate. The annotated domain classes are in one jar/bundle, the Spring repositories and spring config to instantiate the datasource, entity manager, and transaction manager are in another bundle.These jars are used by other non-RCP apps across the project.I have all of the hibernate 3.6.8 jars and dependencies either wrapped and exposed through my own plugins or as OSGI bundles (jta, antlr, commons collections, etc) in the target platform. I’m using Eclipse Gemini Blueprint to manage the bundle spring contexts within the RCP app.
When the DAL bundle spring context is being initialized by the gemini extender, I get Hibernate related ClassDefNotFound exceptions, usually either on org.hibernate.ejb.HibernatePersistence or javax.persistence.spi.Persistenceprovider. I’ve tried putting all of the hibernate jars and dependencies in a single plugin that exports the javax and hibernate packages. I’ve tried setting Eclipse Buddy policies in the manifests of the plugins that I have control over, etc.
I haven’t been able to find any solution to this class loading issue between Spring and Hibernate under Eclipse RCP using Gemini. I’ve done past hibernate DAO projects within RCP by putting all of the hibernate jars in the same plugin as my domain and DAO classes, so only my classes are exported.
At this point I don’t think moving to a more OSGI friendly JPA provider like OpenJPA or EclipseLink is an option.
I found an answer to a slightly related question that suggested putting all of the spring jars and dependencies into a single plugin, with the hibernate jars and dependencies in another plugin with buddy policies set. This seems dirty when most of the jars involved are OSGi bundles.
I suppose I could create a plugin that holds the DAL jar, hibernate jars, and spring ORM jars, so they can all see each other.
Is there a clean way to get this to work?
The core bundle if you own it, add all the suspected bundles as registered buddies and make sure that you have defined packages or bundles are reuired otherwise sometimes buddy class loading is not going to work. If the loading is initiated from you bundle the eclipse buddy policy should cascade. Other option would be to modify the manfiest files in target bundles that are causing issues.

Resources