Indexing for key which belongs to Primary key of other table - mysql

I have a two table as table as
#Entity
#Table(name = "product", uniqueConstraints=#UniqueConstraint(columnNames={"product_idn"}))
public class Product implements Serializable {
private static final long serialVersionUID = 21409635044L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String product_idn;
}
another table as
#Entity
#Table(name = "storage")
public class Storage implements Serializable {
private static final long serialVersionUID = -67165579239L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "product_id", nullable = false)
#JsonBackReference
private Product product;
}
Now I am fetching record from storage table. Should create a index (Non-Unique)on product_id of storage table? I have to fetch storages count per on product/ featching storages based on product.

Just add #Index annoation like below and it should work fine.
#Entity
#Table(name = "storage", indexes = #Index(columnList = "product_id",name = "storage_productId_index"))
public class Storage implements Serializable

Related

How to define composite foreign key mapping in hibernate?

I have two tables: users and userdetails as follows:
package com.example.easynotes.model;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Table(name = "users")
#IdClass(UserID.class)
public class User implements Serializable {
#Id
int id;
#Id
String name;
String department;
//getters and setters
}
The userdetails classes will be this:
public class UserDetails implements Serializable{
int id;
String name;
String address;
String otherFields;
//getters and setters
}
id and name in users is a composite primary and I want the same fields in userdetails to be the foreign key. How can I achieve this in hibernate ?
We need to put both key in #Embeddable to detach compound key thenafter, put it in User Entity using #EmbeddedId and map both primary key using Hibernate Relational Mapping...
There are two option to Composite Primary Key:
Using #EmbeddedId
Using #IdClass()
Here down is example:
----------------------------------- Using EmbeddedId -----------------------------------
Compound primary key:
#Embeddable
public class UserIdName implements Serializable {
int id;
String name;
// getter and setter
}
User:
#Entity
#Table(name = "users")
public class USER{
#EmbeddedId
private UserIdName id;
String department;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Set<Userdetail> userdetail;
// getter and setter
}
UserDetails:
#Entity
#Table(name = "Userdetail")
public class Userdetail {
#Id
private int detail_id;
#ManyToOne
#JoinColumns({ #JoinColumn(name = "id", referencedColumnName = "id"),
#JoinColumn(name = "name", referencedColumnName = "name") })
private USER user;
String address;
String otherFields;
// getter setter
}
----------------------------------- Using IdClass -----------------------------------
Compound primary key:
public class UserIdName implements Serializable {
int id;
String name;
// getter and setter
}
User:
#Entity
#Table(name = "users")
#IdClass(UserIdName.class)
public class USER{
#Id
int id;
#Id
String name;
String department;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Set<Userdetail> userdetail;
// getter and setter
}
UserDetails:
#Entity
#Table(name = "Userdetail")
public class Userdetail {
#Id
private int detail_id;
#ManyToOne
#JoinColumns({ #JoinColumn(name = "id", referencedColumnName = "id"),
#JoinColumn(name = "name", referencedColumnName = "name") })
private USER user;
String address;
String otherFields;
// getter setter
}
-> If you wanna insert both foreign key manually try below code
Put this code in UserDetails
#ManyToOne
#JoinColumn(name = "id", referencedColumnName = "id", insertable = false, updatable = false)
#JoinColumn(name = "name", referencedColumnName = "name", insertable = false, updatable = false)
private USER user;
#Column(name="id")
private int id;
#Column(name="name")
private String name
// don't forget to put getter setter
User Table:
User Detail Table:

How can I use post method data in back end to save many to many relationship data with #JsonIgnore annotation.My joining table is not updating

How can I use the data send through post method, and save those data in a many to many relationship? I used #JsonIgnore annotation to stop the recursion.
I have implemented two entities.One is Employee and the other is Skills.In the Employee entity I used #JsonIgnore annotation to avoid the reccursion. But when I inserted the values Employee table got updated but the joining tabled is not updating.
This is my Employee entity class
#Entity
#Table(name = "Employee")
public class Employee implements Serializable{
private static final long serialVersionUID = -3009157732242241606L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long emp_id;
#Column(name = "emp_fname")
private String emp_fname;
#Column(name = "emp_lname")
private String emp_lname;
#Column(name = "emp_email")
private String emp_email;
#Column(name = "emp_dob")
private Date emp_dob;
#ManyToMany(cascade = CascadeType.MERGE)
#JoinTable(name = "emp_skills",
joinColumns = #JoinColumn(name = "emp_id", referencedColumnName = "emp_id"),
inverseJoinColumns = #JoinColumn(name = "s_id",referencedColumnName = "s_id"))
#JsonIgnore
private Set<Skills> skills;
}
This is my Skills class
#Entity
#Table(name = "Skills")
public class Skills implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long s_id;
#Column(name = "s_name")
private String s_name;
#ManyToMany(mappedBy = "skills")
private Set<Employee> employees;
And this is my controller class method to save data
#RequestMapping("/save")
#PostMapping("/save")
#CrossOrigin
public void createEmployee(#RequestBody Employee employee, BindingResult bindingResult){
Employee emp = new Employee(employee.getEmp_fname(),employee.getEmp_lname(),employee.getEmp_email(),employee.getEmp_dob());
emp.setSkills(employee.getSkills());
empRepository.save(emp);
//empRepository.save(new Employee(employee.getEmp_fname(),employee.getEmp_lname(),employee.getEmp_email(),employee.getEmp_dob()));
}
I want to update both Employee and emp_skills (the joining table) when the save method is triggered.

path expected for join JPA Hibernate MySQL

I'm using composite PK in my app with 2 tables and one joining table.
I wrote this query for function:
#Repository
public interface HospitalDoctorDao extends JpaRepository<HospitalDoctor, Integer>{
#Query("select hd from HospitalDoctor hd join hospital on hd.hospital_id=hospital.id join doctor on hd.doctor_id = doctor.id where hospital_id = ?1 and doctor_id = ?1")
HospitalDoctor findByHospitalIdAndDoctorId(int hospital_id, int doctor_id);
}
and I am getting error Path expected for file! In MySQL everything is working. How Hibernate works in this case? How I should write this query? Here is my #Entity of join table:
#Entity
#Table(name = "hospital_doctor")
public class HospitalDoctor {
#Embeddable
static class HosdocPK implements Serializable {
private int hospitalId;
private int doctorId;
}
#EmbeddedId
#JsonBackReference
public HosdocPK hosdocPK;
#JsonManagedReference
#MapsId("DoctorId")
#ManyToOne(optional = false)
#JoinColumn(name = "doctorId", referencedColumnName = "id")
private Doctor doctor;
#JsonManagedReference
#MapsId("HospitalId")
#ManyToOne(optional = false)
#JoinColumn(name = "hospitalId", referencedColumnName = "id")
private Hospital hospital;
#Column(name = "Id")
private int id;
#Temporal(TemporalType.DATE)
private Date contract_start_date;
#Temporal(TemporalType.DATE)
private Date contract_end_date;
private String position;
private String supervisor;
private boolean part_time;
Getters and setters
}
Your query is incorrect.
Try:
select hd from HospitalDoctor hd where hd.hospital.id = ?1 and hd.doctor.id = ?2

Named query join select

I need to get GroupEntity objects which have assigned specific role in RoleGroupEntity Table.
I try to do this like that:
#NamedQuery(name = "GroupEntity.getGIDs", query = "SELECT o FROM RoleGroupEntity u JOIN FETCH u.GroupId o WHERE u.role_id LIKE :role_id")
but I got:
org.hibernate.QueryException: could not resolve property: role_id of: RoleGroupEntity
Entites sample:
#Entity
#Table(name = "group")
public class GroupEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "gid_number")
private Long gid_number;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "GroupId")
private List<RoleGroupEntity> GroupId;
RoleGroupEntity has Composite Key:
#Entity
#Table(name = "role_group")
public class RoleGroupEntity implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private RoleGroupCompositeKey posRGKey;
#MapsId("role_id")
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "role_id")
private RoleEntity roles;
#MapsId("group_id")
#ManyToOne
#JoinColumn(name = "group_id")
private GroupEntity GroupId;
Do you know how to make my select query to work?
You can not use the joincolumn you have to use: u.roles.id LIKE :role_id")
#NamedQuery(name = "GroupEntity.getGIDs", query = "SELECT o FROM RoleGroupEntity u JOIN FETCH u.GroupId o WHERE u.roles.id LIKE :role_id")

How to fetch set of data using ID in hibernate if we have composite primary key?

I have Entities (Student,StudentSkills, StudentEmpSkills)
Student.java
#Entity
#Table(name = "Student", catalog = "dbs")
public class Student implements java.io.Serializable {
private int id;
...
...
private Set<StudentSkills> studentSkills= new HashSet<StudentSkills>(0);
private Set<StudentEmpSkills> studentEmpSkills= new HashSet<StudentEmpSkills>(0);
#OneToMany(fetch = FetchType.EAGER, mappedBy = "Student")
public Set<StudentSkills> getStudentSkills() {
return this.studentEmpSkills;
}
public void setStudentSkills(
Set<StudentSkills> studentSkills) {
this.studentSkills = studentSkills;
}
#OneToMany(fetch = FetchType.EAGER, mappedBy = "Student")
public Set<StudentEmpSkills> getStudentEmpSkills() {
return this.StudentEmpSkills;
}
public void setStudentEmpSkills(
Set<StudentEmpSkills> studentEmpSkills) {
this.studentEmpSkills= studentEmpSkills;
}
}
in StudentEmpSkills.java
#Entity
#Table(name = "studentEmpSkills", catalog = "npdbs")
public class StudentEmpSkills implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private StudentEmpSkillsId id;
private Student Student ;
......
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "studentID", nullable = false, insertable = false, updatable = false)
public Student getStudent() {
return student;
}
In the above we are getting the Set of StudentEmpSkils from Student by one to many and many to one relation.
as we are getting Student from StudentEmpSkill
In JoinColumn we are giving studentID column to fetch the abject.
Now I want to get StudentSkill object from StudentEmpSkills.
StudentSkills - (studentID* | skillID*) | skillname
StudentEmpSkills - (studentID* | skillID* | empID*) | empName
(..) - composit primary key
So I want to fetch StudentSkills from StudentEmpSkill
What I need to write in StudentEmpSkills to fetch StudentSkill. because we have composit primary key in StudentSkill.
How to map the StudentSkill to StudentEmpSkills.
Can anyone please suggest?