adam bien's blog

Nice, but hidden, CRUD wizard in Netbeans 6.0 📎

I explored a nice CRUD wizard, which is able to generate a Session Bean from existing JPA entities. I'm not sure, whether this functionality was already existent before Netbeans 6.0.  The wizard is available in the context of an EJB project. You can access the wizard from the menu "New File" (Strg+N), then navigate to the folder "persistence", and choose "Session Beans For Entity Classes". At least a single JPA entity should exist in your project...

The generated code is almost perfect. E.g. I generated a CRUD Session Bean from the following Entity "Bike":

@Entity
@Table(name = "BIKE")
public class Bike{
    @Id
    private String name;
    private String description;
    private int expectedkilometers;

//...some code omitted.

 The generated Session Bean looks like: 

@Stateless
public class BikeFacade implements BikeFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Bike bike) {
        em.persist(bike);
    }

    public void edit(Bike bike) {
        em.merge(bike);
    }

    public void remove(Bike bike) {
        em.remove(em.merge(bike));
    }

    public Bike find(Object id) {
        return em.find(com.abien.wizardsample.Bike.class, id);
    }

    public List<Bike> findAll() {
        return em.createQuery("select object(o) from Bike as o").getResultList();
    }
}

The coresponding Local interface is clean and simple as well:

@Local
public interface BikeFacadeLocal {

    void create(Bike bike);

    void edit(Bike bike);

    void remove(Bike bike);

    Bike find(Object id);

    List<Bike> findAll();

}

As mentioned, the code is o.k, but there is still room some for improvement:

  1. Inside the findAll method a simple query and not a NamedQuery was used. This hits not only the performance, but is harder to maintain. The declaration, or reuse of an query at the JPA entity would be much better (but much harder to implement :-)).
  2. The naming of the method "edit" is strange a - "update" or even "merge" would be better.
  3. The @Local annotation could be moved from the interface, to the Session Bean - then the interface would be clean and independent from the EJB 3 API.
  4. Actually a Unit Test could be generated as well :-).
I wondered in a Java EE 5 training, why a student can accomplish his exercise so fast - he used this wizard :-).