Hibernate – Tutorials & Examples
Table of Contents
Hibernate Annotations
@Embeddable:
This annotation can be used for the joining tables, by default it is not even necessary with hibernate, but if your intermediate joining table needs any other addtional columns to be added, then you must have @Embeddable annotated class.
Do remember, in spring boot projects, we used to create repository for all the entity files, so even though we have embedded class separately, we should not create repository classes for this.
@Entity:
This is the main annotation to link your class with your tables.
@Entity – Hibernate annoation example
@Entity public class Coupon extends BaseEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "coupon_id") private Long couponId; private String couponTitle; @Column(columnDefinition = "varchar(3500)") private String couponDesc; // setters & getters for all the fields }
- coupon table will be mapped to this entity class
- And maps all the three variables mentioned here to the respective columns in the coupon table.
- if name attribute used inside @Column then the same name should be matched with your table column name.
For couponTitle no @column annotation and no name attribute, so by default, inside your entity class even if you don’t mention the @column it considers all the fields as columns and if the name attribute is not mentioned then it takes the default name, in this case coupon_title (as couponTitle) mentioned in camelcase.
How to see the actual values of the hibernate queries ?
Add the below entries to your spring boot application.properties file to see the actual values of the hibernate sql queries.
logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE logging.level.org.hibernate.type=TRACE
How to resolve “object references an unsaved transient instance – save the transient instance before flushing”?
In your hibernate savings, for any object if you have kept new Employee() (always in onetomany/manytoone mappings, we must give the database saved values only to that reference, we must not give new object() in those areas).