I think duration b/w 6:55 to 7:55 ,what you said is wrong,actually if you check the hibernate sql log,it's not creating any relation b/w these two tables.Your code is working because you have same primary keys in both the tables, if you change the primary of child table i.e user_contact and have different primary key in Users table(i mean primary key of both the table should not match) in DB and then, try to delete any entries from child table i.e user_contact by passing the Id ,then it will remove the record from user_contact table and other records in users table will not be removed........But as per the @OneToOne(cascade=CascadeType.ALL) ,it should not delete the entries from child table also ....But if you maintain a separate column as a foreign key then it will not let you delete.....so i think what you told is wrong........please correct me if i am wrong....i have source code if you want to contact me then ping me at 9962790783
hi sir, thanks for your effort to explain one to one mapping,one gentle request from my side,can you show me how to use postMapping for posting data in both table from one controller
Hi Tech Primers my request is how in real time we face issues and how to solve them and how to check logs please make a video of any real time project it will really help me and I will be very thankful full to u brother
BTW, you should not worry about the primary key sequence in the JSON response. Spring JPA/Hibernate will generate id in the sequence and the end of story when spring jpa persist a record you should see your FK and PK values to be same as they are associated using OneToOne mapping. Query : SELECT * FROM USERS_CONTACT UC, USERS US WHERE uc.users_contact_fk = US.ID
That helped a lot.. :) Is there any chance u can do the mapping without declaring the users table id in users contact as it already exists in the Users instance which we are declaring in the users contact table...?
Hello, Im trying to implement lazy fetch type to join 2 tables based on OnetoOne Mapping to improve performance issues. I see the same performance however. Any ideas?
Hi Anil, Looks like the PrimaryKey (Id) is not getting auto generated. Can you check if you had added @GeneratedValue annotation which populates the Id field
Hey, What if we have data in user table already and now we need to persist data in user contact table which is having a link to user table (same relation) Summary is how can I insert data into one table only while having one to one mapping. Please share your thoughts.
Difference b/w JPARepository and CRUDRepository is:- JPARepository is the defualt repository interface provided by Spring JPA. CRUDRepository is another interface which implements JPARepository thus providing some additional methods other than those provided by JPARepo.
I tried 3 classes - Class1, Class2, Class3 where Class2 had a @OneToOne to Class1, and Class 3 had a @OneToOne to Class2. First I created/saved an instance of Class1, then created a second Class1 and Class2, linked them and saved. The Ids of this second set were not shared - they were one off. When I switched to have a separate foreign key, it was correct. I'm still trying to get an example of Class1 + Class2 + Class3 all linked up working - it blows up will a null pointer error.
I agree the program does not work as it seems. I was having the same problem. The @JoinColumn(name = "firstId", referencedColumnName = "meters_id") The firstId is not the id of the current class, it is the column you want to add to the table. if i was using the same name with the id of the class the application would not run.
When I am using GerationType.AUTO for id, ids generating in tables are not from 0 but sequence wise. if the first table has last id 5 than the new table will save starting id 6. Please help.
i went for an spring interview where hr ask me a question write entity by which u can enter same data in 2 tables and if u update data it will enter only into the 2nd table.so if new data comes it will enter in same table and onky updated data into 2nd table. how to do that?
yes sir, i use abstract class n extend it 2 entities n then i write 1 repository n set its value to another repository. so i can ene same data into 2 tables
is there any other way to do this other than abstract class? plz told me if u know.. cz i think interviewer expected some other way to do this..☺️ he was asking me about mapping in entity class.. and good news is i got job offer..😃
Whenever I do an entry in db the child object is getting +1 id value to that of its parents. Ex: { "id": 1, "name": "Batman", "details": { "id": 2. // increment value of parent "team": "DC" } } I followed each and every step. What could be wrong?
i have a simple question. there is no dependency for hibernate in your pom.xml. does spring jpa use hibernate for implementation by default. reply plz its really confusing me
can we possible with out relationship.i thanking in repository create custom method and on top of the method add @Query annotations inside pass directly join query without relationship. Is it possible
im getting output like this can you please tell the what will be the error.....o/p-- { "id": 1, "phoneNo": 111111, "value": { "name": "srinivas", "salary": 30000, "teamName": "JBHUNT", "id": 2 } }
Hi There! watching for the first time and i liked it, Nice video! When i try to run my Spring boot app i have no issues inserting the data i DB, but when i try to retrieve the data it only the UserContact gets displayed.. { "id": 1, "phoneNumber": "4699553435", "user": null } my userContact respository does nothing special public List getUserContact() { return userContactRespository.findAll(); } Any suggestions would be appreciated!
I am getting 2 records in both the table after i hit "localhost:8080/rest/usercontact/update/abc" anyone facing the same issue? on the console i am getting : Hibernate: insert into user (name, salary, team_name) values (?, ?, ?) Hibernate: insert into user_contact (contact_num) values (?) Hibernate: insert into user (name, salary, team_name) values (?, ?, ?) Hibernate: insert into user_contact (contact_num) values (?)
I followed the exact steps but for me the ids are not getting mapped. Here is the response: [ { "id": 1, "phoneNo": 1111, "users": { "id": 2, "name": “name”, "teamName": “teamname”, "salary": 1000 } } ]
Your videos are really paced right and are quite detailed. You are awesome :)
simple and clear explanation..keep doing such videos
I think duration b/w 6:55 to 7:55 ,what you said is wrong,actually if you check the hibernate sql log,it's not creating any relation b/w these two tables.Your code is working because you have same primary keys in both the tables, if you change the primary of child table i.e user_contact and have different primary key in Users table(i mean primary key of both the table should not match) in DB and then, try to delete any entries from child table i.e user_contact by passing the Id ,then it will remove the record from user_contact table and other records in users table will not be removed........But as per the @OneToOne(cascade=CascadeType.ALL) ,it should not delete the entries from child table also ....But if you maintain a separate column as a foreign key then it will not let you delete.....so i think what you told is wrong........please correct me if i am wrong....i have source code if you want to contact me then ping me at 9962790783
Exactly , this kind of wrong informative videos make a beginner more confusion,🥴
Thanks for this video, really liked the builder pattern you used.
Very good explanation
hi sir, thanks for your effort to explain one to one mapping,one gentle request from my side,can you show me how to use postMapping for posting data in both table from one controller
I wish you have covered bidirectional mapping too.
please try to send request values from the json (postman). you are setting the values using setter method.
really this is helpful video thanks
Hi Tech Primers my request is how in real time we face issues and how to solve them and how to check logs please make a video of any real time project it will really help me and I will be very thankful full to u brother
BTW, you should not worry about the primary key sequence in the JSON response. Spring JPA/Hibernate will generate id in the sequence and the end of story when spring jpa persist a record you should see your FK and PK values to be same as they are associated using OneToOne mapping.
Query :
SELECT * FROM USERS_CONTACT UC, USERS US
WHERE uc.users_contact_fk = US.ID
Hi Team, how to do one to one mappings if we have multiple primary keys and also we have foreign key reference.
thank you . it was very useful for me and i solve my problem
That helped a lot.. :) Is there any chance u can do the mapping without declaring the users table id in users contact as it already exists in the Users instance which we are declaring in the users contact table...?
Hai Buddy,Can u make a video tutorial for Thymelaf..please..Thanks in advance
sure Vignesh. will do
Can you please describe n+1 problem on this same project?
Ajay , Can we write one to one mapping in users entity instead of users contact model class??
Thanku so much bro u r my guru
Can you write an article about Microsoft graph api integration to outlook in java
Thank You Sir, this is really helpful for me.
Thank you. Glad that was helpful!
i am using crud repositories and lombak and therefore i dont use constructors. does your video and method work in my case?
Please check ur Git hub repo for issue reported with refernce to one-to-one papping
Hello, Im trying to implement lazy fetch type to join 2 tables based on OnetoOne Mapping to improve performance issues. I see the same performance however. Any ideas?
hi brother nice video it is very useful for me but i have done same way but i got exception that is Field 'id' doesn't have a default value
o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'id' doesn't have a default value
Hi Anil, Looks like the PrimaryKey (Id) is not getting auto generated. Can you check if you had added @GeneratedValue annotation which populates the Id field
thanks for updating i solved that problem spring.jpa.hibernate.ddl-auto = update to i changed create now it is working fine
Hi, I am getting null response, when i followed your steps. any thoughts?
Hey,
What if we have data in user table already and now we need to persist data in user contact table which is having a link to user table (same relation)
Summary is how can I insert data into one table only while having one to one mapping.
Please share your thoughts.
Thanks for this video
difference between JPARepository vs CurdRepository please explain .thanks in advance
Difference b/w JPARepository and CRUDRepository is:-
JPARepository is the defualt repository interface provided by Spring JPA. CRUDRepository is another interface which implements JPARepository thus providing some additional methods other than those provided by JPARepo.
grt bhai
Very fucking useful!! thank you
I tried 3 classes - Class1, Class2, Class3 where Class2 had a @OneToOne to Class1, and Class 3 had a @OneToOne to Class2. First I created/saved an instance of Class1, then created a second Class1 and Class2, linked them and saved. The Ids of this second set were not shared - they were one off. When I switched to have a separate foreign key, it was correct. I'm still trying to get an example of Class1 + Class2 + Class3 all linked up working - it blows up will a null pointer error.
I think the problem is at 7:56 of the video. You have 2 instructions for the id - auto-generate it and map it. it cant be both.
I agree the program does not work as it seems. I was having the same problem. The @JoinColumn(name = "firstId", referencedColumnName = "meters_id")
The firstId is not the id of the current class, it is the column you want to add to the table. if i was using the same name with the id of the class the application would not run.
That's the problem with JPA and Hibernate. If the database has multiple relationships with multiple tables, it sucks. Spring JDBC is the best option.
When I am using GerationType.AUTO for id, ids generating in tables are not from 0 but sequence wise. if the first table has last id 5 than the new table will save starting id 6. Please help.
Hi Brother, I am a good follower of all your tutorials, nice work.
Could you please make a video on
Spring data jpa with Cassandra database.
Thanks.
sure will do
i went for an spring interview where hr ask me a question
write entity by which u can enter same data in 2 tables and if u update data it will enter only into the 2nd table.so if new data comes it will enter in same table and onky updated data into 2nd table.
how to do that?
You can google it out. I have seen solution in stackoverflow
yes sir, i use abstract class n extend it 2 entities n then i write 1 repository n set its value to another repository. so i can ene same data into 2 tables
im fresher interview told me if u solve this task i will give u job offer.. i hope i will get job 😃
is there any other way to do this other than abstract class? plz told me if u know.. cz i think interviewer expected some other way to do this..☺️ he was asking me about mapping in entity class..
and good news is i got job offer..😃
how can we do it by post method using JSON?
Why files in your github repo are removed? I cannot find updated Users.java and UserContact.java.
Hi Tech Primers, I Follow Your Videos, They are Really Helpful,
CAN YOU DO A POST METHOD Using JSON,
Thanks In Advance
sure Mohammed. Will do it tomorrow.
Is It Possible For You to Do it for OnetoMany and ManyToMany ALSO,
Thank You So Much
Thank You For The Informative Videos TECH PRIMERS, Waiting for POST METHOD using Json
Hi Mohammed, I have done both already. Check out the "Hibernate playlist", also i have created the POST method video. Take a look
POST video is done. here you go: ruclips.net/video/nhH1PQF1698/видео.html
Whenever I do an entry in db the child object is getting +1 id value to that of its parents. Ex:
{
"id": 1,
"name": "Batman",
"details": {
"id": 2. // increment value of parent
"team": "DC"
}
}
I followed each and every step. What could be wrong?
I am also facing exactly same issue.... :(
You can fix it by providing GenerationType.IDENTITY for ID.
@@SubhSingh i got the same issue but your solution solved it,. Thank you very much
Okay I get that using get to make this simple tut
i have a simple question. there is no dependency for hibernate in your pom.xml. does spring jpa use hibernate for implementation by default. reply plz its really confusing me
yes
how to fetch the data from multiples tables and how perform joins in spring data jpa
please replay any body
you can use @OneToOne, @OneToMany or @ManyToOne mappings via jpa
can we possible with out relationship.i thanking in repository create custom method and on top of the method add @Query annotations inside pass directly join query without relationship. Is it possible
im getting output like this can you please tell the what will be the error.....o/p-- {
"id": 1,
"phoneNo": 111111,
"value": {
"name": "srinivas",
"salary": 30000,
"teamName": "JBHUNT",
"id": 2
}
}
It's not working, throws null pointer exception.
hi bro manytomany relationship video link share me.
can you sent me previous video
Hi There! watching for the first time and i liked it, Nice video!
When i try to run my Spring boot app i have no issues inserting the data i DB, but when i try to retrieve the data it only the UserContact gets displayed..
{
"id": 1,
"phoneNumber": "4699553435",
"user": null
}
my userContact respository does nothing special
public List getUserContact() {
return userContactRespository.findAll();
}
Any suggestions would be appreciated!
Can u check if your Enity with user object does EAGER loading rather than lazy loading?
Thanks bud! I was using one to one mapping and i believe its eager by default, any ways i resolved the issue! Thank you! Subscribed :)
+Pranav Patil cool
I am getting 2 records in both the table after i hit "localhost:8080/rest/usercontact/update/abc" anyone facing the same issue?
on the console i am getting :
Hibernate: insert into user (name, salary, team_name) values (?, ?, ?)
Hibernate: insert into user_contact (contact_num) values (?)
Hibernate: insert into user (name, salary, team_name) values (?, ?, ?)
Hibernate: insert into user_contact (contact_num) values (?)
I followed the exact steps but for me the ids are not getting mapped. Here is the response:
[
{
"id": 1,
"phoneNo": 1111,
"users": {
"id": 2,
"name": “name”,
"teamName": “teamname”,
"salary": 1000
}
}
]
hello nirmal mohanty..i'm also getting same output did you find the soultion for that!
@@7mssrinivas No, not yet.
@@nirmalmohanty7852 ok but i got it... Change the autogeneration to identify... It is in auto so once change it check it its working for me..