Thanks Max,
My reason for not wanting an instance created to determine the unsaved-value is (ah, the hibernate team will not like this I think) because I am using spring to configure persistent beans via the @Configure annotation. This annotation triggers an aspectj aspect to initialize the instance in the same way as if spring had created the bean. But since the SessionFactory is created during the bootstrap process, the spring contexts are not yet ready and I hit a race condition.
The above aside, and after pondering unsaved-values further, unless there is to be an explicit @UnsavedValue annotation hibernate really /must/ create an instance to determine the unsaved value. Because if the class does initialize the property during construction, reversing the if-conditions in UnsavedValueFactory.getUnsavedIdentifierValue would prevent the class specified default value from ever being considered. (ie, it would simply use 0 in the case of an long, even though the class may specify "@Id private long id = 1010").
So really what I want is for spring to not attempt to configure a bean when created by UnsavedValueFactory. And this is easy enough using my own aspect:
Code:
@Aspect
public class HibernateAwareAnnotationBeanConfigurerAspect extends BeanConfigurerSupport {
public HibernateAwareAnnotationBeanConfigurerAspect() {
setBeanWiringInfoResolver(new AnnotationBeanWiringInfoResolver());
}
@Pointcut("initialization((@org.springframework.beans.factory.annotation.Configurable *).new(..)) && this(beanInstance)")
void beanCreation(Object beanInstance) {}
@Pointcut("call(* org.hibernate.engine.UnsavedValueFactory.instantiate(..)) && within(org.hibernate.engine.UnsavedValueFactory)")
void hibernateUnsavedValueFactory() {}
@AfterReturning("beanCreation(beanInstance) && !cflow(hibernateUnsavedValueFactory())")
public void configure(Object beanInstance) {
configureBean(beanInstance);
}
}
Well, aside from an aspectj bug that prevents this aspect weaving correctly. Once I get thru the aspectj issues, I will post this to the hibernate wiki, as others spring users have been troubled with this exact race condition.
-barry