Controlling auto-generated methods of persistent entities

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:

  • Specifying the prefix to be used for a method
  • Instructing Roo not to generate a particular method

For the purpose of this recipe, we'll instruct Roo to do the following for the Flight entity:

  • Change the name of the persist auto-generated method to save
  • Change the name of the findFlight auto-generated method to finderForFlight
  • Don't generate countFlights and findFlightEntries methods

Getting ready

Exit 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.

How to do it...

The following steps will show you how to control auto-generated methods:

  1. Open Flight.java file in your favorite IDE.
  2. Change the @RooEntity annotation to:
    @RooEntity(identifierType = FlightKey.class,
        persistMethod="save", countMethod="", 
        findMethod="finderFor", findEntriesMethod="")
  3. You'll see the following output on the Roo shell:
    Updated SRC_MAIN_JAVAsample
    ooflightappdomainFlight_Roo_Entity.aj
    

How it works...

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

countMethod, findAllMethod, findEntriesMethod, findMethod, flushMethod, mergeMethod, persistMethod, removeMethod

Attributes for specifying the prefix of the generated method. A value of "" means that the method will not be generated by Roo.

identifierColumn, identifierField, identifierType

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.

versionField, versionType, versionColumn

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.

finders

Attribute that specifies names of the methods for which dynamic finder methods are generated by Roo.

mappedSuperclass

Instructs Roo to generate a @MappedSuperclass annotation instead of @Entity. We'll see mapped superclass usage in Chapter 3, Advanced JPA Support in Spring Roo.

inheritanceType

Inheritance type to be used for the JPA entity.

persistenceUnit

The name of the persistence unit, defined in persistence.xml, with which the entity is associated.

transactionManager

[Supported since Spring Roo 1.1.5] The name of the transaction manager associated with the entity.

See also

  • Refer to the Creating persistent entities recipe to see how to create JPA entities using Roo
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.56.29