It seems like you are trying to mix things that are used for different purposes. First, @Formula properties are used for properties that doesn't have a column of their own. The value is always calculated using the formula that you have specified. But I guess this is is not what you want since the 'select max(r.itemId)...' is likely to change over time.
Second, the @Generated annotation is used when there is some mechanism on the database that is generating the value for you. So, if you have a trigger or something in the database that generates (eg. executes the something like the sql in the formula) the value for you the correct annotations to use would be:
Code:
@Generated(GenerationTime.INSERT)
@Column(insertable = false)
I guess that you don't have a trigger in the database either, but that you want to use the formula as some kind of Hibernate-triggered action that is evaluated at insertion time only and then it behaves as a regular property. There is currently no support for this in Hibernate.
If you have the possibility to create a trigger in the database, this is the easiest solution. Otherwise you would have to map it as a regular property and create your own code that initializes new objects with the proper value. Just be sure that you have a strategy for how to avoid duplicates being generated.