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

5 comments:

  1. Welcome Post very usefull informatation.and iam expecting more posts like this please keep updating us........

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

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

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

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