Here is my Hibernate/JPA unidirectional mapping short note.
One To One
Employee |
TravelProfile |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="travel_profile_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="destination")
private String destination; |
|
|
@OneToOne
TravelProfile travelProfile; |
|
Database tables created by Hibernate
employee
emp_id |
emp_name |
TRAVELPROFILE_travel_profile_id |
|
|
|
travel_profile
travel_profile_id |
destination |
|
|
If you want to change
TRAVELPROFILE_travel_profile_id column name to
travel_pro_id add @JoinColumn annotation.
@OneToOne
@JoinColumn(name="travel_pro_id")
TravelProfile travelProfile;
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@OneToOne
//@JoinColumn(name="travel_pro_id")
TravelProfile travelProfile;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TravelProfile getTravelProfile() {
return travelProfile;
}
public void setTravelProfile(TravelProfile travelProfile) {
this.travelProfile = travelProfile;
}
}
TravelProfile.java
package com.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="tavel_profile")
public class TravelProfile implements Serializable {
@Id
@Column(name = "travel_profile_id")
private Long id;
@Column(name = "destination")
private String destination;
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Many To One
Employee |
Address |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="address_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="city")
private String city; |
|
|
@ManyToOne
Address address; |
|
Database tables created by Hibernate
employee
emp_id |
emp_name |
ADDRESS_address_id |
|
|
|
address
If you want to change
ADDRESS_address_id column name to
address_id add @JoinColumn annotation.
@ManyToOne
@JoinColumn(name="address_id")
Address address;
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
@Entity
@Table(name="employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@ManyToOne
//@JoinColumn(name="address_id")
Address address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="address")
public class Address implements Serializable {
@Id
@Column(name = "address_id")
private Long id;
@Column(name = "city")
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
One To Many
Employee |
AnnualReview |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="annual_review_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="description")
private String description; |
|
|
@OneToMany
List <AnnualReview> annualReviews; |
|
Database tables created by Hibernate
employee
annual_review
annual_review_id |
description |
|
|
employee_annual_review
Employee_emp_id |
annualReviews_annual_review_id |
|
|
If you want to change
employee_annual_review table name to
emp_annual_review add @JoinTable annotation.
@OneToMany
@JoinTable(name="emp_annual_review")
List<AnnualReview> annualReviews;
If you want to change
Employee_emp_id,
annualReviews_annual_review_id columns to
emp_id,
annual_review_id use following code
@OneToMany
@JoinTable(name="emp_annual_review",
joinColumns=@JoinColumn(name="emp_id"),
inverseJoinColumns=@JoinColumn(name="annual_review_id")
)
List<AnnualReview> annualReviews;
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
@Entity
@Table(name="employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@OneToMany
/*@JoinTable(name="emp_annual_review",
joinColumns=@JoinColumn(name="emp_id"),
inverseJoinColumns=@JoinColumn(name="annual_review_id")
)*/
List<AnnualReview> annualReviews;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<AnnualReview> getAnnualReviews() {
return annualReviews;
}
public void setAnnualReviews(List<AnnualReview> annualReviews) {
this.annualReviews = annualReviews;
}
}
AnnualReview.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="annual_review")
public class AnnualReview implements Serializable {
@Id
@Column(name = "annual_review_id")
private Long id;
@Column(name = "description")
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Many To Many
Employee |
Patent |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="patent_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="description")
private String description; |
|
|
@ManyToMany
List<Patent> patents; |
|
Database tables created by Hibernate
employee
patent
employee_patent
Employee_emp_id |
patents_patent_id |
|
|
If you want to change
employee_patent
table name to
emp_patent add @JoinTable annotation.
@ManyToMany
@JoinTable(name="emp_patent")
List<Patent> patents;
If you want to change
Employee_emp_id,
patents_patent_id columns to
emp_id,
patent_id use following code.
@ManyToMany
@JoinTable(name="emp_patent",
joinColumns=@JoinColumn(name="emp_id"),
inverseJoinColumns=@JoinColumn(name="patent_id")
)
List<Patent> patents;
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
@Entity
@Table(name="employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@ManyToMany
//@JoinTable(name="emp_patent",
// joinColumns=@JoinColumn(name="emp_id"),
// inverseJoinColumns=@JoinColumn(name="patent_id")
//)
List<Patent> patents;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Patent> getPatents() {
return patents;
}
public void setPatents(List<Patent> patents) {
this.patents = patents;
}
}
Patent.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="patent")
public class Patent implements Serializable {
@Id
@Column(name = "patent_id")
private Long id;
@Column(name = "description")
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Reference - SCBCD 5.0 Study Guide by Mikalai Zaikin
Thanks for the explanation. It’s really helpful .Thanks you. Please keep sharing
ReplyDeleteHibernate training in Delhi
Nice post .Really appreciable. Please share more information. Thanks you
ReplyDeleteHibernate Institute in Noida
Nice blog. Thank you for sharing such useful post. Keep posting
ReplyDeleteHibernate Course in Gurgaon
Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
ReplyDeleteiphone job training center in bangalore
best iphone training institute bangalore
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city
blockchain Training in electronic city
Hibernate Training in electronic city
Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.Advanced Java Online Course
ReplyDeleteNice Information
ReplyDeleteWe are the best piping design course in Hyderabad, India. Sanjary academy Offers Piping Design Course and Best Piping Design Training Institute in Hyderabad. Piping Design Institute in India Piping Design Engineering.
Piping Design Course
Piping Design Course in india
Piping Design Course in hyderabad
Thanks for all your information's about Courses and Placements...It's Really used for enhance the Careers...Here The information's about Best Java Training with Placements Visit here for more...
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
ReplyDeleteArtificial Intelligence Training
Java Training
AWS Training
Machine Learning Training
Data Science Training
DevOps Training
I was very interested in the article , it’s quite inspiring I should admit. I like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards.
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
Thank you for sharing this useful article with us. This blog is a very helpful to me in future. Keep sharing informative articles with us.
ReplyDeletehttps://www.ahmedabadcomputereducation.com/course/laravel-training-course/
Awesome content for reading as well as knowledge. Thank you for sharing this informative article with us.
ReplyDeletehttps://www.sdsfin.in/about-us/
Thanks For sharing
ReplyDeletefull stack training in delhi
AI training course in Delhi
Power BI Institute in Delhi
tableau training in delhi
SASVBA
GMB
For more information
Thank you for sharing this informative article.
ReplyDeletehttps://web30india.com/
Android Mobile App Development in Ahmedabad
ReplyDeleteWeb 3.0 India is the most trusted Web3 Blockchain Development and NFT Development Company in USA. We also provides ICO, STO development for cryptocurrency.
https://web30india.com/
Beamaged
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThank you for this knowledgeable article.
ReplyDeleteBlockchain Development Company in Ahmedabad
Web3 Development Company
Mobile Application Development
Game Development Company