未知Id.generator未找到

时间:2014-03-29 09:13:53

标签: hibernate

Caused by: org.hibernate.AnnotationException: Unknown Id.generator:   com.agitech.erp.model.act.JournalPeriodIdGenerator

为什么hibernate无法找到我的IdentifierGenerator,是否有要添加的注释?或者我应该在配置期间添加它以及如何添加它?

public class JournalPeriodIdGenerator implements IdentifierGenerator {
        private static Long id = new Long(1); // just for test it should return a composite id
        public JournalPeriodIdGenerator(){                  
        }
        @Override
        public Serializable generate(SessionImplementor session, Object object)throws HibernateException {
            //session.list( "select ", queryParameters)
            return id++;
        }       
}
    @Entity
    public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {                 @GeneratedValue(generator="com.agitech.erp.model.act.JournalPeriodIdGenerator")
        @EmbeddedId
        private JournalPeriodId codeId;
        public JournalPeriodId getCodeId() {
            return codeId;
        }
        public void setCodeId(JournalPeriodId codeId) {
            this.codeId = codeId;
        }   
    }

在JB Nizet的帮助下,我也尝试了(因为同样的例外也失败了)

@GenericGenerator(name="JournalPeriodIdGenerator", strategy="increment")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
....
}
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
    private static final long serialVersionUID = 1L;
    @GeneratedValue(generator="JournalPeriodIdGenerator")
    @EmbeddedId
    private JournalPeriodId codeId;
}

我试着提出第二个建议:

@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
    @GenericGenerator(name="JournalPeriodIdGenerator", strategy="jrn_increment")
    public class JournalPeriodIdGenerator implements IdentifierGenerator {
         .....      
    }
    @GeneratedValue(generator="JournalPeriodIdGenerator")
    @EmbeddedId
    private JournalPeriodId codeId;
...
}

另一个测试

@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
        @GeneratedValue(generator="JournalPeriodIdGenerator")//with com.agitech.erp.model.act.JournalPeriodIdGenerator not working too
        @EmbeddedId
        private JournalPeriodId codeId;
....
}
@Entity
@GenericGenerator(name="JournalPeriodIdGenerator", strategy="com.agitech.erp.model.act.JournalPeriodIdGenerator")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
....
}

2 个答案:

答案 0 :(得分:1)

在JB Nizet的帮助下,关注此博客http://blog.eyallupu.com/2011/01/hibernatejpa-identity-generators.html

    @Entity
    public class ActEntry extends Record implements Comparable<ActEntry>,Serializable { 
// here is the definition that JB Nizet is talking about !
        @GenericGenerator(name="JournalPeriodIdGenerator", 
strategy="com.agitech.erp.model.act.JournalPeriodIdGenerator") 
        @GeneratedValue(generator="JournalPeriodIdGenerator")
        @EmbeddedId
        private JournalPeriodId codeId;
        .....
    }
    @GenericGenerator(name="JournalPeriodIdGenerator", strategy="increment")
    public class JournalPeriodIdGenerator implements IdentifierGenerator {
        ....
    }

答案 1 :(得分:0)

javadoc of GeneratedValue说:

  

generator :(可选)要在SequenceGenerator或TableGenerator注释中指定的主键生成器的名称。

要定义自己的生成器,您可以使用GenericGenerator,然后在GeneratedValue中引用GenericGenerator的名称。