02 April 2008

NetBeans6.1+EJB3+Struts [PART 2]

Click Next and in packages you type entities and hit the Finish button.

This will generate the Java POJO's for you directly from the Database with all of the Annotations required.

Now navigate to Source Packages and open your Java Class. Take a look at how much code the IDE has already created. Also methods like hashCode, equals and even a @NameQueries annotation was added.

Now add @GeneratedValue(strategy = GenerationType.AUTO) to the top of your id field.

After adding the Annotation your code will look like this:

@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

This will tell the Entity Manager to take care of the ID sequence.

Right click again on MyApp-ejb, New->Other->Persistence->Session Beans for Entity Classes.
Click on Next
select the entity class available
click Add >
And click Finish.

This will create the the Session Bean interface and the implementation.
Two classes will open in your editor.
Click on the Facade one and check it out all of the methods that the IDE generated one more time for you. You have a CRUD just with few clicks. ;)

Creating all of those classes through the IDE diminishes the eventuality of error-prone code of the many, many Session Beans you might be creating by hand. And also removes the tedious/slow work of editing xml's and the necessity of redundant code writing. ;)

No comments: