Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Hibernate use logical name for entity property different from field name?

Let's say, there's a Hibernate entity configured with field access by means of annotations. I would like to map a Java class field _name so that its logical name for Hibernate be name, for instance, when referred from HQL queries. I need this mostly for collections.

Anticipating improper suggestions: switching access type to "property" is not possible; the task has nothing to do with the name of the physical column.

like image 899
Jiří Vypědřík Avatar asked Jun 10 '13 14:06

Jiří Vypědřík


People also ask

What is hibernate entity name?

entity-name is by default the class name. This name we can use in our HQL Queries like table name in normal SQL Queries. You can give any entity name in mapping file and use it in your HQL instead of table name.

What is the use of @entity annotation in hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.

What will happen if @table is not specified while defining pojo?

If no @Table is defined the default values are used: the unqualified class name of the entity. For example if you have: @Entity public class MyTest{ ... Your table will have the name my_test in your database.

What are entity States defined in hibernate?

In the Hibernate framework, an entity can be in three states, transient, persistent, and detached.


2 Answers

Based on my understanding of your question - You could define the entity like this. This will generate a hibernate table named (NewName_ABC with a column name)

@Entity
@Table(name = "NewName_ABC")
public class ABC
{
.
@Column(name = "name")
private string _name;
.
.
}

parallely can use liquibase to create the table.

like image 128
ND27 Avatar answered Oct 07 '22 20:10

ND27


Do you mean like this?

@Column(name = "name")
private string _name;
like image 35
Kenneth Nissel Avatar answered Oct 07 '22 20:10

Kenneth Nissel