How to decide which entity should be the owner of relation - java

Like in a topic. How to decide which entity should be the owner of relation. I have two entity and want to create mantToMany relation between. I hvae class teams and persons. One person can be in many teams, and many teams can have many person. But my question is how to decide in which entity should i create #jointable.

Related

Data Modelling question - Java/ Spring- Implementing Mutually exclusive association

I am trying to build a Book app.
The entities are so-
The Entity User has a field called Role which specifies if this is an Author or Reviewer. Currently, I am using an Enum data member to differentiate if the User is an Author or Reviewer. One single instance of a User cannot be both.
A Book will have 1...* Authors- So a User entity which is an Author will have a many-to-many relation with entity Book
A Book will have 1...* Reviewers- So a User entity which is a Reviewer will have a many-to-many relation with entity Book
I was wondering how would I implement the User side - I preferably want one single collection of Books- If the User is an Author, this will contain a reference to the books he has authored. If it is a reviewer, it will contain references to the books on which he is a reviewer.
I am wondering if there is any construct in JPA/ Hibernate which can enable this.
I could always implement Author and Reviewer as different entities, but I still want to know the answer to such a situation which I presume must be quite commonly encountered.

Entity relationship where one entity has references to 2 of the same entity type?

I'm working on a project for university that requires me to implement a basic Paypal-like money transfer system. I'm currently modelling the relationships between entities. My current problem involves 2 entities: User and Transaction.
The logic is that a Transaction has a sender User and a recipient User, and the amount of money sent. I'm struggling with modelling this relationship however as I can't figure out the correct way of forming the relationship between User and Transaction.
Is it correct to have 2 seperate one-to-one relationships on the Transaction entity which references the same attribute on the User but with a different column name on the database table?
This is a classical situation: two Entities are connected by two different relationships. In this case you have a one-to-may relationship Send between User and Transaction, and a one-to-many relationship Receive between User and Transaction.
And you should treat them as any other relationship for what concerns the implementation. For instance, in a relational database you can have a table Users with its attributes, and a table Transactions with, in addition to the amount of money and other relevant information, two different attributes, sender and receiver, both of which foreign keys for the table User.

How to store objects which have relations in ElasticSearch (Search engine)

I am going to use ElasticSearch for as the search repository in my application.
I have a few questions regarding what is best practice when it comes to organizing
objects in the search index when the objects have associations/relations to each other.
From what I know search indexes is a flat structure and doesn't work with the concept of
relations in the same way as a database.
Let say you have these domain objects:
Person:
- Has a one-to-many relationship with Car
Car:
- Is owned by one Person, many-to-one with Person
Department:
- Each Department have many People and each Person may belong to many Department, many-to-many
What would be the best way to store this in the search index? What are the options? For instance I want to find all the people belonging to a certain deparment, or all people where the car has more than 300 bhp.
I am using the Java client API if it matters.
Elastic search (or Lucene) isn't a relational database, so you would need to flatten your relationship model.
Try to model a view that gets this structure -
Car|Person|Department
This will give you all attributes required to lookup a car. This can be imported into a document for Car.
Similarly
Person|Department
will give you all information for a person. This will help you lookup a Person
Department can be a third document.
You can have multiple documents for each entity. But the relationship needs to be translated as a property of the entity.

Hibernate mapping

I need help in modeling a set of tables/classes in my project. I also need help on how to do the hibernate mapping for these tables. I have following tables in my project.
Person
Organization
Contact
Address
Person table can have one or more addresses. Organization and Contact can have only one Address. So I have added the following columns to establish the relationship between the tables.
Address table has PersonId [Since one Person can have more than one address].
Organization and Contact table has AddressId [Since these tables can have only one address].
I want to know what java classes I need to create for these tables. Currently I have Person, Address, Organization and Contact classes. Not sure how to link Address class to Person, Organization and Contact class.
I want to know whether I should consider Address as a Component or an Entity. And how create the xml mapping Address is considered as a Component.
The PersonId in Address table may have null values for the Address records created for Organization and Contact. I fine with creating a separate table [say Person_Address] to store list of addresses for a person. But having this link table might give an provision for many to many relationship between Person and Address tables. How do I enforce one to many relationship in this case.
I would go for what you suggest in 3.
By using a OneToMany relationship on the set of addresses in the Person class the intermediate table will be created automatically and I guess it will have a unique constraint on the address id.
Edit: You will only get a reference in the address table if you add a corresponding many-to-one annotation in the address class and use the and map the one-to-many annotation to that field. But since your not doing that you'll get a Person_Address table even with a one-to-many relationship without creating any extra classes.
You should model your classes like you would in plain java. I.e Person would have a set of addresses, the others will have just one and the Address class is oblivious of the others.
Then you add a #OneToMany annotation in the Person class and #OneToOne class in the others. Or you put that in your orm.xml, although annotations is much better for maintenance.
As for Component/Embedable vs Entity I would suggest entity as it is the simplest and no limitations. Don't use to many concepts at once, and stick to the main road.

Modelling a manyToMany relationship with attributes

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.

Categories