Generating Entity Classes from a Database
See Also
In addition to writing entity classes from scratch,
you can also generate a set of persistent entity classes for an existing database.
You can use the New Entity Classes from Database wizard to generate the entity classes from
a connected database or from a database schema.
To generate entity classes from a database:
- Right-click the module project node in the Projects window and choose New > Other.
- In the New File wizard, select Entity Classes from Database from the Persistence category.
- Select the source database that contains the tables that you want to use to generate the entity classes:
- Data Source. Choose a data source from the dropdown list.
Alternately, you can choose Add Data Source to create a new data source.
The database must be running to choose this option.
- When choosing a data source, the server must be running and the data source must be registered with the server.
- If your target server is not a Java EE container,
the dropdown list contains the database connections registered with the IDE.
- Database Schema. Choose a database schema from the dropdown list.
This option is available only if there is a database schema in your project's src/conf folder.
After you select the source database, the tables in that database are listed in the Available Tables pane.
- Select any tables in the left pane and click the Add button.
Any tables related to the tables you select are automatically added to the list in the right pane.
The IDE will generate entity classes for each table listed in the right pane.
- Deselect Include Related Tables if you do not want entity classes created for related tables.
- Click Next.
- Confirm the name of the classes that will be generated for each table listed.
- Select the location where you want to save the entity classes.
- Select an existing package from the Package drop-down menu or type the name of a new package.
- Confirm that you want the IDE to generate named query annotations in the entity classes.
If you do not want the IDE to generate the annotations, deselect Generate Named Query Annotations for Persistent Fields.
- Click Next.
- (Optional) Specify any mapping options.
- Click Finish.
When you click Finish, the IDE creates entity classes for each of the tables you specified in the wizard.
The package containing the generated entity classes is selected in the Projects window.
Notes:
- When you select a data source or JDBC connection, the IDE also creates a database schema for the database and saves the schema in your src/conf folder.
- To persist entity classes, your project requires a persistence unit.
- See Also
- Working with Enterprise Applications
- About Java Persistence
- Creating an Entity Class
- Mapping Entity Classes
- Creating a Persistence Unit
- Connecting to a Database
- Obtaining a Database Schema
Legal Notices