*
The moose likes Object Relational Mapping and the fly likes Can a JPA entity have a field not mapped to a DB column? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can a JPA entity have a field not mapped to a DB column?" Watch "Can a JPA entity have a field not mapped to a DB column?" New topic
Author

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

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hi,

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. Right now if I just add the field to the JPA entity like this...



It gives a DB error...



-206 means the column is unknown to the DB.

Thanks for any help.



Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Write the @Transient in the "get" method of your field.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

this is not a good practice to use the Entity as view Object. will suggest you to have them separately as Objects fetched from database will be attached entities.
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hi Herbert,

Thank you for your response.

I tried what you said but it still is not working. First I tried putting the transient annotation above the getter, then I put it above the field and the getter, and both times it gives the same error...



It seems to be happening when it is running a query, meaning it is putting this extra field in the query. Does transient also prevent the field from being included in queries...



Thanks.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Could you post your query?
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hebert,

Your suggestion worked. I just had to restart my tomcat for it to pick up the change.

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a JPA entity have a field not mapped to a DB column?
 
Similar Threads
Deleting Entity in ManyToOne relationship - Make foreign key as null
@ManyToOne & @JoinColumn
Problem when delete 1 record in ROOM table
Hibernate3.0 with DB2 8.1.3UDB - org.hibernate.exception. SQLGrammarException
Merge detached sub-class entity (JPA InheritanceType.SINGLE_TABLE) org.hibernate.WrongClassException