Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Add a property in my class that is not mapped to a db-table

I have a table tbl_sky that has 2 properties name and model and I would use Hibernate annotation like;

@Entity @Table(name="tbl_sky") public class Sky implements Serializable {     private String name;     private String model;     private String status;      @Id     public String getName() {         return name;     } . . . 

But I need to add one more property status that does not exist in the table but is needed in the class. How could I declare that property so that I have it in my class but not in my db-table?

All help is appreciated.

like image 530
Adnan Avatar asked Oct 24 '10 13:10

Adnan


People also ask

How are properties of a class mapped to the columns of a database table in hibernate?

In hibernate, the preffered way to do this is by annotations. Annotations are piece of code that eases our work in mapping classes with tables, and methods with coulmns. Also, rows of tables to the Objects of the classes. This is also possible in XML mapping, but I've used annotations more.

Is @entity annotation mandatory in hibernate?

The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity. So, if you use annotations for mappings, @Entity is mandated by the specification and Hibernate has to adhere to it.

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. @Id annotation marks the identifier for this entity.


2 Answers

Use @Transient annotation for field you are not going to store in DB:

@Transient public String getStatus() {     return status; } 

or:

@Transient private String status; 
like image 163
Kel Avatar answered Oct 11 '22 17:10

Kel


Mark it as @Transient, and it won't be part of the DB schema.

like image 24
Sean Patrick Floyd Avatar answered Oct 11 '22 17:10

Sean Patrick Floyd