Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

A brief insight into Java Persistence API

In this article you will see some highlights of the significant attributes that make things easier for the Java Persistence API and its use in applications for the Java Platform.

The Java Persistence API offers an object/relational mapping option to Java developers for handling relational data in Java applications. Java Persistence compromises three areas in it:

  • The Java Persistence API
  • the query language
  • Object/relational mapping metadata

Entities

An entity is a trivial persistence area entity. Classically an entity symbolizes a table in a relational database, and each entity example match up to a row in that table. The main programming work of art of a unit is the entity class, though entities can make use of assistant classes. The persistent condition of an entity stands for moreover from end to end persistent fields or persistent possessions. These fields or properties make use of relational mapping comments to map the entities and entity associations to the relational data in the original data stock up.

Necessities for Entity Classes

An entity class is obliged to pursue these necessities:

  • The class have got to be explain with the javax.persistence.Entity explanation.
  • The class be required to have a open or confined, no-argument constructor. The class may have other constructors.
  • The class must not be stated final. No procedure or persistent instance variable have got to be affirmed closing.
  • If an entity case in tip be passed by value as a separate object, like all the way through a session bean’s remote business boundary, the class must put into practice the Serializable interface.

Entities may expand both entity and non-entity classes, and non-entity classes may enlarge entity classes.

  • Persistent instance variables must be affirmed confidential, confined, or package-private, and can only be accessed openly by the entity class’s methods. Clients have got to access the entity’s state during accessory or business process.

Persistent Fields and Properties in Entity Classes

The constant state of an entity can be viewed whichever all the way through the entity’s instance variables or via JavaBeans-style attributes. The fields or attributes be obliged to be of the subsequent Java language sorts:

  • 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

Entities may either deploy persistent fields or persistent attributes. In case the mapping explanations are put into practice to the getter methods of entity, persistent attributes are used by the entities. One cannot deploy mapping explanations to both fields and attributes in altogether a single entity.

Fields associated with Persistent

In case the persistent attributes or fields are used by the entity class, the runtime of persistence makes use of the variables in a direct manner. The fields that are not marked as javax.persistence.Transient will make its route direct to the data store.

Attributes of persistent

The cases where persistent features are used by the entity, the procedure conventions of JavaBeans elements must be followed. These attributes make use of getter-setter techniques that are usually named on entity class’s example or illustration variable names. For each of the property of persistent that has a type “Entity” we have a method known by the name of getproperty and is a getter method and on the other hand, we have a setter method as setProperty.

In case the Boolean property is there, isProperty can be made use of and not the getProperty. Illustration of the same is that in case the Customer entity deploys the properties of persistence with its own connfidentails or private instance variable known as firstName, a method known by the name of getFirstName and setFirstName is defined by the class so as to extract and placing the state of the variable known as firstName.

Entities Primary Keys:

Each of the entity is gifted with its own and unique object identifier. For instance, you may recognize a customer by the entity known as customer number. This unique identifier assists the clients in identifying a specific entity instance. Each of the entity is supposed to possess a primary key. If we talk about the primary keys, they make use of the annotation as javax.persistence.Id so as to resemble the property of the primary key whereas the Composite primary keys should direct to a single persistent attribute or the other option will be to a group of persistence attributes or properties.

Primary Key Classes

A primary key class has got to convene these necessities:

  • The access manage modifier of the class must be unrestricted or open.
  • The attributes of the primary key class be obliged to be open or confined in case attribute-based access is used.
  • The class be required to put into practice the hashCode() and equals(Object other) techniques.
  • The class ought to be serializable.
  • A composite primary key must be symbolized and mapped to numerous fields or features of the entity class.

The subsequent primary key class is a composite key, the orderId and itemId fields jointly exclusively recognize an entity.

Listing 1: Entities

public final class LineItemKey implements Serializable {
public Integer orderId;
public int itemId;
public LineItemKey() {}
public LineItemKey(Integer orderId, int itemId) {
this.orderId = orderId;
this.itemId = itemId;
}
public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (!(otherOb instanceof LineItemKey)) {
return false;
}
LineItemKey other = (LineItemKey) otherOb;
return (
(orderId==null?other.orderId==null:orderId.equals
(other.orderId)
)
&&
(itemId == other.itemId)
);
}
public int hashCode() {
return (
(orderId==null?0:orderId.hashCode())
^
((int) itemId)
);
}
public String toString() {
return "" + orderId + "-" + itemId;
}
}


I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login