When a persistent entity is created using Roo, a number of methods are auto-generated to simplify usage and testing of the entity. For instance, when the Flight
entity was created in the Creating persistent entities recipe, the corresponding Flight_Roo_Entity.aj
AspectJ ITD file was created with methods like persist
, remove
, merge
, flush
, findFlight
, and so on.
In this recipe we'll look at how to control the generation of entity methods by:
For the purpose of this recipe, we'll instruct Roo to do the following for the Flight
entity:
persist
auto-generated method to save
findFlight
auto-generated method to finderForFlight
countFlights
and findFlightEntries
methodsExit the Roo shell and delete the contents of the C:
oo-cookbookch02-recipes
directory.
Execute the ch02_jsr303_fields.roo
script. It creates a flight-app
Roo project and sets up Hibernate as the persistence provider using the persistence
setup
command. The script also creates a Flight
entity, which has FlightKey
as its composite primary key class, and adds fields to the Flight
and FlightKey
classes. If you are using a different database than MySQL or your connection settings are different from what is specified in the script, then modify the script accordingly.
Start the Roo shell from the C:
oo-cookbookch02-recipes
directory.
The following steps will show you how to control auto-generated methods:
Flight.java
file in your favorite IDE.@RooEntity
annotation to:@RooEntity(identifierType = FlightKey.class, persistMethod="save", countMethod="", findMethod="finderFor", findEntriesMethod="")
Updated SRC_MAIN_JAVAsample
ooflightappdomainFlight_Roo_Entity.aj
The @RooEntity
annotation of Roo is responsible for managing the methods defined in the corresponding *_Roo_Entity.aj
AspectJ ITD. @RooEntity
annotation defines attributes which let you specify the prefix to be used for the methods generated by Roo and also to control whether a particular method is generated or not. The @RooEntity
annotation also defines attributes which you can use to specify entity identifier and version fields name, type, and table column information.
In the previous code, the @RooEntity
annotation specifies the following information:
identifierType
=
FlightKey.class
: specifies the Flight
entity identifier type as FlightKey
class. The default value is Long.
persistMethod
=
"save"
: the value 'save' means that instead of generating a persist
method, Roo generates a method named save.
countMethod
=
""
: as the value is "", it means Roo must not generate countFlights
method.findEntriesMethod
=
""
: as the value is "", it means Roo must not generate findFlightEnteries
method.findMethod
=
"finderFor"
: as the value is finderFor
, instead of generating findFlight
method, Roo will generate method named finderForFlight
.The follow table describes all the attributes defined by @RooEntity
annotation:
@RooEntity attributes |
Description |
---|---|
|
Attributes for specifying the prefix of the generated method. A value of "" means that the method will not be generated by Roo. |
|
Attributes for specifying JPA entity identifier information, which includes the name of the table column to which the identifier field maps, the name of the identifier field in the AspectJ ITD file, and the Java type of the identifier field. |
|
Attributes for specifying the version field information, which includes name of the table column to which the version field maps, name of the version field in the AspectJ ITD file, and the Java type of the version field. |
|
Attribute that specifies names of the methods for which dynamic finder methods are generated by Roo. |
|
Instructs Roo to generate a |
|
Inheritance type to be used for the JPA entity. |
|
The name of the persistence unit, defined in |
|
[Supported since Spring Roo 1.1.5] The name of the transaction manager associated with the entity. |
18.224.56.29