Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write the @Transient in the "get" method of your field.
 
Anurag Verma
Ranch Hand
Posts: 168
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert,

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

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic