org.hibernate.AnnotationException: No identifier specified for entity


Ran Paul

I have an extendable entity ClientInvoiceAttachmentExt which extends the Java entity ClientInvoiceAttachment. ClientInvoiceAttachment has @Id annotation but still seeing "No identifier specified for entity" error

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 dependencies

    <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

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;
    }

}

This is my funky 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; 

}
Cherry blossoms

Your problem scenario is inheritance mapping. The Id field exists in your superclass, but what about your subclasses, how will they relate to their parent.

There are different strategies for mapping inheritance hierarchies.

  1. Using discriminators (single table)
  2. Tables for each specific category
  3. table for each subclass (if the parent class is abstract)

I think you are missing a note related to the above.

in a Discriminator based approach. You need to annotate your ParentClass as follows:

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

In your child's class:

@DiscriminatorValue("childClasss")

Please refer to some tutorials on inheritance mapping like this or you can also search with it.

Related


No identifier specified for entity: XYZ (AnnotationException)

ppst99 entity import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entit

No identifier specified for entity: XYZ (AnnotationException)

ppst99 entity import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entit

hibernate: no identifier specified for entity

Patrick H I am trying to build a small project in Hibernate and use mysql docker database for demonstration. Everything worked fine until I added a new class called Flight and a corresponding FlightController. The log says: 2018-12-02 14:42:47,281 DEBUG [main]

Identifier not specified for entity in Java Hibernate

Pratic Josh I am using hibernate and I have tables mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records with

Identifier not specified for entity in Java Hibernate

Pratik Joshi I am using hibernate and I have the table mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records w

Identifier not specified for entity in Java Hibernate

Pratic Josh I am using hibernate and I have tables mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records with