Hibernate Name Annotation

Suppose we want to have a different table name to that of the model class(Default),then how to do that,

@Entity (name=”USER_DETAILS“)  is the annotation used to specify the Table Name and not the default.

Similarly we can have different column names also against the default names,see below code,

Use @Column (name=”USER_ID”)  for the same.

The annotation can be placed on top of the field or the corresponding getter

 

Now as we have changed our table name so lets update the hbm2dll to create in hibernate.cfg.xml file

<property name=”hbm2ddl.auto”>create</property>

and run the HibernateTest.

Output:

Hibernate: drop table if exists USER_DETAILS cascade
Hibernate: create table USER_DETAILS (USER_ID int4 not null, USER_NAME varchar(255), primary key (USER_ID))
Sep 17, 2016 4:03:35 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into USER_DETAILS (USER_NAME, USER_ID) values (?, ?)

So the previous table in dropped and new one is created .


   

Comments are closed