So far we have seen recipes where the JPA entity identifier is generated by Spring Roo. By default, entities created by Roo specify the identifier generation strategy as GENERATIONTYPE.AUTO
, which means that the persistence provider will choose an appropriate strategy for the database. You may want to customize this identifier generation strategy based on your application's requirements.
In this recipe, we'll look at how we can modify a Roo-generated identifier definition to use a database table for generating identifier values.
Exit the Roo shell and delete the contents of the C:
oo-cookbookch03
-recipes directory.
Start the Roo shell from the C:
oo-cookbookch03
-recipes directory.
Execute the ch03_jpa_setup.roo
script which creates the flight-app
Roo project, sets up Hibernate as a persistence provider, and configures MySQL as the database for the application. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.
Now, create a new Flight
entity with a Long
type identifier field, as shown here:
roo> entity --class ~.domain.Flight --identifierColumn FLIGHT_ID --identifierField flightId --identifierType java.lang.Long --table FLIGHT_TBL
The following code from the Flight_Roo_Entity.aj
file shows the identifier definition generated by Spring Roo for the Flight
entity:
privileged aspect Flight_Roo_Entity { ... @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "FLIGHT_ID") private Long Flight.flightId; public Long Flight.getFlightId() { return this.flightId; } public void Flight.setFlightId(Long id) { this.flightId = id; } ... }
Now, lets say that we want the Flight
entity's identifier value generated using a database table named ID_GENERATOR
. This means we need to annotate our flightId
field with the @TableGenerator
and change the identifier generation strategy to GenerationType.TABLE
.
You can override the Roo-generated identifier definition by defining the identifier (with the same name) in the entity's Java source file (and not in the *_Roo_Entity.aj
file). The following code shows the modified Flight.java
file containing the flightId
identifier definition:
... public class Flight { ... @Id @TableGenerator(name = "Flight_Gen", table = "ID_GENERATOR", pkColumnName = "ID_COLUMN", valueColumnName = "ID_VALUE", pkColumnValue = "FLIGHT_ID_VALUE", initialValue = 10, allocationSize=100) @GeneratedValue(strategy = GenerationType.TABLE, generator = "Flight_Gen") private Long flightId; public Long getFlightId() { return this.flightId; } public void setFlightId(Long flightId) { this.flightId = flightId; } ... }
The @TableGenerator
JPA annotation specifies the details of the table used for primary key generation. The @GeneratedValue
JPA annotation specifies the strategy used for generating primary key values. The GenerationType.TABLE
indicates that the value of the flightId
primary key is obtained from the database table identified by the generator
element of the @GeneratedValue
annotation.
When you an define identifier in the Java source file of an entity, Roo automatically removes the identifier definition from the corresponding *_Roo_Entity.aj
AspectJ ITD file. If you now check the Flight_Roo_Entity.aj
file, then you will find that the flightId
definition has been removed from it.
Roo's entity
command provides limited support for creating different types of identifier definitions. In most scenarios, you will possibly find it compelling to customize the Roo-generated identifier definition by defining the entity identifier in the entity's Java source file.
3.16.51.157