@Id annotation

Each Entity entity in Hibernate must have a field that will be the primary key: it contains a unique value for all objects of this class. Typically, this field is annotated with the @Id annotation .

General form:

@Id
Class Name;

Example:

@Entity
@Table(name="user")
class User
{
   @Id
   @Column(name="id")
   public Integer id;

   @Embedded
   public UserAddress address;

   @Column(name="created_date")
   public Date createdDate;
}

Annotation placement

By the way, you can write @Column annotations not only for fields, but also for methods: for getters or setters. Example:

@Entity
@Table(name="user")
class User
{
    public Integer id;
    public Date createdDate;

   @Id
   @Column(name="id")
   public Integer getId() {
   	return this.id;
   }
   public void setId(Integer id)    {
  	this.id = id;
   }

   @Column(name="created_date")
   public Date getDate() {
  	return this.createdDate;
   }
   public void setCreatedDate(Date date) {
      this. createdDate = date;
   }
}

This approach appeared when annotations began to be added not only to classes, but also to interfaces. The interface does not have class fields, but there are methods: getters and setters. Hibernate supports both of these standards.

Important! If a class has the @Entity annotation , then all of its fields are treated by Hibernate as persistent fields (unless they have the @Transient annotation specified ). Even if the fields do not have any annotations at all: in this case, the column name is considered equal to the class field name.

This is where the @Id annotation plays an important role. If the annotation is placed on a class field, then Hibernate will look at the names and types of the fields. If the @Id annotation is placed on a method, then Hibernate will look at the names and types of the methods.

Example 1:

@Entity
@Table(name="user")
class User
{
   @Id
    public Integer id;
    public Date createdDate;  //this field will be treated as if it had @Column(name=" createdDate ")

}

Example 2:

@Entity
@Table(name="user")
class User
{
    public Integer id;
    public Date createdDate;

   @Id
   public Integer getId() {
   	return this.id;
   }
   public void setId(Integer id)    {
  	this.id = id;
   }

   public Date getDate() { //this field will be treated as if it had @Column(name=”date ”)
  	return this.createdDate;
   }
   public void setCreatedDate(Date date) {
  	this. createdDate = date;
   }

}

@GeneratedValue annotation

You can assign an id to your new objects yourself, or leave it at the mercy of Hibernate. In order for Hibernate to better understand how to assign IDs to your objects, it has a special annotation:

@GeneratedValue

This annotation usually marks the same field as the @Id annotation . She has 4 possible ID assignment strategies:

  • AUTO
  • IDENTITY
  • SEQUENCE
  • TABLE

An example of an annotation with the specified strategy:

@Entity
@Table(name="user")
class User
{
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    public Integer id;

    public Date createdDate;
}

If no policy value is specified, the AUTO strategy will be selected. The AUTO strategy means that Hibernate will assign the ID itself, based primarily on the data type of the ID field.

Why on type? Yes, because ID types can be very different, for example, String or GUID. Example:

@Entity
@Table(name="user")
class User
{
    @Id
	@GeneratedValue
    public UUID id;

    public Date createdDate;
}

Note: A GUID in Java is called a UUID, historically speaking. And Hibernate can generate unique UUIDs for your objects if you ask it to.

Various @GeneratedValue Strategies

If you specify the type GeneratedValue(strategy = GenerationType.IDENTITY) , then Hibernate delegates the ID setting to the database layer. Typically, this uses a column labeled PRIMARY KEY, AUTOINCREMENT.

But if you want your IDs to be unique and generated according to a specially specified algorithm, then you can use the GeneratedValue(strategy = GenerationType.SEQUENCE) annotation , for example:

@Entity
@Table(name="user")
public class User {
	@Id
	@GeneratedValue(generator = "sequence-generator")
	@GenericGenerator(
  	name = "sequence-generator",
  	strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
  	parameters = {
    	@Parameter(name = "sequence_name", value = "user_sequence"),
    	@Parameter(name = "initial_value", value = "4"),
    	@Parameter(name = "increment_size", value = "1")
    	}
	)
	private long userId;

	// ...
}

There are many more ways to generate an ID. For example, you may have a composite unique key consisting of several columns. And when writing an object to the database, you need to fill in all these columns.

I will not give them in detail. Still, the purpose of our lectures is to get acquainted with Hibernate, and not to retell the official documentation.