I want Hibernate 3.3.0 to generate a value by performing a SELECT query before INSERT
I don't think this is supported.
Alternative #1: you could maybe perform the SELECT as part of the "create" logic, directly in your code.
Alternative #2: use an Hibernate Interceptor, provide a reference to the SessionFactory
and perform the SELECT during onSave
from the Interceptor.
So, what's the right combination of annotations?
Setting defaults is actually not well supported (see HHH-4341) and the easiest solution would be IMO to define a DEFAULT value at the column level. That would be my Alternative #3. Below an example:
@Generated(GenerationTime.INSERT)
@Column(insertable=false, columnDefinition="INT DEFAULT 20")
private int someNumber;
I don't know if this is an option for you and I'm not sure if using RANDOM()
in the DEFAULT would be supported by the database (how could RANDOM() * 2
become an int
by the way?).