Learn about @Column annotation in JPA

In the case of column name of a table in the database which you are working on is not same with the name of the entity you are defining, you can use the @Column annotation to change the name. Here are some other functions that this annotation supports for us. Let’s find out about it in this tutorial.

First, I will create a new Maven project as an example:

Learn about @Column annotation in JPA

Maven dependencies:

In this tutorial, I will use the database as MySQL with a table named clazz, which is defined with the following structure:

Entity of this table is defined initially as follows:

Content of JPA configuration file:

Application class:

OK, so everything is ready, now we will go to the main topic of this tutorial.

In my example, because the properties of the Clazz entity are the same as the names of the columns in the clazz table, then I do not need to use the @Column annotation:

Then you can insert a new record into table clazz without any problem.

Learn about @Column annotation in JPA

If the name attribute of the Clazz entity is a different value, for example, you change it to className, then you need to declare it to use the @Column annotation like this:

The result is the same:

Learn about @Column annotation in JPA

In the @Column annotation, in addition to the name attribute, we can also declare other attributes as follows (most of these attributes are used to define the column structure, which is needed when we use the JPA Tool to generate table from entity) :

columnDefinition

This property allows you to use the Data Definition Language (DDL) to define the structure of a column. Then, we can generate the table from the entity using the JPA Tool with this definition. Using this attribute, you can define the value length of the column (length), the precision for the column with the DECIMAL value (precision), the scale also for the column with the type DECIMAL (scale), is nullable or not (nullable), is unique or not (unique).

For example, you can define a column name using the @Column annotation with the columnDefinition property as follows:

When using the JPA Tool to generate the table, this column will have the following structure:

insertable

This attribute is used to allow the use of this column in an INSERT statement. The default value of this attribute is true. If you define this attribute false as follows:

When you run the example again, you will see the error as follows:

length

This property defines the length of the column value. It will override if you have defined the length of the column in the columnDefinition property.

nullable
This property defines whether this column is null or not. It will also be overridden if you defined it in the columnDefinition property.
precision
This attribute defines the value of the column with the DECIMAL value. It will also be overridden if you defined it in the columnDefinition property.
scale
This attribute defines the scalability of the column with the DECIMAL value type. It will also be overridden if you defined it in the columnDefinition property.
An example with precision and scale attributes is:

where 5 is precision and 2 is scale. If the value of this column is as follows: 123.45, can round two decimal places.

table
Defines the table which this column belongs to.
unique
Defines the uniqueness of the value that this column contains. We can be used it to define the primary key column in a table.
updatable
Like the insertable attribute, this attribute defines whether to use this column in an UPDATE statement. By default, the value of this attribute is true.



Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment