AnnotationException没有为超类上具有@Id的实体指定的标识符

时间:2013-03-17 01:05:18

标签: spring hibernate

我正在开始一个新项目,这是我第一次使用Hibernate 4并使用没有hibernate模板的Spring 3。

以下是我的两个实体类:(这似乎发生在所有实体类上)

/**
 * Tweener to connect multiple dictionary entries in various ways
 * @author thom
 */
@Entity
@Table( name = "CROSS_REF",
        uniqueConstraints=    @UniqueConstraint(columnNames = {"FROM_ENTRY_UUID", "TO_ENTRY_UUID", "XREF_TYPE"})
)
public class CrossReference     extends Pojo 
                                 implements Comparable<CrossReference>{
    /**
     * Cross reference types
     * @author thom
     */
    public enum XrefType{
        /**
         * Another way to spell this word
         */
        alternateSpelling,
        /**
         * Word you get by adding or removing a letter
         */
        hook,
        /**
         * Root word with additional endings
         */
        variation
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FROM_ENTRY_UUID", referencedColumnName = "UUID", nullable=false)
    private DictionaryEntry     fromEntry;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "TO_ENTRY_UUID", referencedColumnName = "UUID", nullable=false)
    private DictionaryEntry     toEntry;

    @Column(name = "XREF_TYPE", nullable = false)
    private XrefType            xrefType;
...

这是超级课程:

/**
 * Base class for all POJO objects
 * @author Heavyweight software
 */
public class Pojo {
  public static final long      EMPTYUUID   = -1;

  /**
   * Convert a calendar into a formatted timestamp useful for display
   * @param date the calendar being strung
   * @return a standardized time stamp version of a calendar
   */
  @Transient
  public static String getTimestamp(Calendar date) {
    if(date==null){
      return "";
    }

    DateFormat formatter = DateFormat.getDateTimeInstance();
    String str = formatter.format(date.getTime());
    return str;
  }

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name = "UUID", nullable = false, unique = true)
  protected Long uuid = EMPTYUUID;

这是我的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSessionFactory' defined in URL [file:/home/thom/workspace/LexDataAccess/bin/config/test-dao-spring.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.heavyweight.lexaholic.model.lexicon.CrossReference
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at com.heavyweight.lexaholic.dao.hibernate.HibernateTestSchema.initBeanFactory(HibernateTestSchema.java:42)
    at com.heavyweight.lexaholic.dao.hibernate.HibernateTestSchema.getBeanFactory(HibernateTestSchema.java:50)
    at com.heavyweight.lexaholic.dao.hibernate.HibernateTestSchema.getDictionaryEntryDAO(HibernateTestSchema.java:74)
    at com.heavyweight.lexaholic.dao.hibernate.lexicon.DictionaryEntryHibernateDAOTest.setUp(DictionaryEntryHibernateDAOTest.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.heavyweight.lexaholic.model.lexicon.CrossReference
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:664)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3448)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3402)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 34 more

我正在做的所有研究都说这个错误是由缺少的@Id引起的,但正如你所看到的,我有它。这是基类和Hibernate 4的问题吗?

1 个答案:

答案 0 :(得分:4)

我认为您应该在@MappedSuperclass上添加Pojo