Reputation: 46876
I want Hibernate 3.3.0 to generate a value by performing a SELECT
query before INSERT
(persist()
). Which would work like this:
@Generated(GenerationTime.INSERT)
@GenerateSQL("SELECT RANDOM() * 2")
private int number;
I've had a look at @Generated()
, that's good for TRIGGER
s. I don't want to introduce a trigger.
I also looked at @Formula
, that's read-only.
So, what's the right combination of annotations? Thanks.
Upvotes: 1
Views: 872
Reputation: 570505
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?).
Upvotes: 1