How to use @Mappedsuperclass @Id with @Sequence Individual - Hibernate oracle

Asked

Viewed 292 times

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 EntidadeDominio, in which all classes extend.

Entidadedominio

    @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 Quence, because the Bank is oracle and it will be responsible for generating this Quence (business rule).

I did several searches for the internet where all had the same code I put, but I only get the error that

org.hibernate.Annotationexception: Unknown Id.Enerator: SQ

as follows the Stacktracer below.

Is there 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 tried several code changes but could not solve, I do not have much experience with Hibernate + oracle.

From now on I appreciate 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)
  • There is the SequenceGenerator in the Oracle database with the name "SQ"?

  • Yes there is. CREATE SEQUENCE SQ MINVALUE 1 MAXVALUE 9999999 INCREMENT BY 1 START WITH 1 NOCACHE;

  • You didn’t reverse the references in name and sequenceName? Link

  • I just reviewed and this right the orders of name and sequence T.T

  • I’ve used it with code like this: @MappedSuperclass&#xA;public class EntidadeDominio implements IEntidade, Serializable {&#xA;&#xA; @Id&#xA; @GeneratedValue(generator="increment")&#xA; @GenericGenerator(name="increment", strategy = "increment") &#xA; @Column(nullable = false, insertable = true, updatable = true)&#xA; public Integer id; But I need to use Quence

2 answers

0

I got 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 class Email:

@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 there is SEQ_EMAIL class Email, is using SQ for all classes. There is no way to overwrite the annotation @SequenceGenerator?

0

I coded to separate each @Id in each class.

I wrote down the class as Inheritance to carry out the inheritance.

Entidadedominio

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

Address class

@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 ...

}

But it is showing 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.