简体   繁体   English

org.hibernate.AnnotationException:没有为实体指定标识符

[英]org.hibernate.AnnotationException: No identifier specified for entity

I have a groovy entity ClientInvoiceAttachmentExt which extends java entity ClientInvoiceAttachment.我有一个 groovy 实体 ClientInvoiceAttachmentExt,它扩展了 java 实体 ClientInvoiceAttachment。 The ClientInvoiceAttachment has @Id annotation but still am seeing the "No identifier specified for entity" error ClientInvoiceAttachment 有 @Id 注释,但仍然看到“没有为实体指定标识符”错误

Here is my stack trace这是我的堆栈跟踪

[Mar 03 17:11:54] ERROR | org.springframework.web.context.ContextLoader | Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'collaborationAPI': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dc.apps.collaborationportal.security.service.CollaborationSecurityService com.dc.apps.collaborationportal.core.api.CollaborationAPI.security; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'collaborationSecurityService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.dc.apps.collaborationportal.feature.service.FeatureService com.dc.apps.collaborationportal.security.service.CollaborationSecurityService.featureService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'featureService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.dc.core.api.Services com.dc.apps.collaborationportal.feature.service.FeatureService.api; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dc.core.api.SearchAPI com.dc.core.api.Services.search; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchAPI': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private transient org.hibernate.SessionFactory com.dc.core.entity.service.impl.QueryService.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is com.dc.core.common.exception.BaseApplicationException: org.hibernate.AnnotationException: No identifier specified for entity: com.dc.apps.collaborationportal.ebilling.model.impl.ClientInvoiceAttachmentExt

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.dc.apps.collaborationportal.ebilling.model.impl.ClientInvoiceAttachmentExt
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:268)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:223)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:686)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:4035)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3989)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1398)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)
    at com.dc.core.entity.support.CustomFieldsSessionFactoryBean.buildSessionFactory(CustomFieldsSessionFactoryBean.java:252)
    ... 94 more

My pom dependancies我的 pom 依赖

    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search</artifactId>
            <!-- this is the latest version that works with lucene-core 3.0.3
                 (which we are stuck with until jackrabbit supports later versions) -->
            <version>3.3.0.Final</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>ejb3-persistence</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-commons-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Here is my Java class这是我的 Java 类

package com.dc.apps.collaborationportal.ebilling.model.impl;

import javax.persistence.AssociationOverride;
import javax.persistence.Embedded;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.Entity;

import com.dc.core.common.annotations.AttributeMetadataDefaults;
import com.dc.core.common.annotations.EventDefaults;
import com.dc.core.common.annotations.EventHandlerDefaults;
import com.dc.core.entity.model.IPersistentEntityInstance;
import com.dc.core.entity.model.impl.File;
import com.dc.core.events.enums.EventTypeEnum;
import com.dc.core.security.annotation.AttributeReadPermission;
import com.dc.core.security.annotation.AttributeWritePermission;
import com.dc.core.security.annotation.ContainerReference;

@Entity
@EventDefaults({
        @EventHandlerDefaults(eventType = EventTypeEnum.OnBeforeCreate, beanName = "checksumInvoiceAttachmentCommand"),
        @EventHandlerDefaults(eventType = EventTypeEnum.OnBeforeCreate, beanName = "encryptInvoiceAttachmentCommand") })
public class ClientInvoiceAttachment implements IPersistentEntityInstance {

    private static final long   serialVersionUID = 1L;
    private Long                id;

    private File                attachment;

    private String              checksum;

    private String              invoiceNumber;

    @Embedded
    @AssociationOverride(name = "fileData", joinColumns = @JoinColumn(name = "attachment_file_data_id"))
    public File getAttachment() {
        return attachment;
    }

    public String getChecksum() {
        return checksum;
    }

    @Transient
    @AttributeMetadataDefaults(defaultDisplay = true)
    public String getFileName() {
        if (attachment == null) return "";
        return attachment.getName();
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public String getInvoiceNumber() {
        return invoiceNumber;
    }

    public void setAttachment(File attachment) {
        this.attachment = attachment;
    }

    public void setChecksum(String checksum) {
        this.checksum = checksum;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setInvoiceNumber(String invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

}

Here is my groovy class这是我的常规课程

package com.dc.apps.collaborationportal.ebilling.model.impl

import com.dc.core.api.Services;

import java.util.List
import javax.persistence.Column
import javax.persistence.OneToMany
import javax.persistence.ManyToMany
import javax.persistence.ManyToOne
import javax.persistence.Cacheable
import javax.persistence.Transient
import javax.persistence.JoinColumn
import javax.persistence.Embedded
import javax.persistence.Id
import javax.persistence.GeneratedValue
import javax.persistence.OneToOne
import javax.persistence.AssociationOverride
import javax.persistence.Lob
import javax.persistence.FetchType
import javax.persistence.Entity
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Indexed
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import org.hibernate.annotations.NamedQueries
import org.hibernate.annotations.NamedQuery
import org.hibernate.envers.Audited
import org.hibernate.envers.NotAudited
import org.hibernate.annotations.Cascade
import org.hibernate.annotations.Type
//import org.hibernate.annotations.Index
import org.hibernate.annotations.LazyCollection
import org.hibernate.annotations.LazyCollectionOption
import org.hibernate.annotations.Formula
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.hibernate.annotations.BatchSize
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.beans.BeansException
import com.dc.core.entity.enums.VersionStatusEnum
import com.dc.core.behavior.command.model.ICommandResult
import com.dc.core.behavior.command.model.impl.CommandResult
import com.dc.core.behavior.command.model.IEntityInstanceCommand
import com.dc.core.entity.model.IPersistentEntityInstance
import com.dc.core.behavior.trackchanges.model.IEntityChangeset
import com.dc.core.security.model.IContainer
import com.dc.core.security.annotation.ContainerReference
import com.dc.core.security.annotation.Encrypted
import com.dc.core.collaboration.enums.CollaborationRequestStatus
import com.dc.core.collaboration.model.ICollaborationParcel
import com.dc.core.collaboration.model.ICollaborationFeatureDetail
import org.hibernate.annotations.Cache
import org.hibernate.annotations.CacheConcurrencyStrategy


@javax.persistence.Entity
class ClientInvoiceAttachmentExt extends com.dc.apps.collaborationportal.ebilling.model.impl.ClientInvoiceAttachment implements IAmGroovy, Serializable {

    @Autowired
    protected transient Services         services;

 private static final long                    serialVersionUID = 711967972L;
  public java.lang.String getCreatedBy() {
    return this.createdBy;
  }

  public void setCreatedBy(java.lang.String createdBy) {
    this.createdBy = createdBy;
  }

  protected createdBy;

  public java.lang.String getUpdatedBy() {
    return this.updatedBy;
  }

  public void setUpdatedBy(java.lang.String updatedBy) {
    this.updatedBy = updatedBy;
  }

  protected updatedBy;

  public java.sql.Timestamp getCreatedAt() {
    return this.createdAt;
  }

  public void setCreatedAt(java.sql.Timestamp createdAt) {
    this.createdAt = createdAt;
  }

  protected createdAt;

  public java.sql.Timestamp getUpdatedAt() {
    return this.updatedAt;
  }

  public void setUpdatedAt(java.sql.Timestamp updatedAt) {
    this.updatedAt = updatedAt;
  }

  protected updatedAt;

  protected transient ApplicationContext applicationContext; 

}

Your problem scenario is of Inheritance Mapping.您的问题场景是继承映射。 The Id field is there in your super class but what about your child class, how they will be related to their parent. Id 字段在您的超类中,但是您的子类呢,它们与父类的关系如何。

There are different strategy to map an Inheritance hierarchy.有不同的策略来映射继承层次结构。

  1. Using Discriminator value (Single Table)使用鉴别器值(单表)
  2. Table per concrete class每个具体类的表
  3. Table per Child class (if parent is an abstract)每个子类的表(如果父类是抽象类)

I think you are missing annotations related to above.我认为您缺少与上述相关的注释。

In Discriminator based approach.. You need to annotate your ParentClass as follows:在基于鉴别器的方法中.. 您需要按如下方式注释您的 ParentClass:

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)  
@DiscriminatorValue(value="ParentClass")  

And in you child class:在你的孩子班上:

@DiscriminatorValue("childClasss")

Please refer some tutorial on Inheritance Mapping like this one or you can google it.请参考一些教程继承映射像这一个,也可以google一下。

There is a new annotation @MappedSuperclass that you add to the base class and it only affects the OOP without the need to change the DB.您添加到基类中的新注释@MappedSuperclass只影响 OOP,无需更改数据库。 That is what I was looking for.这就是我一直在寻找的。 You can read more about it in this post你可以在这篇文章中阅读更多关于它的信息

I saw the similar error.我看到了类似的错误。 In case you have a Class which has a one to one relation with other class , that other class should have a Primary key as an identifier.如果您有一个与其他类具有一对一关系的类,则该其他类应该有一个主键作为标识符。 (My app was using spring boot,jpa,h2,lombok) (我的应用程序使用的是 spring boot、jpa、h2、lombok)

Ex:前任:

@Entity
@Getter
@Setter
Class A{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

 @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="b")
    public B b;
}

Now the similar error might be raised if you missed writing the primary key or an identifier for the class B which is shown below现在,如果您错过编写 B 类的主键或标识符,可能会引发类似的错误,如下所示

@Entity
@Getter
@Setter
Class b{

   /* Same error may be raised when he below code is missing*/
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long b_id;

     @OneToOne(mappedBy = "bid")
    public A a;

}

I got the same error while working with JPA & I have added the @Id annotation,我在使用 JPA 时遇到了同样的错误,我添加了 @Id 注释,

While working with JPA, kindly make sure that this @Id annotation should be from package javax.persistence.Id & not from org.springframework.data.annotation.Id .在使用 JPA 时,请确保此 @Id 注释应该来自包javax.persistence.Id而不是来自org.springframework.data.annotation.Id

Hope it can save your precious time.希望它能节省您宝贵的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.AnnotationException:未为实体:login.Users指定标识符 - org.hibernate.AnnotationException: No identifier specified for entity: login.Users org.hibernate.AnnotationException:未使用JPA XML实体映射为实体指定标识符 - org.hibernate.AnnotationException: No identifier specified for entity using JPA XML entity-mapping 原因:java.lang.IllegalArgumentException:不是托管类型:&原因:org.hibernate.AnnotationException:未为实体指定标识符: - Caused by: java.lang.IllegalArgumentException: Not a managed type: & Caused by: org.hibernate.AnnotationException: No identifier specified for entity: 异常:org.hibernate.AnnotationException:没有为实体指定标识符:jpa.tenants.DataSourceConfig - Exception: org.hibernate.AnnotationException: No identifier specified for entity: jpa.tenants.DataSourceConfig java.lang.RuntimeException:org.hibernate.AnnotationException:未为实体指定标识符 - java.lang.RuntimeException: org.hibernate.AnnotationException: No identifier specified for entity init 方法调用失败; 嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符 - Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity 如何解决线程“main”org.hibernate.AnnotationException中的错误异常:没有为实体指定标识符:员工 - How to Resolved the error Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity: Employee Org.Hibernate.AnnotationException:没有为实体指定标识符我的表中没有 id - Org.Hibernate.AnnotationException: No Identifier Specified For Entity I don't have a id in my table org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体 - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity 获取 org.hibernate.AnnotationException - Getting org.hibernate.AnnotationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM