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.

1

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Could you paste your class in your question?
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;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.