0

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.

7
  • Can you provide an example of such an attribute? Commented Mar 19, 2013 at 14:25
  • Added it in the question. Commented Mar 19, 2013 at 15:50
  • Not trying to be difficult here, but there is no requirement to have your entity fields follow table column names, for example @Column(name='wrkxGrumble') Boolean isNameChangeRequested() {...} So I am still trying to understand the use case. Commented Mar 19, 2013 at 17:22
  • I know, THAT IS WHY I want to map the field and use HQL with nice names. Commented Mar 20, 2013 at 12:08
  • So in your example FJ345KJ is an unmapped table with an id reference to your mapped entity? What will your access="field" column definition look like in Java? Commented Mar 20, 2013 at 13:03

1 Answer 1

2

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.

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

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.