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.
Select the JPA project in the Navigator or Project Explorer and then click New > Other. The Select a Wizard dialog appears.
Select JPA > Entity and then click Next. The Entity Class page appears.
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.
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 NUMBER(10,0) ADDRESS_ID (primary key) VARCHAR2(80) PROVINCE VARCHAR2(80) COUNTRY VARCHAR2(20) P_CODE VARCHAR2(80) STREET VARCHAR2(80) CITY |
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.
Eclipse also displays the Address entity in the JPA Structure view.
After creating the entity, you must associate it with a database table.
Select the Address class in the Project Explorer view.
In the JPA Details 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. |
Tip: After associating the entity with the database table, you should update the Right-click the
|
Now we are ready to map each fields in the Address class to a column in the database table.
Select the id field in the JPA Details view.
Right-click id and then select Map As > id.
In the JPA Details view, select ADDRESS_ID in the Name field:
Eclipse adds the following annotations to the Address entity:
@Id @Column(name="ADDRESS_ID")
Map each of the following fields (as Basic mappings) to the appropriate database column:
Field | Map As | Database Column |
---|---|---|
|
Basic |
|
|
Basic |
|
|
Basic |
|
|
Basic |
|
|
Basic |
|
Dali automatically maps some fields to the correct database column (such as the city
field to the City
column) if the names are identical.