Creating a Java persistent entity

This quick start shows how to create a new persistent Java entity. We will create an entity to associate with a database table. You will also need to add the ADDRESS table to your database.

  1. Right-click the project in the Package Explorer and select New > Class. The New Java Class page appears.

  2. On the Java Class page, enter a package name (such as quickstart.demo.model), class name (such as Address), and click Finish.

  3. Right-click the Address.java file in the Package Explorer and select Open.

    Open Address.java

    Opening a Java class.
  4. Select the Address entity in the JPA Structure view.

    Address Class in JPA Structure View

    Address class in JPA Structure view.
  5. In the JPA Details view, in the Map As field, select Entity. In the Table field, select the ADDRESS database table.

    Address Entity in JPA Details View

    JPA Details view.

Eclipse creates the persistent entity and adds the @Entity annotation to the class.

Address Entity

Java editor with the Address entity.

Eclipse also displays the Address entity in the JPA Structure view:

Address Entity

Address entity in the JPA Structure view.

After creating the entity, you must associate it with a database table.

  1. Select the Address class in the Explorer view.

  2. In the Persistence Properties view, notice that Dali has automatically associated the ADDRESS database table with the entity because they are named identically.


    Note:

    Depending on your database connection type, you may need to specify the Schema.

    JPA Details View for Address Entity

    Table selection for the Address entity in the JPA Details view.

After associating the entity with the database table, you must update the persistence.xml file to include this JPA entity.

Right-click the persistence.xml file in the Package Explorer and select JPA Tools > Synchronize Classes. Dali adds the following to the persistence.xml file:

<class>quickstart.demo.model.Address</class>

 

Now that you have created a persistent entity, you can continue with Mapping an entity to map the entity's fields to columns on the database table.