Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a JPA entity have a field not mapped to a DB column

Tags:

orm

hibernate

jpa

Is it possible for a JPA entity to have a field which is not mapped to any column in the data base table. Basically I need to store some additional data on my front end but I do not want to create an extra column in the table for this.

like image 341
Kumar Aditya Avatar asked Oct 23 '18 06:10

Kumar Aditya


People also ask

What annotation can exclude fields and properties of entity from mapping in JPA?

Introduction. When persisting Java objects into database records using an Object-Relational Mapping (ORM) framework, we often want to ignore certain fields. If the framework is compliant with the Java Persistence API (JPA), we can add the @Transient annotation to these fields.

Can we use JPA without primary key?

There are a number of different ways you can handle it but it really is situational. If your table without a key has a foreign key into another table that does have an id, you can use the @Embedded annotation.

What is the easiest way to ignore a JPA field during persistence?

To ignore a field, annotate it with @Transient so it will not be mapped by hibernate.

Is @column annotation necessary?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.


2 Answers

Just mark the field or getter with @javax.persistence.Transient annotation.

The data stored there will not be persisted, nor the ddl generator, if you use any, will pick that one up as an additional column of the table.

like image 144
Maciej Kowalski Avatar answered Nov 01 '22 11:11

Maciej Kowalski


Marking @Transient annotation solve this problem but it will work after restart host server(application/web).

like image 41
Kumar Aditya Avatar answered Nov 01 '22 11:11

Kumar Aditya