In the join table one entry is missing that is I'd of 2nd employee with I'd of 2nd project. As e1 is working on both project and on p2 project bot employee are working
The reason why single project has not been assigned to multiple employees is that, In a bidirectional relationship between two entities, it is essential to update both sides of the relationship when adding or removing entities. Here, e1 is set with list of projects, and p2 is set with list of employees, but same has to be done for e2 and p1 as well. set e2 with a project and p1 with an employee, the table is udated successfully
For those commenting that project 2 is not assigned two employees. Here is the code he forgot to add. e1.setProjects(list2); e2.setProjects(new ArrayList(Arrays.asList(p2))); p1.setEmps(new ArrayList(Arrays.asList(e1))); p2.setEmps(list1);
guys tumlogo ko bhi problem aa raha hoga 1 project ek 2 employees show nhi ho raha hoga...so mapped by hata do....toh dikh jayega 1 project ke 2 employees but ek aur table create hoga mtlb 4 table
Sir one doubt project_2 has 2 employees e1 and e2 but in join_table we don't have entry for that, I guess one more column should have been there [e2,p2 ] correct me if I am wrong
Sir pls ek esi series b start kren project ki jis me ap koi b framework use na kren. I mean hibernate, spring ya status me se koi b na use kren framework. Lekn ek project bnayen desktop app ka jdbc k sath. Let it be student management or hospital management or whatever u like. But make sure to create it without usin framework. I want to Know how do we manually Map these relationships in jdbc and java application. I hope u get me and will soon create it Thanks. Love you :)
empl_proj table me one employee to multiple project ka relation dikh raha hai but One project to multiple Employees wala relation nahi dikh raha haii. like emplyId 34 is assigned 2 projets but any of the projects is not done by both of them, why this happening ???
In many to many mapping, an employee has been assigned to multiple projects but the single project has not been assigned to multiple employees. So it will be an example of OneToMany or ManytoMany. Please help to clear the doubt.
is there any extra setting to do ? Because upon setting the hbm2ddl = create every time table dropped first and then it create again, But you are manually dropping the Table many times... and also in logs hibernate fires the drop tables query as well and then create each table .
Nope his hbm2ddl has value of update he , i think he said mistakenly that its value is create . No extra setting is required if u have create value for hbm2ddl field.
Why do we add setters getters, a constructor with fields, and constructors without field in this video or any of your videos for that matter? please do reply to this or mention this question in any of your further videos.
I think p2.setEmps(list1); didn't work here. Because according to this command p2 should have been assigned to both the employees in the join table. Can you please clarify this sir?
Thanks for the videos bhaiya. Really learning a lot from you. Jitna ho sake utna video upload kijiye during this break. Maine apko instagram pe msg kiya hua hai, kuch question the time mile toh dekh lijiyega. Thank you.
Hi durgesh, Why there is no mapping entry in db for the project "Chatbot" with emp2? p2.setEmps(list1) clearly defines the many to many relationship ie. Project 2 is mapped with emp1 and emp2.Can you please check it again as there are only 2 mapping entries for emp1 only but for (pro2 -emp2) there is no entry. Thanks for lovely content.
Hi Suhail, according to the code Durgesh set: employee 1 --> project 1 & 2 projects and then project 2 --> emp 1 & emp 2 but he forgot to set project 1-->employee 1 and employee 2 --> project 2. Its bidirectional mapping, we need to set both to each other
I have do same project . I have one doubt . as per you told one employee assign many projects . my requirement is req.1]. emp 1 assign proj. 1 and 2 req.2] emp2 assign proj.2 and 3 req.3] emp3 assign proj2 only . please guide me how to do this . Thanks In advance
Exception in thread "main" org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: com.maping.Project.employees at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:564)
Everything is correct here . Those who are complaining about e2 projects. Just add this 2 lines below line 45 @12:01 e2.setProjects(projList); p1.setEmployees(empList); and everything will work as expected
If you use hbm2ddl = create, it is giving FK issue in first run but not in 2nd run.... and if you use hbm2ddl = update, no issue in first run... Don`t know why?? can anyone explain pls.. @Learn Code With Durgesh
yeah it can be bcz of cache problem. I am working in IntelliJ and I have to run 3 times in create mode to drop the table.😂But in update mode the program just has to update new things without erasing the older ones so that maybe the reason.
I have few issues, but solved...I am pasting my code may be it can be useful to you as well... //Project.java ============= package com.map1; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Project {
@Id private int pid; @Column(name="project_name") private String projectName;
@ManyToMany(mappedBy="projects") private List emps;
public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } public List getEmps() { return emps; } public void setEmps(List emps) { this.emps = emps; } public Project() { super(); this.pid = pid; this.projectName = projectName; this.emps = emps; }
What if we try to delete any project from proejct table. Then it gives error saying it exists as a foreign key in anothet table. How to handle this? If any project is deleted then all the related entries should also be deleted
When I am trying to post many to many relation through postman, it throws error saying, " Resolved [org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError]". I tried different methods like not using @Data. It didn't worked. What could be solution for this error
nested objects form ho rhe hain. jab aap employee get kroge toh usme employee { project { employee { project {....... ...... ....} } } ese recursion honge. Modify your toString() method. for each entity to avoid recursions.
really appreciable sir ji ..2012 se ye sikha par aapne jis andaaj me sikhaya ...Natmastak ho gya main to prabhu
In the join table one entry is missing that is I'd of 2nd employee with I'd of 2nd project.
As e1 is working on both project and on p2 project bot employee are working
Yes I have the same question.. can anyone clarify.. I don't have an IT background.. so it would really help.
Thanks in advance!!
Remove mapped by attribute from @ManyToMany annotation and it will create a new entity to map project_emp
@@kirtiarora9588 so can we say that many to many mapping is lost by introducing mappedBy attribute ??
The reason why single project has not been assigned to multiple employees is that, In a bidirectional relationship between two entities, it is essential to update both sides of the relationship when adding or removing entities. Here, e1 is set with list of projects, and p2 is set with list of employees, but same has to be done for e2 and p1 as well. set e2 with a project and p1 with an employee, the table is udated successfully
What if we use cascading will it also solve the problem?
For those commenting that project 2 is not assigned two employees. Here is the code he forgot to add.
e1.setProjects(list2);
e2.setProjects(new ArrayList(Arrays.asList(p2)));
p1.setEmps(new ArrayList(Arrays.asList(e1)));
p2.setEmps(list1);
Sir ke videos ka khajana khul gaya phir se. thanks sir!
keep learning
Thank you sir for your great teaching 😊🙏
guys tumlogo ko bhi problem aa raha hoga 1 project ek 2 employees show nhi ho raha hoga...so mapped by hata do....toh dikh jayega 1 project ke 2 employees but ek aur table create hoga mtlb 4 table
@12:44 1 Emp assigne to M Project can be seen , but 1 Project have M employees is not visible.
Sir can u pls clarify this, thanks
In the joined table one emp has two projects but however one project isn't being worked by two emps. Isn't it? Explain if somebody got it.
Yes right I'm also confused 😕
Same doubt for me also. 35 employee working on CHATBOT is not showing.
I am also trying to get this. In third table there should be 3 enteries
but second project has 2 employees set, where is this info in mapped table..?
Remove mappedBy tag u will get mapping of two tables
Sir one doubt project_2 has 2 employees e1 and e2 but in join_table we don't have entry for that, I guess one more column should have been there [e2,p2 ] correct me if I am wrong
yes same doubt
Remove mapped by attribute from @ManyToMany annotation and it will create a new entity to map project_emp
I've Fixed the bug ... Now its working . Keep the good work going mate .. And give some jobs too :)
Sir pls ek esi series b start kren project ki jis me ap koi b framework use na kren. I mean hibernate, spring ya status me se koi b na use kren framework. Lekn ek project bnayen desktop app ka jdbc k sath. Let it be student management or hospital management or whatever u like. But make sure to create it without usin framework. I want to Know how do we manually Map these relationships in jdbc and java application. I hope u get me and will soon create it Thanks. Love you :)
Great !!! Thanks Bro
Can we use paramaterized constructor put values in Emp e1=new Emp(11,"Ram","Hotel management"); instead of using setters to avoid code length?
sir jo project 2 ko jo humne do employess assign kiye the unki table to bani hi nhi???
Sir how can we fetch data from the third table?
empl_proj table me one employee to multiple project ka relation dikh raha hai but One project to multiple Employees wala relation nahi dikh raha haii. like emplyId 34 is assigned 2 projets but any of the projects is not done by both of them, why this happening ???
In many to many mapping, an employee has been assigned to multiple projects but the single project has not been assigned to multiple employees. So it will be an example of OneToMany or ManytoMany.
Please help to clear the doubt.
i have same doubt. Emp_Projects should have 3 records.. kuch to gadbad hai
@@kirtiarora9588 are you mad
Thanks
is there any extra setting to do ? Because upon setting the hbm2ddl = create every time table dropped first and then it create again, But you are manually dropping the Table many times... and also in logs hibernate fires the drop tables query as well and then create each table .
Nope his hbm2ddl has value of update he , i think he said mistakenly that its value is create . No extra setting is required if u have create value for hbm2ddl field.
Durgesh bhai getter, setter constructors k lie Lombok use kr skte ho.. generate krne ki jarurat ni padegi.. annotation se kaam ho jaega..
Hn g
Foreign key not created in emp table?
Why do we add setters getters, a constructor with fields, and constructors without field in this video or any of your videos for that matter? please do reply to this or mention this question in any of your further videos.
Learn java basics first then come to this playlist
I think p2.setEmps(list1); didn't work here. Because according to this command p2 should have been assigned to both the employees in the join table.
Can you please clarify this sir?
Thanks for the videos bhaiya. Really learning a lot from you. Jitna ho sake utna video upload kijiye during this break.
Maine apko instagram pe msg kiya hua hai, kuch question the time mile toh dekh lijiyega. Thank you.
best sir
Hi durgesh,
Why there is no mapping entry in db for the project "Chatbot" with emp2? p2.setEmps(list1) clearly defines the many to many relationship ie. Project 2 is mapped with emp1 and emp2.Can you please check it again as there are only 2 mapping entries for emp1 only but for (pro2 -emp2) there is no entry.
Thanks for lovely content.
Hi Suhail, according to the code Durgesh set: employee 1 --> project 1 & 2 projects and then project 2 --> emp 1 & emp 2 but he forgot to set project 1-->employee 1 and employee 2 --> project 2. Its bidirectional mapping, we need to set both to each other
Remove mapped by attribute from @ManyToMany annotation and it will create a new entity to map project_emp
@@kirtiarora9588 Do we need here total 4 tables? I don`t think so..
gajab ho bhai app
Sir agr ap xml file mein update ki jagha create laga dein tou apko bar bar table drop krne ki zaroorat nahi padegi
Unresolved compilation problem....... 10:33
Type mismatch: cannot convert from org hibernate.Transaction to javax.transaction.Transaction
I have do same project . I have one doubt . as per you told one employee assign many projects . my requirement is req.1]. emp 1 assign proj. 1 and 2 req.2] emp2 assign proj.2 and 3 req.3] emp3 assign proj2 only . please guide me how to do this . Thanks In advance
create new proj. and new proj. list with only proj. to be added and then add this to emp.
You need to create multiple list of project and multiple list of employee specific to each assignment in main class.
why giving null value in all table emp,project and emp_project
please make a video on inheritance mapping too...
P2 should have e1 and e2 employees
Remove mapped by attribute from @ManyToMany annotation and it will create a new entity to map project_emp
Exception in thread "main" org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: com.maping.Project.employees
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:564)
How can you assign mappedBy on both sides? It can be used only on a single side.
Everything is correct here .
Those who are complaining about e2 projects.
Just add this 2 lines below line 45 @12:01
e2.setProjects(projList);
p1.setEmployees(empList);
and everything will work as expected
Why?
it is only e1 who is assigned in two project and not e2.
If you use hbm2ddl = create, it is giving FK issue in first run but not in 2nd run....
and if you use hbm2ddl = update, no issue in first run...
Don`t know why?? can anyone explain pls.. @Learn Code With Durgesh
yeah it can be bcz of cache problem. I am working in IntelliJ and I have to run 3 times in create mode to drop the table.😂But in update mode the program just has to update new things without erasing the older ones so that maybe the reason.
thanks sir very helpful for us please start the spring frame work on project based like jsp servlet
But for me everytime why this two table are creating automatic.?
1)employee_project.
2)project_employee.
You missed it as below
@ManyToMany(mappedBy = "projects")
private List emps;
Sir many to many web app ki video please 🙏🏻🙏🏻
Sir aap ne hibernate me inheritance mapping q ni krwai 😔
Bhai tum bade ghuma ghuma ke bata rahe ho maine jitna sikha tha usme bhi tumhara video dekh ke aur confuse ho gaya😢
showing error line no: 42,43 8:46
sir plz code ko likh ne ke liye ek bar pura code show kar diya karo video ke end main
source code diya hai
Check Desc
waiting your reply !!!
Remove mappedBy Attribute
Not working
I have few issues, but solved...I am pasting my code may be it can be useful to you as well...
//Project.java
=============
package com.map1;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Project {
@Id
private int pid;
@Column(name="project_name")
private String projectName;
@ManyToMany(mappedBy="projects")
private List emps;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public List getEmps() {
return emps;
}
public void setEmps(List emps) {
this.emps = emps;
}
public Project() {
super();
this.pid = pid;
this.projectName = projectName;
this.emps = emps;
}
}
package com.map1;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Emp {
@Id
private int eid;
private String name;
@ManyToMany
private List projects;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getProjects() {
return projects;
}
public void setProjects(List projects) {
this.projects = projects;
}
public Emp() {
super();
this.eid = eid;
this.name = name;
this.projects = projects;
}
}
package com.map1;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public abstract class MappingDemo {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Emp e1= new Emp();
Emp e2 = new Emp();
e1.setEid(34);
e1.setName("Ram");
e2.setEid(35);
e2.setName("Shyam");
Project p1=new Project();
Project p2=new Project();
p1.setPid(111);
p1.setProjectName("Library Management");
p2.setPid(222);
p2.setProjectName("School Management");
List list1=new ArrayList();
List list2=new ArrayList();
list1.add(e1);
list1.add(e2);
list2.add(p1);
list2.add(p2);
e1.setProjects(list2);
p2.setEmps(list1);
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
s.save(e1);
s.save(e2);
s.save(p1);
s.save(p2);
tx.commit();
s.close();
factory.close();
}
}
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/demo_Project?useSSL=false
******
*******
org.hibernate.dialect.MySQL5Dialect
create
true
kya padata hai ye re kya padata hai.... devmanus hai ye toh
What if we try to delete any project from proejct table. Then it gives error saying it exists as a foreign key in anothet table. How to handle this? If any project is deleted then all the related entries should also be deleted
When I am trying to post many to many relation through postman, it throws error saying, " Resolved [org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError]". I tried different methods like not using @Data. It didn't worked. What could be solution for this error
nested objects form ho rhe hain. jab aap employee get kroge toh usme
employee {
project {
employee {
project {.......
...... ....}
}
} ese recursion honge.
Modify your toString() method. for each entity to avoid recursions.