Hibernate Unidirectional Mapping short note

Here is my Hibernate/JPA unidirectional mapping short note.


One To One


One To One unidirectional mapping





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

Many To One


Many To One unidirectional mapping






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
address_id city


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

One To Many


One To Many unidirectional mapping





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
emp_id emp_name

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

Many To Many


Many To Many unidirectional mapping






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
emp_id emp_name

patent
patent_id description

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

Reference - SCBCD 5.0 Study Guide by Mikalai Zaikin

18 comments:

  1. Thanks for the explanation. It’s really helpful .Thanks you. Please keep sharing
    Hibernate training in Delhi

    ReplyDelete
  2. Nice post .Really appreciable. Please share more information. Thanks you
    Hibernate Institute in Noida

    ReplyDelete
  3. Nice blog. Thank you for sharing such useful post. Keep posting
    Hibernate Course in Gurgaon

    ReplyDelete
  4. 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.
    iphone job training center in bangalore
    best iphone training institute bangalore

    ReplyDelete
  5. 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

    ReplyDelete
  6. Nice Information
    We 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

    ReplyDelete
  7. 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...
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  8. 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. 
    Artificial Intelligence Training
    Java Training
    AWS Training
    Machine Learning Training
    Data Science Training
    DevOps Training

    ReplyDelete
  9. 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.
    Salesforce 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

    ReplyDelete
  10. 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.

    https://www.ahmedabadcomputereducation.com/course/laravel-training-course/

    ReplyDelete
  11. Awesome content for reading as well as knowledge. Thank you for sharing this informative article with us.

    https://www.sdsfin.in/about-us/

    ReplyDelete
  12. Thank you for sharing this informative article.

    https://web30india.com/

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete