Table relationship in JPA - jpa

How to change the namedquery of a particular field whose relation is being done by another field.

Related

eclipselink jpa joined table inheritance with mappedsuperclass

I have 2 tables. Employee and EmployeeDetails. Employee table has the basic details like Employee Id, Department and some audit fields like Created By, Created Timestamp. EmployeeDetails table has all the personal details about the employee and same audit fields (Created By, Created Timestamp) like Employee table. Now the audit fields and Version column are part of a MappedSuperclass ModelBaseFields.
I am using JOINED Inheritance in Employee which is my base class. It extends ModelBaseFields which is a MappedSuperclass. EmployeeDetails extends Employee.
Now the problem is, whenever I try to persist the data, Employee table INSERT query is formed properly however, EmployeeDetails INSERT query is missing audit fields (Created By, Created Timestamp) and version column.
I have tried using SINGLE TABLE inheritance with Secondary table. I am getting same issue in that scenario as well.
How do I add common columns in child table?

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.

Need help with Entity Framework Relationshipships

I'm new to Entity Framework and just experimenting...
Consider 3 db tables where Person is a base table. I want the Employe table to enherit from Person, storing employee specific info. It seems that EF requires that PersonId also be the PK of the Employee table, so I made EmployeeID a unique index.
Next I added a table, Application, which stores one record for every software application that the Employee supports, creating a foreign key from Application.EmployeeId to Employee.EmployeeId.
However, EF doesn't seem to recognize relationships that involve unique indexes, but only Primary Keys.
What I can do is create a relationship from Application.PersonId to Person.PersonId, however, only Employees can be responsible for an Application, so it seems more natural to me to have Application as a "child" of the Employee table rather than the Person table.
Is this possible in EF?
You can build your relation between Employee (PersonId) and Application (EmployeeId). In such case the integrity should work as you expect because only PersonIds in Employee table will be only for existing employees. EF has currently no support for unique keys.

How to insert many2many table into entity

I have a entity and a many to many table associate with it. Because the many2many table have a nchar field, it can not be mapped to a association in EFv1. Is that possible to create a Entity hold both the original entity and the nchar field? Thanks!
You can map such a table, but not as a many to many association with properties, because associations in the EF don't have properties. Instead, you'll have two entities with one to many associations to a third entity representing the association.