Saturday, February 3, 2018

Entities In Java Persistence API

Entities In Java Persistence :

An entities is a lightweight persistence domain object.Typically an entity represents a table in relational database, and each entity instance corresponds to a row in that tables.

The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store.


Requirements for Entity Classes:-


An entity class must follow these requirements:
1.The class must be annotated with the javax.persistence.Entity annotation.
2.The class must have a public or protected ,no-argument constructor. The class may have other constructors.
3.The class must not be declared final. No method or persistent instance variable must be declared final.
4.Entities may extend both entity and non-entity classes,and non-entity classes may extend entity classes.
5.Persistent instance variables must be declared private, protected, or package-private, and can only be accessed directly by the entity class’s methods. Clients must access the entity’s state through accessor or business methods.
   
 
Persistent Fields and Properties in Entity Classes:- 

The persistent state of an entity can be accessed either through the entity's   instance variable or through JavaBeans-style properties. The fields or properties must be the following java language types:

 1. Java primitive types.
 2. 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[]
  *Bytes[]
  *char[]
  *character[]

Enumerated types
Other entities and collections of entities.
Embeddable classes

Entities may either use persistent fields or persistent properties. If the mapping annotations are applied to the entity’s instance variables,
the entity uses persistent fields.

Persistent Fields:-


If the entity class uses persistent fields, the Persistence runtime accesses entity class instance variables directly.
all fields not annotated javax.persistence.Transient or not marked as java transient will be persisted to data store. The Object/relation mapping annotation
must be applied to the instance variables.


Persistent Properties:-

If the entity class use persistent properties, the entity must follow the method conventions of JavaBeans components. JavaBeans-style properties use getter and setter
methods that are typically named after the entity class's instance variable names. For every persistent property property of type Type of the entity, there is a getter method getProperty and setter method setProperty.

The method signature for single -valued persistent properties as follow:-

Type getProperty()
void setProperty(Type type)

Collection-valued persistent fields and properties must use the supported Java collection interfaces regardless of whether the entity uses persistent fields or properties.
The following collection interfaces may be used:

  *java.util.Collection
  *java.util.Set
  *java.util.List
  *java.util.Map
 
Primary Keys in Entities:-
 
Each object has a unique identifier. A customer entity, for example , might be identified by a customer number.
The unique identifier, or primary key, enables clients to locate a particular entity instance.
Every entity must have a primary key. An entity may have either a simple or a composite primary key.

The primary key, or the property or field of a composite primary key, must be one of the following Java language types:

    Java primitive types

    Java primitive wrapper types

    java.lang.String

    java.util.Date (the temporal type should be DATE)

    java.sql.Date

Floating point types should never be used in primary keys. If you use a generated primary key, only integral types will be portable.

Primary Key Clasees:-

A primary key class must these requirements:
  *The access control modifier of the class must be public.
  *The properties of the primary key class must be public or protected if property based access is used.
  *The class must have a public default constructor.
  *The class must be serializable

Multiplicity in Entity Relationships:-


 There are four types of multiplicities :
  1. one-to-one
  2. one-to-many
  3. many-to-one
  4. many-to-many

1. One-To-One:- Each entity instance is related to a single instance of another entity. For example, to model a physical warehouse in which each storage bin conatins a single widget.
2. One-To-Many:- An entity instance can be related to multiple instance of the other entities. A sales order, for example,can have multiple line items. In the order application,order would have a one-to-many relationship with line item.
3. Many-To-one:- Multiple instance of an entity can be related to a single instance of the other entity. this Multiplicity is the opposite of a one-to-many relationship.
4. Many-To-Many:-The antity instance can be related to multiple instance of each other. For example, in college each course has many students ,a nd every may take several courses.therefore in an enrollment application, Course and Student would have a many-to-many relationship.

Entity Inheritence:-
 
 Entities support class inheritence, polymorphic associations, and polymorphic queries. They can extend non-entity classes, and non-entity classes can extend entity classes. ENtity classes can be both abstract and concrete.

Abstract Entities:-


 An abstract class may be declared an entity by decorating the class with @Entity. Abstract entities differ from
 concrete entities only in that they cannot be instantiated.

 Abstract entities can be queried just like concrete queries. If an abstract entity is the target of a query, the query operates on all the concrete subclasses of the abstract entity.

@Entity
public abstract class Employee {
    @Id
    protected Integer employeeId;
    ...
}
@Entity
public class FullTimeEmployee extends Employee {
    protected Integer salary;
    ...
}
@Entity
public class PartTimeEmployee extends Employee {
    protected Float hourlyWage;
}



 

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...