Creating a column in the table – @Column

In the previous recipe, you learned how to create a table in the database with attributes. Now, we will take a look at how to declare a column in the table with some useful options.

How to do it…

First of all, we take a basic example of creating a column:

  1. We will first create a column with the name empCode in the employee table. As no information is provided for the column name, hibernate uses a variable name. Enter the following code:
    @Entity
    public class Employee {
        @Column
        private String empCode;
    
        // fields and getter/setter
    
    }
  2. If we need a custom column name, we can use the name attribute, as shown in the following code:
    @Column(name="emp_code")
    private String empCode;

Now, hibernate will create a column with the name "emp_code".

There's more…

Let's take a look at some useful attributes available for the @Column annotation.

length

The length attribute is used to provide the column with a maximum size.

Here is an example:

@Column(name="emp_code", length=100)
private String empCode;

Note

If length is not provided, the default size of the data type is used, and it is database-specific.

nullable

The nullable attribute accepts a Boolean value. If nullable is true, it means that the column contains a NULL value. It does not accept NULL if the value of nullable is set to false, but the default value of nullable is true.

Here is an example:

@Column(name="emp_code", nullable=false)
private String empCode;

unique

The unique attribute also accepts a Boolean value. If unique is set to true, hibernate will create a column with a UNIQUE index. However, the default value is false.

Here is an example:

@Column(name="emp_code", unique=true)
private String empCode;

Here, hibernate creates a UNIQUE index for the "emp_code" column.

columnDefinition

This is a useful attribute of the annotation. It accepts a string value. We can give it a SQL fragment, which is used at the time of table creation.

Let's consider a useful example.

If we have a date field and want to set a default date but no date is provided at the time of the insertion of the rows, we can use the following code:

@Column(columnDefinition = "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP")
private Date startDate;

Hibernate directly uses this string while creating the table.

scale and precision

We will consider scale and precision together for a better understanding.

The precision and scale attributes come into the picture when we have the decimal data type of the column and want to store the value with the decimal point.

Here is an example:

@Column(precision = 7, scale = 2)
private BigDecimal salary;

This will create a salary column with the decimal data type and length 7,2. This means that you can enter a value up to 7 and from among that 2 digit contains decimal part.

insertable and updatable

Both these attributes accept a Boolean value. This denotes whether the column takes part in the insert and update operations or not.

See also…

There is another annotation, @JoinColumn, which is used when we want a reference between tables. We will discuss this is in Chapter 5, Working with Associations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.96