2.1 Annotation @Entity, @Table
If you want to map your class to a table in the database, then for this you need to add as many as 3 annotations to it.
First, you need to add an annotation to the class @Entity
. This annotation in front of the class will tell Hibernate that this is not just a class, but a special class whose objects need to be stored in the database.
Example:
@Entity
class User {
public int id;
public String name;
public int level;
public Date createdDate;
}
This is enough for Hibernate to treat objects of this class in a special way.
The second annotation is @Table
. With its help, you can set the name of the table in the database with which this class will be associated. Example:
@Entity @Table(name="user")
class User {
public int id;
public String name;
public int level;
public Date createdDate;
}
If the class name and table name are the same, then the annotation @Table
can be omitted.
Also, if your application works with tables from several schemas at the same time, then you need to specify in which particular schema the table is located:
@Entity @Table(name="user", schema="test")
class User {
public int id;
public String name;
public int level;
public Date createdDate;
}
Yes, everything is as simple as it seems.
2.2 @Column annotation
The second important thing to know is the mapping of table columns to class fields. In the simplest version, Hibernate will simply map the fields of your entity class to the columns of the desired table.
If you want to control all the nuances of mapping, you can use the annotation @Column
. Example:
@Entity @Table(name="user")
class User {
@Column(name="id")
public Integer id;
@Column(name="name")
public String name;
@Column(name="level")
public Integer level;
@Column(name="created_date")
public Date createdDate;
}
The annotation @Column
has various parameters, below we will consider the most popular of them:
# | Attribute name | Attribute type | Description |
---|---|---|---|
1 | name | String | Sets the table column name for the class field |
2 | unique | boolean | All field values must be unique |
3 | nullable | boolean | The field can be null |
4 | length | int | Maximum length (for strings) |
Let's add some constraints to the fields of our User Entity class:
- username must be unique and no longer than 100 characters
- level can be null
- createdDate cannot be null
Then our annotations become:
@Entity @Table(name="user")
class User {
@Column(name="id")
public Integer id;
@Column(name="name", unique=true, length=100)
public String name;
@Column(name="level", nullable=true)
public Integer level;
@Column(name="created_date", nullable=false)
public Date createdDate;
}
You can create an object of type User and even assign all null fields to it, however, when you try to save it to the database, Hibernate will perform a check, and if any restrictions are violated, an exception will be thrown.
2.3 @Id annotation
And one more important annotation is @Id
. It can be used to set the primary key for a table.
You just need to specify this annotation for the desired field - and Hibernate will do everything itself:
@Entity @Table(name="user")
class User {
@Id
@Column(name="id")
public Integer id;
@Column(name="name")
public String name;
@Column(name="level")
public Integer level;
@Column(name="created_date")
public Date createdDate;
}
If you want Hibernate to independently generate the IDs of your objects when adding them to the database, then you need to add one more annotation - @GeneratedValue
. Then our example would look like this:
@Entity @Table(name="user")
class User {
@Id
@GeneratedValue
public Integer id;
@Column(name="name")
public String name;
@Column(name="level")
public Integer level;
@Column(name="created_date")
public Date createdDate;
}
We omitted the annotation @Column
for the id field here, since it does not carry valuable information - the field name and column name in the table are the same, and thanks to the rest of the annotations, Hibernate already understands that we are talking about a table column.
GO TO FULL VERSION