If you are using a trigger, the intended generation strategy is org.hibernate.id.SelectGenerator
. However, in order to use this strategy, Hibernate must be able to locate the inserted row after insertion to see what value the trigger assigned there are 2 ways to do this.
First is to specifically configure the generator to tell it a column that define a unique key (at least logically) within the table:
@Id
@Column(name="S_ID")
@GeneratedValue( strategy = "trigger" )
@GenericGenerator(
name="trigger", strategy="org.hibernate.id.SelectGenerator",
parameters = {
@Parameter( name="keys", value="userName" )
}
)
private String s_id;
private String userName;
The other is via Hibernate's natural-id support:
@Id
@Column(name="S_ID")
@GeneratedValue( strategy = "trigger" )
@GenericGenerator( name="trigger", strategy="org.hibernate.id.SelectGenerator" ) )
private String s_id;
@NaturalId
private String userName;
GenerationType.IDENTITY may work for you. It will really come down to the JDBC driver and how (if) it implements getGeneratedKeys
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…