4

Is it possible to add extra member variables in hibernate beans that are not fields in the actual database?

eg. I need to add hasComments a member variable in MyEntity, and has comments is not an actual field in the db.

Komal Goyal
  • 233
  • 5
  • 17
  • possible duplicate of [how to make hibernate ignore class variables that are not mapped!](http://stackoverflow.com/questions/4662582/how-to-make-hibernate-ignore-class-variables-that-are-not-mapped) – earthmover Jan 29 '14 at 05:44

3 Answers3

4

Let either the field be transient, or annotate it with the @Transient annotation.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
  • I tried adding Transient annotation but while I try to read a record from db it gives the following exception org.hibernate.exception.SQLGrammarException: could not load an entity: [com.en.server.domain.Myentity#1] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'myentity0_.hasComments' in 'field list'.. What could be the issue? – Komal Goyal Oct 16 '12 at 07:27
  • Could you paste your class in your question? – Aleksander Blomskøld Oct 16 '12 at 08:37
1

See this

@NotNull
@Column(name = "comment")
private String comment;

@Column(name = "time")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date time;

@Transient private String information;

Vinit Prajapati
  • 1,593
  • 1
  • 17
  • 29
0

For a quick hack to move past this error during development, you can use hbm2ddl to create the database automatically from the hibernate mappings, and it will create fields in the table for the transient properties. This does not solve the long term problem, but it lets you continue to work on other things until you have time to resolve the underlying issue.

CodeMed
  • 9,527
  • 70
  • 212
  • 364