Netbeans really hit a home run when it automated the creation of session beans from entity classes. It also does a good job creating entity beans. It’s a win-win (God! I can’t believe I just wrote the phrase win-win). It makes good use of the facade design pattern and gives you a service that you can inject into your controller.
Generated Entity Beans
First, let’s talk about what you get when you let Netbeans generate your entity beans. You might say, “Well Mr. RAJP, I’m perfectly capable of creating my own damn entity beans.” To which I say “Fiddlesticks. You’re wasting your employer’s valuable time!”
The entity bean that Netbeans generates gives you everything you need (and perhaps more) just by selecting a table in a list. Netbeans generated entity bean come with the following features:
- Declares each field in the table with an estimation of the proper type and annotates maximum sizes, default values, whether it can take a null value, and whether it is the primary key.
- It maps all of the database relationships with @OneToMany, @ManyToOne, and @ManyToMany depending on how well you designed your database.
- It creates a nested array of @NamesQueries searching on each field.
- It generates all the getters and setters
- It overrides hashCode() , toString(), and equals()
The only modifications I ever have to make is to add a sequence generator to the primary key and add a few more named queries.
“But David! This is too easy. Shouldn’t I have to work harder to get my job done?”
“No. This gives you more time to build your damn system. Now swallow your pride and code!”
Generated Session Beans for Entity Beans
This is where things get really cool. For years I’ve been coding a service class, and doing it poorly. Being a lowly RAJP (Regular Average Java Programmer), I was building a static service of operations to perform on the entity beans. Basic CRUD plus a few custom business functions. Did it work? Yes. Should I have created some sort of service that could be injected into my controller? Yes. Am I too lazy to do anything that cool? Definitely.
When I upgraded to Netbeans 7.1, and was generating some entity beans, I happened to notice an option to generate “Session Beans for Entity Classes…” in the New menu. Honestly, I had no idea what this would do. When I did it though, which was as simple as selecting from a list of entity beans, it gave me something totally cool. It gave me a set of stateless EJB classes replete with EntityManagers already hooked up to my persistence unit. But wait! There’s more! It gave me a nicely designed AbstractFacade for the EJBs to extend with all the CRUD methods I would need. It’s so nicely designed, in fact, that it can be extended by any session bean you generated from an entity. It’s all generical and stuff.
Netbeans got it right! This is a far better a design than what I could have produced myself. This is a design worthy of a JE (Java Expert).
The only slightly weird thing is that the session bean never uses the named queries in the entity bean. It handles all of it’s operations with JPAs CriteriaQuery class. But that’s just fine with me. When I need it, I just add another operation to the session bean to call named queries.
Now I have a set EJB facades that I can inject into my controllers. I use Stripes ActionBean for my controllers, by the way. It’s a kick ass stack. JSP, Stripes, EJBs, JPA, Glassfish, and Netbeans.
Netbeans team, you NAILED this down TIGHT! Yessir!
Thanks for reading! If you would like to see what else I’m up to, check out Whiff