Creating a Java persistent entity with persistent fields

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. Select the JPA project in the Navigator or Project Explorer and then click New > Other. The Select a Wizard dialog appears.

  2. Select JPA > Entity and then click Next. The Entity Class page appears.

  3. Enter the package name (such as quickstart.demo.model), the class name (such as Address) and then click Next. The Entity Properties page appears, which enables you to define the persistence fields, which you will map to the columns of a database table.

  4. Use the Entity Fields dialog (invoked by clicking Add) to add persistence fields to the Address class:

    private Long id;
    private String city;
    private String country;
    private String stateOrProvince;
    private String postalCode;
    private String street;
    

    Note:

    You will also need to add the following columns to the ADDRESS database table:

    NUMBER(10,0) ADDRESS_ID (primary key)
    VARCHAR2(80) PROVINCE
    VARCHAR2(80) COUNTRY
    VARCHAR2(20) P_CODE
    VARCHAR2(80) STREET
    VARCHAR2(80) CITY
    

  5. Click Finish. With the Create JPA Entity wizard completed, Eclipse displays the Address entity in the JPA Structure view.

Address.java includes the @Entity annotation, the persistence fields, as well as getter and setter methods for each of the fields.

Address Entity in Address.java

Java editor with the Address entity.

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

Address Entity in the JPA Structure View

Address.java in the JPA Structure View.

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

  1. Select the Address class in the Project Explorer view.

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

    JPA Details View for Address Entity

    Address.java in the JPA Details view.

    Note:

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



Tip:

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

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

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


Now we are ready to map each fields in the Address class to a column in the database table.

  1. Select the id field in the JPA Details view.

  2. Right-click id and then select Map As > id.

  3. In the JPA Details view, select ADDRESS_ID in the Name field:

    JPA Details View for the addressId Field

    The JPA Details view for the Address entity’s id attribute.

    Eclipse adds the following annotations to the Address entity:

    @Id
    @Column(name="ADDRESS_ID")
    
  4. Map each of the following fields (as Basic mappings) to the appropriate database column:

    Field Map As Database Column

    city

    Basic

    CITY

    country

    Basic

    COUNTRY

    postalCode

    Basic

    P_CODE

    provinceOrState

    Basic

    PROVINCE

    street

    Basic

    STREET


Dali automatically maps some fields to the correct database column (such as the city field to the City column) if the names are identical.