SEARCH BY

All

  • All
  • Java8
  • Spring

JPA Entity class complete guide

Post a Comment

Introduction

In this article/tutorial we will see complete guide on JPA Entity class. JPA Entity class is a POJO class to map database table with java object nature which is called as ORM. Entity class is different than normal java class. Entity class is the object based representation of database table. 

Rules to create Entity class

Following are the rules to follow while creating Entity class

We must use @Entity annotation to create Entity class

Entity class must have public or protected zero-arguments constructor, entity class may contains other constructors

Entity class can not be final class and entity class must not have final methods and fields

Entity class can extend another entity class and non entity classes too

Any non entity class can extends entity class

To use Entity class object as a parameter to any method, Entity class must be serialised

Entity class fields or properties of entity class

Now we will see what are type of fields can be created in Entity class.
  • Java primitive types

  • java.lang.String

  • Other serializable types, including:

    • Wrappers of Java primitive types

    • java.math.BigInteger

    • java.math.BigDecimal

    • java.util.Date

    • java.util.Calendar

    • java.sql.Date

    • java.sql.Time

    • java.sql.TimeStamp

    • User-defined serializable types

    • byte[]

    • Byte[]

    • char[]

    • Character[]

  • Enumerated types

  • Other entities and/or collections of entities

  • Embeddable classes

Getter and setter methods of entity class

Every property of entity class must have getter and setter methods to get the value of property and to set the value of property. Setting value for property will lead to change the state of object.

States of Entity class objects

There are two states of entity class objects one is transient state and another one is persistent state. If we create an object for entity class with new operator then object is in transient state. If object is stored in DB or retrieved from DB then then state of entity object is persistent object.

Using Collection classes as a property in entity class

We can use List,Set and Map collections as a properties of entity class. But we can use these collection classes with mapping, collection of embeddable entities only.


jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox