Thursday, March 09, 2006

EJB 3.0 Packaging - Entities

I am looking at the various EJB 3.0 packaging options and trying to determine best practice. My first recommendation is that you should create separate projects for your entities and package your entities in independent jar files. This is important for a number of reasons.

Firstly, you will want to share your entities in multiple projects, so having them maintained independently makes it easier to share them across projects.

Secondly, the teams that define the entities are probably going to work closely with the folks that are responsible for defining the database Entity Model. It will be easier if these teams can work independently from other teams.

Last but not least, it is not possible to define Entities in a portable manner. The main portability issues are with the way Surrogate Keys are generated using Sequence Generators. Having separate projects for your entities will allow you to create drop-in replacements for different database vendors. So you can have one set of entities for Oracle, another for Apache Derby, and so on. For an example of this, please have a look at the EJB3Demo project.

When you create your application archives, place the Entity jar files in the lib sub-directory within the archive. This will ensure that the entity definitions are available to all your EJBs and Web applications within the application. This article by Sahoo explains the benefits of putting your entity jar files in the lib directory.

1 comment:

Anonymous said...

Hi,

Maybe you can help me.

I have two EJB 3 projects. In the second, I want to use entity beans from the first. But when I do this

class Car {
.
.
.
@ManyToOne
public Engine getEngine() {
return engine;
}
.
.
.
}

(Engine is in other ejb3 file)
I receive the error message "unknow entity bean".

Do you know how to resolve this particular problem?
Thanks.

Luiz Carlos (lcmuniz@yahoo.com)