OneToOne Join On Non-Primary Column Spring Data JPA Hibernate - spring-data-jpa

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?

Related

How JPA knows the foreign key field from a mappedBy attribute?

In this example here
I understand what the mappedBy attribute is doing. It is telling JPA that the foreign key exists in Users table. But the foreign key in db is a field, but here they are designating a whole object
BillingInfo
as foreign key. How does JPA know which field inside BillingInfo object serves as foreign key?
mappedBy = "billingInfo" doesn't say where the foreign key is. It's telling Hibernate that the annotated field (user) constitutes the inverse side of a bidirectional association, and that the owning side of this association is the field User.billingInfo.
Hibernate thus looks how the User.billingInfo is mapped to know how this bidirectional association must be mapped.
In this case, the only annotation on User.billingInfo is #OneToOne. Since that doesn't say how the association is mapped, the defaults specified in the JPS specification will apply, and, IIRC, a join column named "billingInfo_id" will be used.

Entity Framework : junction tables with own primary keys

Ì am currently working on a ASP NET MVC project. We use Entity Framework and follow the Database First approach. The database already exists.
The database has been created using the convention, that every table has a specified single primary key, even if it is a junction table.
Example :
Table User :
UserId (PK);
Username
Table UserRole :
UserRoleId (PK);
UserId (FK);
RoleId (FK)
Table Role :
RoleId (PK);
Rolename
As said, the database already exists and this convention is not discussable.
When I want to create an Entity Data Model in Visual Studio, I also have three Entities. But it would only make sense to have two Entities: User and Role. The UserRole Entity makes no sense.
Is there any possibility I can influence the way that Entity Framework maps my tables, so I can get rid of those relational (useless) entities?
Is there any possibility I can influence the way that Entity Framework
maps my tables, so I can get rid of those relational (useless)
entities?
No, you cannot force EF designer to do that. When using automatic tools you will always end with junction table mapped as a separate entity because it is not considered as junction table any more - it has special data (a separate key) which gives this entity new possibilities (for example relation between two entities can exist multiple times which is not possible with normal junction table).
The only way to avoid this is abandon tooling support and use either code mapping or manually write EDMX file and don't tell EF about that additional key. Instead let EF believe that there are only those two FKs forming composite PK as expected from junction table. Obviously if your database requires those special possibilities allowed by separate PK you cannot do this.

implement foreign Key in JPA side( in database this relation is nt implemented)

how to implement Foreign key relation in JPA side ( There is no foreign key for this relation in db, Db owned by another application , i cant able to change db structure( SW vendor not allowing me to do it)
Is there just no foreign key constraint, or nothing referencing the id at all?
If there is just no constraint, then it does not matter, JPA does not care if there is a constraint or not, just use the column that references the id.
If there is nothing referencing the id, then you cannot have a relationship with nothing store it. If you cannot alter the table, then perhaps you can add a new table that defines the join between the two tables (similar to a many to many, but JPA also allows a join table to be used for a one to one).

Entity Framework Compound Key (Many-To-Many Relationship with a Payload)

I've got database tables like this:
A person may be a member of many teams. A team may have many members. Each person may have a position (think job title) within the team.
I've tried to set this up with ADO.NET Entity Framework and get errors:
Error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.PersonId is mapped to
<MembershipPerson.Membership.PersonId,
MembershipPerson.Person.Id>
and
error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.TeamID is mapped to
<MembershipTeam.Membership.TeamId,
MembershipTeam.Team.Id>
The primary key of my Membership entity is a compound key of two foreign keys. I think that's the problem.
What must I do differently?
This happens if you use independent association on the property which is both part of primary key and foreign key. EFv4 introduced Foreign key associations (the difference is described here) and once you expose foreign key in the entity you must define foreign key association. After defining referential constraints delete mapping of independent association in Mapping details window.

Entity Framework Association with Non Key fields

Is it possible to create associates b/t 2 non-key fields in the Entity Framework?
Example: Take the 2 tables in a legacy application (i.e. keys/structure cannot change)
Order (
OrderId : int : PK
OrderNo : varchar
)
OrderDetails (
DetailRecordId : int : PK
OrderNo : varchar
)
In the Entity Framework, I want to create an association b/t Order and OrderDetails by the OrderNo field, which is not a primary key on either table or a FK relationship in the database.
This seems to me as not only should it be easy to do, but one reasons to use something like EF. However, it seems to only want to allow me to create associations using entity keys.
The Entity Framework allows you to claim that columns are keys and that FK constraints exist where none actually exist in the database.
That is because the SSDL (StorageModel part of the EDMX) can if necessary be manipulated by you and lie about the database.
The EF will then interact with the database as if the keys and foreign keys really do exist.
This should work, but all the normal caveats about referential integrity apply.
See my Entity Framework Tips
Hope this helps.
The problem with using non-key fields to define relationships is that the keys are not guaranteed to be properly navigatable. That could lead to a situation where you have a one to one relationship between two entities where there are more than one possible rows that fufill the relationship.
...when relating data from a database, the relationships should always be based on keys. The keys enforce the referential integrity.
One more workaround:
create view vOrder which will not include PK and create Entity from it.
Set PK in this entity to OrderNo
Now you will be able create association