jn025
jn025

Reputation: 2895

Managing hibernate entities in EJB project

I made an 'Enterprise application' in NetBeans and I added hibernate as well as some hibernate mapping files to the web application project. Additionally, I need to add a 'HibernateUtil.java', 'hibernate.cfg.xml' and 'hibernate.reveng.xml' in the web application.

However I wanted to manage these entity/mapping classes in my EJB project as they seem to behave like enterprise beans and I wanted to separate them from my web application. I was wondering how this can be done and how I could access the entities ejb's in my web application project through dependency injection? There was also an option in the 'New' menu: 'Create session bean for Entity classes' and wondered if it has any use in relation to the question?

EDIT: Using EJB3

Upvotes: 0

Views: 354

Answers (1)

Roman Nazarenko
Roman Nazarenko

Reputation: 608

You shouldn't have put your JPA-related files to a WAR at the first place. Keep them in EJB JAR and reference your JAR from WAR as a provided runtime dependency in Maven (or use provided scope's analogue for whichever build tool you prefer). Keeping your business logic in EJB JAR will pay off when you'll need more than one WAR in your EAR (different security realms, etc.)

In general, this is as simple as putting all your JPA-related stuff in EJB JAR, they will then be available for dependency injection from WAR-provided managed beans as well.

Also, consider replacing your Hibernate-specific configurations with JPA and let application server to resolve all the stuff for you.

Upvotes: 1

Related Questions