【问题标题】:could not interpret id generator strategy : x.y.z.CustomIDGenerator无法解释 id 生成器策略:x.y.z.CustomIDGenerator
【发布时间】:2013-08-13 05:40:30
【问题描述】:

我遇到了 Hibernate 映射问题:无法解释 ID 生成器策略: 堆栈跟踪:

Exception in thread "SpringOsgiExtenderThread-14" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryMTS' defined in OSGi resource[classpath:spring-beans.xml|bnd.id=288|bnd.sym=persistence]: Invocation of init method failed; nested exception is org.hibernate.MappingException: could not instantiate id generator [entity-name=com.db.model.UserProfiles]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
        at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)
        at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)
        at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
        at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)
        at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.MappingException: could not instantiate id generator [entity-name=com.db.model.UserProfiles]
        at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:132)
        at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:230)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1419)
        ... 14 more
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: com.db.custom.id.generator.UserProfileCustomIDGenerator
        at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:151)
        at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:124)
        ... 23 more

我的客户 ID 生成器类如下所示:

public class UserProfileCustomIDGenerator extends IdentityGenerator{

@Override
public Serializable generate(SessionImplementor session, Object object)
        throws HibernateException {
    // TODO Auto-generated method stub
        UserProfiles userProfile = (UserProfiles)object;
        if(userProfile.getId() != 0){
            return userProfile.getId();
        }else{
            Serializable id = super.generate(session, object);
            return id;
        }

}

}

我在谷歌上搜索,但找不到解决方案。 如果我犯了任何错误或有解决方案,请告诉我。 谢谢。

【问题讨论】:

    标签: java hibernate id-generation


    【解决方案1】:

    尝试IdentifierGenerator 而不是IdentityGenerator

    【讨论】:

    • 如果我们实现了 IdentifierGenerator 那么我们不能在没有设置 UserProfile Id 的情况下调用 super.generate 方法。如果我们想在 userProfileId 没有明确设置时使用默认策略,那么我们必须扩展 类。
    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多