Hibernate Entity with 2 foreign key options - java

I have that use case:
I have table A and table B (none of them is extending the other, they are logically different). Both tables should have a link with one or more 'documents'.
So I can design table A and table B, but how to design the table for the 'documents'? Should it have 2 foreign key constraints (there is a possibilite one record from documents to have only 1 foreign key to Table A or B and the other foreign key will be null)? How I will declare that in SQL and in Hibernate/JPA. Thanks in advance.

You can have one-to-many unidirectional relationship from Table A/B to documents. By doing so, you will require link tables between Table A/B and document (in other words, document will be a master table). Also you will require to store document separately first and then can assign the documents to the Table A/B entities.
Alternatively, if want to save the documents along with Table A/B entity, then you need to maintain both foreign keys which will be nullable i.e. either A or B.
Both approaches are fine hence depending on your business requirement you should pick one.

Related

JPA mapping in 2 entities to model 2 tables having reciprocally a foreign key each other

I have to create the JPA entities from an existing Oracle database that i am not allowed to change the structure.
In this existing Oracle database that i can not change, i have 2 tables :
a table A that presents a foreign key to the table B
a table B that presents a foreign key to the table A
There is NO assocation table between tables A and B.
I have created an entity corresponding to the table A and another entity class corresponding to the table B.
But i can not find the good relationship between these 2 entities.
#OneToMany in these 2 entities is not good.
But #ManyToMany does not convince me too.
Please, have you an idea of a correct mapping for this strange relationship between 2 tables ?
Thank you for your answer.

JPA Composite Key for one Table and a Primary Key for another Table - Possible?

Is it possible to have both a composite key and a primary key in the same Domain Model (Entity Class) so that some tables (queries) are joined using the composite key and other tables (queries) are joined using the primary key?
I'm dealing with legacy applications and I have limited access to changing the underlying database. Some of our queries are expecting a single row result but are getting many rows because of flaws in our database design. We can fix this problem by introducing a composite key to one of our Domain Models but doing so will affect many (many) other components that rely on the original primary key.
From my understand of JPA and the reading I've done so far on this matter I do not think this is possible but I thought it would be worth a shot to reach out to others who may have had a similar problem.
The table has only one primary key, so you have no options to choose which primary key to use. Also, i can't understand why you going to have differences between database original model and JPA. Actually, getting single row instead of many rows is where clause's task.
You said some of your queries fails after adding composite pk, so may be you just made your composite pk in wrong way?
Anyway, here is nice example or implementation composite pk, may be it will help you:
Mapping ManyToMany with composite Primary key and Annotation:
Maybe you should give a different look at your problem.
If your queries are returning multiple and different rows, then you should be able to resolve this using a more specific WHERE clause;
If your queries are returning multiple and equal rows, you should try the DISTINCT clause inside your query, example:
SELECT DISTINCT e FROM br.com.stackoverflow.Entity e

It is possible to maintain relation between two table in Hibernate level?

I am new to Hibernate. Can you please answer my question?
Is it acceptable to maintain the 2 table relations in ORM level? I dont want to add foreign key relation between two tables, but I would like to add many to one relation in hbm file.
Ex I have Account and Account_Type tables. Account table contain AcntType column. Its not null column. AccountType contain AcntType as PK. There is no FK relation in Account table.
Now I would like to add relation from ORM level, I don't want to alter Account table and add foreign key constraint now.
I would like to add "many-to-one" attribute in my Account hbm file. I would like to add "one-to-many" attribute in my AccountType hbm file.
Is it acceptable to maintain relation in ORM level nor from DB level. I am using Oracle DB
Hibernate doesn't care if there is a foreign key constraint or not between the tables.
But adding a foreign key constraint guaranteees that, whatever you use to update your database, and whatever bug your code could have, you'll never have an account referencing an account type that does not exist. So you should definitely have a foreign key constraint.

Why is in many to one bidirectional relation many side is always the owning side of the relationship?

documentation says:
The many side of many-to-one bidirectional relationships must not
define the mappedBy element. The many side is always the owning side
of the relationship.
Can you explain why? what the reason?
Each of the (potentially many) rows representing the entity on the "many" side contain (foreign key) references back to single row representing the entity on the "one" side of the relationship as a single database column. In order for the "one" side to own the relationship, the keys for all of the rows corresponding to the "many" side would have to fit into a single database table row, which isn't practical.
In other words, if A is the "many" side and B is the "one side", for each row in the B table, there are potentially several rows in table A that contain a reference to it. Each of those B rows can store the key of the linked A row in a single column. There's no way for that one A row to store the keys of all those B rows.
For the same reason, in the case of a many-to-many relationship, a join table always ends up getting used in addition to the tables representing the entities in order to link the foreign key references as single columns.
thats because in the database, the many side has the foreign key. for example consider tables
User and Transaction with relation user has many transactions. In the transaction table, every transaction has a user_id which is a foreign key to the user table not the other way round. User table does not have a transaction_id.

Can i add all indexing in single table if my tables are in many to many relationship?

Example:-I have 3 tables product, branch & employee.Those are in Many to many relationship.So can i add all primary keys of those in new table as a foreign key for referencing?Instead of creating 3 different tables.means combination of product_branch, product_employee & branch_employee.
that relationship may have meaning - or maybe not.
probably this does not avoid the need to have other normalized relationships between just the employee and the branch for example.

Categories

Resources