In the previous recipe, we saw how the many-to-one (or one-to-one) relationship is established between entities using the field
reference
command. In this recipe, we'll extend the same example to show how a one-to-many (or many-to-many) relationship can be created using the field
set
command. The following class diagram shows a one-to-many relationship between the FlightDescription
and Flight
entities:
In the given figure, you'll notice that we have added a flights
field of type Set<Flight>
to the FieldDescription
entity, reflecting the one-to-many relationship between the FlightDescription
and the Flight
entity.
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_relationship_many_to_one.roo
script, which creates the flight-app
Roo project, sets up Hibernate as a persistence provider, configures MySQL as the database for the application, creates the Flight
and FlightDescription
JPA entities, and defines a many-to-one relationship between the Flight
and FlightDescription
entities. 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.
To create a one to many (or many to many) relationship between the FlightDescription
and Flight
entities, follow the steps given here:
FlightDescription
entity:.. roo> focus --class ~.domain.FlightDescription
FlightDescription
and Flight
entities using the field
set
command:~.domain.FlightDescription roo> field set --fieldName flights --type ~.domain.Flight --cardinality ONE_TO_MANY --mappedBy flightDescription
The field
set
command is used to create the many side of a one-to-many JPA relationship within an entity. If the relationship between two entities is many-to-many, then the field
set
command is used for both the entities in the relationship. The following table describes some of the important arguments that can be specified for the field
set
command:
Even though the Roo shell displays MANY_TO_ONE
, MANY_TO_MANY
, ONE_TO_MANY
, and ONE_TO_ONE
as possible values of the cardinality
argument of the field
set
command, it only accepts MANY_TO_MANY
and ONE_TO_MANY
. The reason for this—field
set
only makes sense in the case of a many-to-many relationship or when the entity is on the one side of a one-to-many relationship.
The execution of the field
set
command against the FlightDescription
entity results in the following field added to it:
@OneToMany(cascade = CascadeType.ALL, mappedBy="flightDescription") private Set<Flight> flights = new HashSet<Flight>();
The given code shows that the field
set
command simply adds a relationship field to the entity.
field
set
and field
reference
commands don't provide the option to specify the cascade effect that applies to the related entity. The behavior of the commands is as described here:
@ManyToMany
or @OneToMany
annotated JPA relationship field created using the field
set
command has the value of the cascade
element as CascadeType.ALL
, which means the entity operations such as refresh, persist, merge, and so on, are propagated to a related entity.@ManyToOne
and @OneToOne
annotated JPA relationship field created using the field
reference
command don't have the cascade
element specified, which means entity operations such as refresh, persist, merge, and so on, are not propagated to an associated entity.In scenarios where you want to specify the cascade effect, you can modify the corresponding JPA annotation in your Java source file.
18.191.189.23