Entities

  • Entities act as Wrapper classes to our database tables
  • Create a sub package under current package and name it as Entities.
  • Create a new class with Entity Name
    • This is basically the name of your Table such as Person,Department etc
  • Annotate the class with @Entity from javax.persistance.
  • Annotate with @Table annotation to add table name.
    • This annotation is not mandatory(by default creates a table with class name)
  • Create Primary key data variable and annotate with @ID,@Column(name="column name")
  • Add more data members which will act as columns of database.
    • Annotate these with @Column(name="columnname")
  • Generate getters and setters.
  • Add empty constructor and non empty constructor.
  • Add Data Access Object(DAO) classes or Repository Classes for entities.
    • We can create a separate package for these.
    • Add Session Data variable from Org.Hibernate.Session.
    • Add a Constructor which takes Session Factory as parameter.
    • Open session in this constructor using Session Factory and assign to data variable Session.
    • All the CRUD operations performed will be a part of this class.

No comments:

Post a Comment

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...