Hello i have this issue on a custom sequence generator that is not managed by spring context.
This issue came to be when we migrated to hibernate 6 as well as spring-boot 3 and spring 6.
The Problem
I cannot seem to get the values from the property file within my customer generator via @Value
I have annotated @Component
to my CustomSequenceGenerator
, see below
CustomSequenceGenerator.java
@Component
public class CustomSequenceGenerator extends SequenceStyleGenerator {
@Value("${value.from.property.file}")
private String valueFromPropFile;
@Override
public Serializable generate(...) {...}
}
MyEntity.java
@Entity
@Table(name="myentity")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_gen")
@GenericGenerator(
name = "seq_gen",
strategy = "com.demo.CustomSequenceGenerator",
parameters = {
@Parameters(name = "increment_size", value = "1")
}
)
private String id;
...
}
Based on the above code snippet, my value valueFromPropFile
is always null.
It seems that CustomSequenceGenerator
is not managed by spring. Instead, hibernate is the one instantiating it and that is why i can't get the values from @Value
.
Confirmed this when i try to display the Object it self.
in CustomSequenceGenerator.java
, in on of the configure
method. i put a logging and display this
. At the same time, i logged somewhere in a @RestController, the instance of the CustomSequenceGenerator
via ApplicatonContext
getBean
method.
Results were, they have different memory reference. therefore i can conluded that they have different instances.
Question
So my question is, how do i make sure that the instance of CustomSequenceGenerator
that is being used by hibernate will be the same instance that is in the spring context. Or how do i make sure hibernate don't instantiate new CustomSequenceGenerator
and instead use one from spring context