Generate one Id column included in composite Primary Keys in JPA, while one Id starts from 1 when the other Id column changes - jpa

The entity A has an OneToMany relation with the entity B. I would like to use a composite primary key for the entity B, where one of the columns is the same as the the primary key of the entity A and the second column should be generated. How can the second column be started from 1 whenever the the other involving column within the composite primary key changes?
I use Oracle data bank and could not find any solution.
Thanks in advance!

Related

Creation of table foreign key constraints in database with JPA

In JPA project, when I generate tables for entities I can't see any table foreign key constraints created on database side.
I concluded that JPA cannot create table foreign key constraints in database and that referential integrity is enforced in JPA (on application side) and not by database.
Can someone confirm if this is so?
According to the JPA 2.2 specification, the persistence manager should create foreign key constraints. For example in the case of a one-to-one mapping:
Assuming that:
Entity A references a single instance of Entity B.
Entity B references a single instance of Entity A.
Entity A is specified as the owner of the relationship.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.

Entity framework - referencing one of composite primary key columns as a foreign key

In table A I've got a composite of 3 columns as a primary key. I want to have only one of these three columns as a foreign key in table B, just to make sure that the value that I insert into table B's column exists in table A.
Currently from what I've read it looks like in Entity Framework I have to add all three columns of composite PK, which is not really what I need. The latest answer that I've found was of 2015, maybe since then something changed?
I know that I can add a manual check on each insert/update call, but I don't want to do that, maybe there is more elegant way.

OneToOne Join On Non-Primary Column Spring Data JPA Hibernate

I am using Spring data JPA(Hibernate).
I am trying to join my tables (Table A & Table B) but on Non-Primary Columns. Is it possible to actually do that? I am trying to use referenceColumnName, but it seems to not working, giving error :
Cannot set int to Integer.
When I am removing referenceColumnName, then it is working but obviously it is joining with Primary Key. Also in case of One-to-one Bidirectional, where should I place mappedBy & JoinColumn?
The annotation #JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table), whereas the attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity.
Regarding the other question of using joining tables on Non-Primary columns, there are plenty of threads why don't you go through. for example
Does the JPA specification allow references to non-primary key columns?

it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element error

While I was trying to deal with many-to-many entities I had a mistake
because it has a DefiningQuery and no element exists in the element
I found an answer on stack that I have to set primary key to the table, but I am not sure which column to select as primary key, because in all articles it is said that mapping table has to be table with just two foreign keys, adding third column (for example ID) just ruins this many-to-many relationship. I have
Projects and Clients and Project_Client table where are two columns with foreign keys one to ProjectId second on ClientId. So where i have to set primary key?
My edmx:
If I add field with Id entity framework recognizes this relationship in a different way.

Setting the value of primary Key manually on Entity Model before insert

I have my Entity model where every entity has a primary key and they are marked with StoreGeneratedPattern = "Identity", the entity I am interested in is PEOPLE. It was created in oracle, and in another schema exists a "PEOLPE_HIST" table with it sequence. Before insert into PEOPLE I search on PEOPLE_HIST and if the person exists. I get the history information to including the ID (primary key) to insert into new PEOPLE table. I created a trigger for PEOPLE table to check if the ID is greater than or equal to 0, if it is, then I will not use the sequence and insert the Hist ID.
Is this possible?
thanks in advance