Hibernate Annotations - @Table and @Column

Hi Everyone, in this blog post we will learn more about using the hibernate annotations @Entity and @Column. In this program, we are modifying the table and column name of the fields in the sample program of hibernate, which was discussed in my earlier post.

The name of the table is changed using @Table annotation and name of the column is changed using @Column annotation.

The updated code for AccountDetails.java is as follows:

package com.hibernate;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name = "ACCOUNT_TABLE")
public class AccountDetails {

    @Id
    @Column(name = "ACCOUNT_ID")
    public int accountId;
    
    @Column (name = "ACCOUNT_NAME")
    public String accountName;
    
    @Column (name = "ACCOUNT_DATE")
    public Date accountDate;
    
    public int getAccountId() {
        return accountId;
    }
    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }
    public String getAccountName() {
        return accountName;
    }
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }
    public Date getAccountDate() {
        return accountDate;
    }
    public void setAccountDate(Date accountDate) {
        this.accountDate = accountDate;
    }
    
}


In the above program we have changed the name of the table and columns. So in the database, the tables and columns of the corresponding names will be created.

The output which we get after executing this program is as given below:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: insert into ACCOUNT_TABLE (ACCOUNT_DATE, ACCOUNT_NAME, ACCOUNT_ID) values (?, ?, ?) 
Hibernate: insert into ACCOUNT_TABLE (ACCOUNT_DATE, ACCOUNT_NAME, ACCOUNT_ID) values (?, ?, ?)


I hope everyone understood this post very well. Please comment below if you have any doubts. All the comments will be responded soon. Suggestions to improvement are always welcome.

Thank you for reading the post.

Other post about hibernate annotations - Some important hibernate annotations. 

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++