Modelling a manyToMany relationship with attributes - java

I have a ManyToMany relationship between two classes, for instance class Customer and class Item. A customer can buy several items and an item can be bought by different customers. I need to store extra information in this relationship, for example the day when the item was bought. I wonder how is this usually modelled in JPA, cause I'm not sure how to express this in code. Do I have to create a new class to model all the attributes of the relationship and make a manyToMany relationship between the other classes or is a better way to do this?
Thanks

The recommended way is to create a new association class to store the needed attributes, and two one-to-many associations to the two parties involved.

I guess you will indeed have to create a new class for the relationship.

Like you said yourself, the correct way is to create a new class with the additional attributes.

Related

Do I need another association in my UML diagram?

I have made this simple UML diagram.
I want to have a list of treatments inside class Customer.
My question is if I need another association to make this possible.
A class diagram describes classes and not objects. A TreatmentList is a TreatmentList, regardless if you look at it from the Menu or the Customer persepective.
So indeed, if you want a list of treatment "inside" the class Customer, you'll need just to add another association between the classes you want to connect.
Now I wonder if you did not try to design user interface using a class diagram:
Main -> Menu -> List 1 or List 2 looks terribly like a flow in the user interface, more than a set of related classes. You don't need this. And lass diagrams are not meant for this purpose. If you want to model an UI Menu, you'd model a menu class, i.e a general class that could instantiate any kind of menus, with a one-to-many association with MenuAction. Perhaps ShowTreamtmentList would be a specialization of such an action.
The TreatmentList and CustomerList only make sense if these are classes, i.e. they could be instantiated into one or several objects that each represent a different list. If, for an association, you'd have a multiplicity of * on the side of customer or treatment, you would not need to add a special list/set/bag in the middle.
You don't need to use an aggregate instead of an association. Aggregate are not very uselful and appear to be overused. Prefer a simple association.

Need Clarifications about Jhipster Entity Relationships

one to many
many to one
many to many
one to one
Need some more clarifications about them. Referred that in many sites but cant able to get the perfect definitions. thanks in advance
I explain you these conceptual data modeling relations on the example.
One to many (1:n) means one entity can work with more ones.
Example: One employer can hire more employees.
Many to one (n:1) works same as the one above from the different view.
One to one (1:1) means one entity can work only with the other one.
Example: Every employee can work only with one computer
Many to many (n:m) means there is a combination of relations between entities.
Example: Each customer belongs to more employees and one employee serves for one or more customers.
Usually in database systems there will be created a new table holding these relations in case of m:n.

Is acceptable Entity circular reference in domain model?

I've seen a lot of domains designed with circular reference. The image below describes a simple example for understand the question: A company has employees and departments and a department has employees, who belong to a department.
Logically, the model allows a employee who works for a company be related with a department of another company - of course, hypothetically, if business logic validation fails.
In some more complex cases, and according to the business rules, I donĀ“t see another way to model.
Is it acceptable?
Thanks in advance.
It is absolutely fine to have such kind of circular association.
Of course there are lots of pitfall that shoot your feet. However with extra cautious such kind of relationship is fact not always harmful.
Things that you need to pay extra attention includes:
Try to define the "owner" of the relationship. This is especially important if you are going to persist the entity
Make sure the owning relationships are not circular. For example, in your example, you can define Company owning the Company-Department and Company-Employee relationship, and Department owning the Department-Employee relationship
Make sure the relationship is consistent. For example, for the bi-directional relationship between Employee and Department, when you remove an Employee from a Department, make sure you remove the Department from Employee consistently.
Try to minimize the way to manage relationship. For example, instead of providing both Department#addEmployee(Employee) and Employee#addDepartment(Department), just provide Department#addEmployee(Employee). This should make your work of keeping consistency easier.
Still, if you can manage to make it uni-directional and non-circular, it is always easier to handle.
If you really want to avoid the situation you've described (where an employee "E" is associated with a department "D", but "D" and "E" are associated with different companies), you can try making your object model a tree, with no cycles:
Company --> Department --> Employee
There are trade-offs here. For example, this model doesn't directy handle Employees with no Department (although this could be papered over with a fake department like NoDepartment). This model may also require two "hops" to get from Employee to Company.

Should I use a facade to access JPA Entities?

I want to retrieve a product from my database. A product is made up of data from several different tables.
Is it best to create a facade that will talk to multiple entities in order to pull together all the info needed to make a product and should the facade marshal the object or is there another way to do this?
I was thinking the method in the facade would be getProduct() and this method would use multiple entities?
Thanks
Why not use a product Entity and have JPA do it all for you? An entity can span multiple tables, and your product Entity can itself have references to other entities where it makes sense. Your application can make accessors within the Product class to expose the referenced entities or just return data from those references as needed.

How to map this tricky entity/relationship model in Java?

I have little bit confusing many to many relationship between 3 entities. And i want to know how can be my object model look like. I have three Entities, A,B,C and A<->B (M:N) and associate table between both, A and B, is linked with another associate table which make another 1:n relationship with third entity. I have never seen such relationship which make 1:n relationship with another associate table. For further information please have look on following diagram.
Uploaded Image link
If i talk about object model then i will say "INSTANCE_A" has many "INSTANCE_B" instance and vice versa but i do not know how can i summarize relationship for "INSTANCE_C".
Please also let me know whether definition of such relationship between all three entities is right ? i mean is there any problem in relationship design.
Thanks in advance
EDIT: All arrows denote (1:n or m:1) relationship
The data model is correct, but the object model for these tables can be kind of trucky. I'd do something like this:
One class for TBL_A, with a List attribute of TBL_B
One class for TBL_B, with a List attribute of TBL_A
One class for TBL_C_TBL_A_B, with and an attribute for TBL_B, TBL_A and TBL_C
Mapping that in an ORM framework can get funky.
This shall bring you into the right direction. Try to design a UML diagram, or ER should be ok too. Here is some paper with a Model and the corresponding Java-Code for this model http://www.csd.uoc.gr/~hy252/references/UML_for_Java_Programmers-Book.pdf. (Go to -> Class diagrams chapter).

Categories