Primary key of an entity class

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

Primary key of an entity class

Rating:

A primary key is an identity in an entity class. Every entity bean must have a primary key that is unique for a class. A primary key can be of the following types:

  • Java primitive type

  • Primitive wrapper type

  • java.lang.String

  • java.util.Date

  • java.sql.Date
A primary key has the following properties:
  • Its value cannot be null.

  • It must be unique.

  • It must not change throughout the lifetime of the instance.

  • It must be accessible to all the users of the database.
The requirements for writing a primary key class are as follows:
  • The class must be serializable.

  • The access modifier of the class must be public.

  • If property-based access is used, the properties of the primary key class must be public or protected.

  • The class must have a public no argument constructor.

  • The class must implement the hashCode() and equals() methods.

  • In case of a composite primary key, the identity must be represented and mapped to multiple fields or properties of the entity class, or an embeddable class must be used.

  • The class must contain fields or properties that match the primary key attributes in the entity in name and type.
A primary key can be of the following two types:
  • Simple primary key: It is composed of a single persistent field or property.

  • Composite primary key: It is composed of multiple persistent properties.
A simple primary key is represented using the @Id annotation. The @Id annotation identifies a property or a set of properties that make up a primary key of a table. The @Id annotation is used to identify a property or a set of properties that make up a primary key of a table. It can be applied to a field or a method. The @Id annotation marks a field or a property as identity for an entity. The identity of an entity can be of the following types:
  • Java Primitives

  • Primitive wrappers

  • Serializable types such as java.lang.String

  • java.util.Date

  • java.sql.Date
Approximate numeric types such as float, Float, double, etc., cannot be used as an identity because they can lead to violation of primary key rules. For example, if two double identities are used as 120.112 and 120.115, the database can round off these values to the closest integer, i.e., 120 or to a precision up to two decimal places, i.e., 120.11. In both the cases, the value will not be unique. This is a violation of primary key.

The @IdClass annotation is used to define a primary key class. Each field of the entity that makes up the primary key must be marked with the @Id annotation. An id class or a primary key class can also be represented in the xml descriptor using the <id-class> element. An example of using the @IdClass annotation is as follows:

@Entity
@IdClass(StudentId.class)
public class Student {
   private String name;
   @Id private String standard;
   @Id
   public String getName(){
      return name;
   }
   public void setName(String name){
      this.fname=name;
   }
}


The @EmbeddableId annotation is used to represent a primary class that is embedded in a bean class. It is used with the @Embeddable annotation to embed the primary key class into the bean class. An example of using the @EmbeddableId annotation is as follows:

@Embeddable
public class StudentId {
   private String standard;
   @Column(name="name")
   private int id;
   public StudentId() {}
   public StudentId(String name, int id) {
      this.name = name;
      this.id = id;
   }
   //More code here
}

@Entity
public class StudentInfo {
   public StudentInfo() {}
   @EmbeddedId
   protected StudentId studentId;
   //More code here
}

The @Id and the @IdClass annotations cannot be used with the @EmbeddableId annotation.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.