Dali Java Persistence Tools User Guide Release 3.2 Release 3.2 |
|
Requirements and installation |
Concepts |
Home > User Guide > Getting started > Dali quick start
This section includes information to help you quickly start using Dali to create relational mappings between Java persistent entities and database tables.
Related reference
This quick start shows how to create a new JPA project.
Select File > New > Project. The Select a Wizard dialog appears.
Tip: You can also select the JPA perspective and then select File > New > JPA Project. |
Select JPA Project and then click Next. The New JPA Project page appears.
Enter a Project name (such as QuickStart
).
If needed, select the Target Runtime (such as Apache Tomcat
) and configuration, such as Default Configuration for Apache Tomcat and then click Next. The Java source page appears.
Note: The Target Runtime is not required for Java SE development. |
If you have existing Java source files, add them to your classpath and then click Next. The JPA Facet page appears.
On the JPA Facet dialog, select your vendor-specific JPA platform (or select Generic 2.0), JPA implementation library (such as EclipseLink), database connection (or create a new connection), define how Dali should manage persistent classes, and then click Finish.
Tip: Select Override the Default Schema for Connection if you require a schema other than the one that Dali derives from the connection information, which may be incorrect in some cases. Using this option, you can select a development time schema for defaults and validation. |
Eclipse adds the project to the workbench and opens the JPA perspective.
Now that you have created a project with persistence, you can continue with 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.
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.