i have been researching on this topic and havent found any answers yet. Im using spring roo and i would like to know if theres a way I can establish a many-to-many relationship with attributes within this relationship. For example i have two tables Employee and MedicalEquipment, Employees can reserve many equipments, and equipment could be reserved by many employee, however i want to store the date this reserve took place.
If some one can help i would appreciate it. Thanks in advance!
Why can't you have a separate entity to denote your relationship?
Just introduce a new entity called a
MedicalEquipmentReservation
which would contain all the attributes of the reservation along with the relationships between the Employee and the Medical Equipment entities.See the following example.
Cheers and all the best with Spring Roo!
In order to achieve a many-to-many relationship with attributes, you need a join table which contains the additional columns. That is, besides Employee and MedicalEquipment, you will need a third EmployeeMedicalEquipment.
Regarding JPA you have two options:
The former is more complicated, but it allowed you to have bidirectional navigation (because it's an entity and hence, can have shared references), the latter is simpler in both building it and using it, but you can't use it to navigate between entities (however, you can write a query to retrieve the objects you need)
In my case, I needed to create an intermediary entity because the table belongs to a legacy database that already existed.
I did something like this:
Furthermore, you need to guarantees referential integrity by managing collections on either side of the association using the constructor. So you need to edit the class in such way:
I tried to give you an example of a Roo configuration.
You can find a better explanation of the JPA stuff in the book from Manning "Java Persistence with Hibernate", chapter 7.2.3.
Note: if you use roo 1.2.1, the count query will generate SQL with "count(id1, id2)", which is not supported by all databases, including HSQLDB. You can customize it like this:
...