How to use @MappedSuperclass @Id with @Sequence Individual - hibernate oracle

0

I need to solve a question of using inheritance and sequences with hibernate and Oracle.

I'm using Maven with: hibernate-core 5.1.5.Final hibernate-entitymanager 5.1.5.Final hibernate-validator 5.1.3.Final eclipselink 2.6.5 ojdbc6 11.2.0.4 javax.persistence 2.1.1 hibernate-jpa-2.1-api 1.0.0.Final

I have my EntityDomain, in which all classes extend.

Domain Entity

    @MappedSuperclass
public class EntidadeDominio implements IEntidade, Serializable {

    private static final long serialVersionUID = -5377726703339445533L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator = "SQ")
    private Integer id;
//Getter e Setters omitidos
}

Class example: Email

@Entity
@Table(name = "EMAIL")
@AttributeOverride(name = "id", column = @Column(name = "COD_EMAIL"))
@SequenceGenerator(name="SQ", sequenceName = "SEQ_EMAIL", allocationSize = 1)
public class EmailEnvio extends EntidadeDominio{  
...
}

Each entity uses a different sequence because the Bank is oracle and it will be responsible for generating this sequence (business rule).

I made several searches on the internet where all had the same code as I put, but I only get the error that org.hibernate.AnnotationException: Unknown Id.generator: SQ as the StackTracer below.

Do you have any other way to use these annotations?

@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator = "SQ")

@SequenceGenerator(name="SQ", sequenceName = "SEQ_EMAIL", allocationSize = 1)

Or am I using the wrong libraries? I have tried several changes in the code but I could not solve it, I do not have much experience with hibernate + oracle.

Thanks in advance for the help of anyone who can help!

StackTracer:

29-May-2018 14:46:52.888 INFO [http-nio-8084-exec-21] org.apache.catalina.core.ApplicationContext.log Marking servlet Servlet as unavailable
29-May-2018 14:46:52.888 SEVERE [http-nio-8084-exec-21] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet Servlet
 org.hibernate.AnnotationException: Unknown Id.generator: SQ
    at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:664)
    at org.hibernate.cfg.AnnotationBinder.processId(AnnotationBinder.java:2360)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2265)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:913)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:740)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at com.projart.core.dao.DaoFactory.entityManagerFactorInstance(DaoFactory.java:51)
    at com.projart.core.dao.AbstractJdbcDAO.<init>(AbstractJdbcDAO.java:61)
    at com.projart.core.dao.imp.UsuarioDAO.<init>(UsuarioDAO.java:31)
    at com.projart.core.imp.Fachada.<init>(Fachada.java:120)
    at com.projart.view.command.imp.AbstractCommand.<init>(AbstractCommand.java:27)
    at com.projart.view.command.imp.SalvarCommand.<init>(SalvarCommand.java:26)
    at com.projart.view.Servlet.<init>(Servlet.java:67)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:121)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
    
asked by anonymous 29.05.2018 / 19:56

2 answers

0

I arrived somewhere using:

@MappedSuperclass
public class EntidadeDominio implements IEntidade, Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator = "SQ")
    @SequenceGenerator(name="SQ", sequenceName="SQ", allocationSize = 1)
    private Integer id;
}

And Email class:

@Entity
@Table(name = "EMAIL")
@AttributeOverride(name = "id", column = @Column(name = "COD_EMAIL"))
@SequenceGenerator(name  = "SQ", sequenceName = "SEQ_EMAIL")
public class ModeloEmail extends EntidadeDominio {
//atributos e metodos
}

But he is not using the correct Sequence. Instead of using SEQ_EMAIL of the Email class, you are using SQ for all classes. Is there no way to override the @SequenceGenerator annotation?

    
29.05.2018 / 21:37
0

I coded to separate each @Id in each class.

I wrote the class as Inheritance to accomplish the inheritance.

EntityDomain

@Inheritance(strategy = InheritanceType.JOINED)
//@MappedSuperclass
public class EntidadeDominio implements IEntidade, Serializable { ... }

Class Enderection

@Entity
@Table(name = "ENDERECAMENTO")
public class Enderecamento extends EntidadeDominio {

    @Id
    @Column(name="COD_ENDERECAMENTO")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_ENDERECAMENTO")
    @SequenceGenerator(name="SEQ_ENDERECAMENTO", sequenceName = "SEQ_ENDERECAMENTO", allocationSize = 1)
    private Integer Id;

//demais atributos e metodos omitidos ...

}

However, it is displaying the following error:

 java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [id] on this ManagedType [com.projart.model.Enderecamento]
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:128)
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:113)
    at org.hibernate.jpa.criteria.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:116)
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204)
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:177)
    at com.projart.core.dao.imp.EnderecamentoDAO.findByCriterios(EnderecamentoDAO.java:139)
    at com.projart.view.ProcessarEnderecamento.getEnderecamento(ProcessarEnderecamento.java:38)
    at com.projart.view.Servlet.<init>(Servlet.java:102)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:121)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
    
29.05.2018 / 23:16