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

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.

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.

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?

JPA 2 one to many - how does JPA infer column information?

I have a JPA2 (Hibernate) application which uses a MySQL database with only two tables. One table is called "companies" and the other table is called "employees". Between the two tables there is a one-to-many ralationship (1 company has many employees). The foreign-key column in table "employees" is called "company_id".
In my JPA2 Application I use the following annotations:
In the entity class "Company" I have the following annotation
#OneToMany(cascade = CascadeType.ALL)
private Collection<Employee> employees;
and in class Employee
#ManyToOne
private Company company;
How does JPA know what column it should use to determine all employees of a company. The annotations do not hold this information, but the application works.
Thank you
The ManyToOne side is missing the optional JoinColumn annotation, which in turn has the optional name attribute defaulting to:
The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "(underscore)"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; "(underscore)"; the name of the referenced primary key column.
On the other side of the relationship, the OneToMany side, it's missing the mappedBy attribute (it should be equal to the name of the field that owns the relationship, in your case "company"). Javadoc says that this attribute is required unless the relationship is unidirectional, so there are chances that the JPA implementation you are using is assuming the relationship is unidirectional.

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