@Id

From CauchoWiki

Jump to: navigation, search


The @Id annotation marks a field as the entity's primary key. Each entity must have at least one primary key.

 @Id
 public int getId() { return _id; }

Contents

[edit] allowed types

An @Id field may use:

  • any Java primitive (byte, int, long, char, etc.)
  • primitive wrappers (Byte, Integer, Long, Character, etc.)
  • String
  • java.util.Date
  • java.sql.Date

In general, it is recommended to use int or long.

[edit] definition

 public @interface Id {
 }

[edit] optional attributes

[edit] Example: Automatic Generation

import javax.persistence.*;

@Entity
public class Course {
     @Id
     @Column(name="t_id")
     @GeneratedValue
     public long getId()

     ...
}

[edit] Example: Sequence Generation

import javax.persistence.*;

@Entity
public class Course {
     @Id
     @GeneratedValue(strategy=GeneratorType.AUTO
                     generator="COURSE_SEQ")
     public long getId()

     ...
}
Personal tools