I've often done presentations where I show how NetBeans IDE can be used as a learning tool for the Java EE 6 Platform. The point I try to make in these presentations is that even if you're not using NetBeans IDE as your daily development environment, you can still use it as a Java EE 6 learning environment because it provides so many handy little tools, especially very focused code generators that are automatically aware of the code that has already been generated, and that let you add powerful annotated code snippets exactly when you need them.
Here's a YouTube movie (without sound) that shows the standard demo that I do with NetBeans IDE when introducing Java EE 6 in a presentation:
I also always make the point that you shouldn't rely on code generators. Instead, you should use them wisely and appropriately, otherwise you'll have a lot of code that you don't understand and that you won't be able to maintain.
However, the first question that I tend to get after such a presentation is about the JPA specification (one of the many specifications that are part of the Java EE 6 Platform) and the handy wizard that NetBeans IDE provides to generate entity classes that conform to this specification. The question is always as follows:
What happens if I change a generated JPA entity class and then later my database changes? Now I need to regenerate my entity classes... which will wipe out any customizations that I've added to them.
In the last few days I learned the answer to the above question. Here's what you see the first time you use the "Entity Classes from Databases" dialog:
However, the second (or any subsequent) time you use it on the same database connection, you see this, in the dialog that appears before the one above, i.e., the IDE knows that the tables from which you're going to generate entity classes have already been used within your project to generate entity classes:
Then, on the next step, after the one above, i.e., this is the dialog step you see when clicking Next above after selecting tables that have already been used within the project to generate entity classes:
Take note of the "Generation Type" column in the "Class Names" table above. The value is set to UPDATE automatically, in this case, since this is the second (or any subsequent) time that the tables will be used to generate entity classes. You can change that UPDATE by clicking in the column, as you can see below:
So, if you like, you can recreate the class. But, more likely, you want to update the class with new columns in your database. So you'll select UPDATE, i.e., that's the default, so no need to select anything. (And the "Create Persistence Unit" checkbox is gone, because the IDE knows you already have a persistence unit from the first time you went through this wizard.)
Depending on the class you're going to be generating, you could have one of a number of different values (New, Recreate, or Update) in the Generation Type column, as you can see below:
I checked up about the UPDATE value by asking Petr Jiricka and Sergey Petrov, the NetBeans engineers behind this functionality. Sergey tells me: "UPDATE is quite limited. Nothing should be deleted (and if a column is deleted from the database table, then that deletion will not be reflected), only new columns should be added and the type for the existing columns can be updated."
Then I tried it myself. I generated some entity classes, changed some of the generated methods, and those modifications were still there after the next run of the "Entity Classes from Database" wizard, because the UPDATE value ensured that nothing was deleted within the classes that were regenerated.
I think that's all pretty good news and makes the "Entity Classes from Database" wizard even more useful than I thought. Nothing is deleted when the UPDATE value is used, which is what the value is automatically set to on subsequent runs of the wizard.