Jens Schauder
Jens Schauder

Reputation: 81907

Map a column in Hibernate without a property in the Java class

Is it possible to map a database column using Hiberanate, so I can use it in HQL queries, but not map it to an actual property in the mapped class?

I don't need this attribute in my class and would like to avoid the clutter of getter and setter, which never should get used anyways.

The usecase I have is to set a flag on certain rows, so a different process will pick up the row and process it. We just have to do an update on the field like this:

 update FJ345KJ set wrkxGrumble=1 
 where wrkxGrumble = 0
 and -- more constraints comming here

Since the table and column names forced upon us by the database resemble hashcodes we want to use HQL for the update, which can use nice mapped names. Therefore we need the column mapped in Hibernate.

Upvotes: 0

Views: 1916

Answers (1)

Johanna
Johanna

Reputation: 5293

As far as I know this is not possible. Any mapped column needs a variable in the class.

What you can do is: You map the column with the attribute access = "field" and in the class you declare the variable as private. Then there still is a useless variable declared (this is not a performance issue as the database has to load the row anyway), but no getter and setter is necessary, and as the variable is declared as private it does not influence the interface of your java class, i. e. it is not visible for other classes.

Upvotes: 2

Related Questions