Hibernate – Tutorials & Examples

Hibernate – Tutorials & Examples

Hibernate All Annotations – Tutorials & Examples

@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.

Eg:


@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.

[code]
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type=TRACE

[/code]

 

 

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).

862 total views, 4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.