Hibernate property setting summary.

1. hbm2ddl.auto property

it will have four values

  "Create":

     Every time you run your hibernate application to insert, hibernate will drop off existing table that has same schema, at the same time, we will lose our data.

  "Create-Drop"

     When you close your sessionFactory explicitly, the hibernate application will drop the table. Other than that, when you don't close sessionFactory,

     It will behave the same as "Create".  

  "Update"

      Everytime you run your application, hibernate will just update the schema, the data will remain the same.

    But remember, not to use this value in PROD.

  "Validate"

    Everytime you run your application, it will just validate the existing database schema with the one which can be generated with the annotated model classes.

    It will not make an changes or updates.

    If there is no differences, it will execute without any issues, but if there are differences, the application will throw an runtime exception

    But remember, not to use this value in PROD.

 

2. Most Common annotations in Hibernate

  @Column:

      By default, hibernate will use the model class variable names  to generate and interact with the database.

      if we hope to use other names to be considered as the column name to be created in DB. we could use this annotation.

    for example

      @Column (name = "Full_Name") 

      private String name ;

      and If we hope this value to be not null in data base we could use below value

      @Column(name = "Full_Name" , nullable = false)

   @Transient

      If we hope to ignore certain variable in the model class and not insert it into the database,

      we could use this annotation. 

   @Temporal

      Default behaviour,

     Hibernate will by default map date type in java to datetime in Database,

     what if we don't want datetime in database and just store the normal date (YYYY-MM-DD).

       In this way, we could use @Temporal annotation.   

3. Primary key Auto Generation in Hibernate 

  @GeneratedValue

    In this case, we don't need to give the value, but let the hibernate itself to generate the primary key.

 

  

posted @ 2019-05-17 10:23  CodingYM  阅读(78)  评论(0编辑  收藏  举报