Sunday, 14 April 2019

Name some important annotations used for Hibernate mapping?


Hibernate supports JPA annotations and it has some other annotations in org.hibernate.annotations package. Some of the important JPA and hibernate annotations used are:

javax.persistence.Entity: Used with model classes to specify that they are entity beans.
javax.persistence.Table: Used with entity beans to define the corresponding table name in database.
javax.persistence.Access: Used to define the access type, either field or property. Default value is field and if you want hibernate to use getter/setter methods then you need to set it to property.
javax.persistence.Id: Used to define the primary key in the entity bean.
javax.persistence.EmbeddedId: Used to define composite primary key in the entity bean.
javax.persistence.Column: Used to define the column name in database table.
javax.persistence.GeneratedValue: Used to define the strategy to be used for generation of primary key. Used in conjunction with javax.persistence.GenerationType enum.
javax.persistence.OneToOne: Used to define the one-to-one mapping between two entity beans. We have other similar annotations as OneToMany, ManyToOne and ManyToMany
org.hibernate.annotations.Cascade: Used to define the cascading between two entity beans, used with mappings. It works in conjunction with org.hibernate.annotations.CascadeType
javax.persistence.PrimaryKeyJoinColumn: Used to define the property for foreign key. Used with org.hibernate.annotations.GenericGenerator and org.hibernate.annotations.Parameter


Here are two classes showing usage of these annotations.

  1. import javax.persistence.Access;  
  2.   
  3. import javax.persistence.AccessType;  
  4.   
  5. import javax.persistence.Column;  
  6.   
  7. import javax.persistence.Entity;  
  8.   
  9. import javax.persistence.GeneratedValue;  
  10.   
  11. import javax.persistence.GenerationType;  
  12.   
  13. import javax.persistence.Id;  
  14.   
  15. import javax.persistence.OneToOne;  
  16.   
  17. import javax.persistence.Table;  
  18.   
  19.   
  20.   
  21. import org.hibernate.annotations.Cascade;  
  22.   
  23.   
  24.   
  25. @Entity  
  26.   
  27. @Table(name = "EMPLOYEE")  
  28.   
  29. @Access(value=AccessType.FIELD)  
  30.   
  31. public class Employee {  
  32.   
  33.   
  34.   
  35.     @Id  
  36.   
  37.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  38.   
  39.     @Column(name = "emp_id")  
  40.   
  41.     private long id;  
  42.   
  43.   
  44.   
  45.     @Column(name = "emp_name")  
  46.   
  47.     private String name;  
  48.   
  49.   
  50.   
  51.     @OneToOne(mappedBy = "employee")  
  52.   
  53.     @Cascade(value = org.hibernate.annotations.CascadeType.ALL)  
  54.   
  55.     private Address address;  
  56.   
  57.   
  58.   
  59.     //getter setter methods  
  60.   
  61. }  



  1. import javax.persistence.Access;  
  2.   
  3. import javax.persistence.AccessType;  
  4.   
  5. import javax.persistence.Column;  
  6.   
  7. import javax.persistence.Entity;  
  8.   
  9. import javax.persistence.GeneratedValue;  
  10.   
  11. import javax.persistence.Id;  
  12.   
  13. import javax.persistence.OneToOne;  
  14.   
  15. import javax.persistence.PrimaryKeyJoinColumn;  
  16.   
  17. import javax.persistence.Table;  
  18.   
  19.   
  20.   
  21. import org.hibernate.annotations.GenericGenerator;  
  22.   
  23. import org.hibernate.annotations.Parameter;  
  24.   
  25.   
  26.   
  27. @Entity  
  28.   
  29. @Table(name = "ADDRESS")  
  30.   
  31. @Access(value=AccessType.FIELD)  
  32.   
  33. public class Address {  
  34.   
  35.   
  36.   
  37.     @Id  
  38.   
  39.     @Column(name = "emp_id", unique = true, nullable = false)  
  40.   
  41.     @GeneratedValue(generator = "gen")  
  42.   
  43.     @GenericGenerator(name = "gen", strategy = "foreign", parameters = { @Parameter(name = "property", value = "employee") })  
  44.   
  45.     private long id;  
  46.   
  47.   
  48.   
  49.     @Column(name = "address_line1")  
  50.   
  51.     private String addressLine1;  
  52.   
  53.   
  54.   
  55.     @OneToOne  
  56.   
  57.     @PrimaryKeyJoinColumn  
  58.   
  59.     private Employee employee;  
  60.   
  61.   
  62.   
  63.     //getter setter methods  
  64.   
  65. }  

No comments:

Post a Comment

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...