1

In a SQL Server database I have a table mapped to JPA:

@Table(name = "Demandas", catalog = "DIAGE", schema = "ade")

There are three tables (DemandaExecutivo, DemandaGerente, DemandaAssessor) that are junction tables, which were automatically created by the ORM under a differente schema (dbo, a default schema in SQL Server) from the one that all other tables in the project were mapped to (schema ade, as shown in the code above):

@JoinTable(name = "DemandaExecutivo", schema="dbo", joinColumns = {
    @JoinColumn(name = "idDemanda", referencedColumnName = "id")}, inverseJoinColumns = {
    @JoinColumn(name = "Matricula", referencedColumnName = "Matricula")})
@ManyToMany
private Collection<UorPos> uorPosCollection;
@JoinTable(name = "DemandaGerente", schema="dbo", joinColumns = {
    @JoinColumn(name = "idDemanda", referencedColumnName = "id")}, inverseJoinColumns = {
    @JoinColumn(name = "Matricula", referencedColumnName = "Matricula")})
@ManyToMany
private Collection<UorPos> uorPosCollection1;
@JoinTable(name = "DemandaAssessor", schema="dbo", joinColumns = {
    @JoinColumn(name = "idDemanda", referencedColumnName = "id")}, inverseJoinColumns = {
    @JoinColumn(name = "Matricula", referencedColumnName = "Matricula")})
@ManyToMany
private Collection<UorPos> uorPosCollection2;

I've built a JPQL to query those 3 (dbo) tables with one (ade) table:

@NamedQuery(name = "Demandas.findAllByMatricula", query = "SELECT d FROM Demandas d INNER JOIN DemandaExecutivo e ON d.id = e.idDemanda INNER JOIN DemandaGerente g ON d.id = g.idDemanda INNER JOIN DemandaAssessor a ON d.id = a.idDemanda WHERE (d.prefixo.prefixo = :prefixo) AND d.status.idStatus IN (1,2,3,4,5,7) AND d.situacao.idSituacao IN (1,2,3,4,5) AND (e.Matricula= :matricula OR g.Matricula = :matricula OR a.Matricula = :matricula)"),

But since the dbo schema is not mapped, it doesn't compile:

The abstract schema type [table name] is unknown

Does anyone knows how can I fix it so I can use the above JPQL ?

Thanks in advance.

-- Edited:

When trying to run the Project, there is the exception:

org.glassfish.deployment.common.DeploymentException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [adePU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT d FROM Demandas d INNER JOIN DemandaExecutivo e ON d.id = e.idDemanda INNER JOIN DemandaGerente g ON d.id = g.idDemanda INNER JOIN DemandaAssessor a ON d.id = a.idDemanda WHERE (d.prefixo.prefixo = :prefixo) AND d.status.idStatus IN (1,2,3,4,5,7) AND d.situacao.idSituacao IN (1,2,3,4,5) AND (e.Matricula= :matricula OR g.Matricula = :matricula OR a.Matricula = :matricula)]. 
[36, 52] The abstract schema type 'DemandaExecutivo' is unknown.
[88, 102] The abstract schema type 'DemandaGerente' is unknown.
[138, 153] The abstract schema type 'DemandaAssessor' is unknown.
[307, 318] The state field path 'e.Matricula' cannot be resolved to a valid type.
[335, 346] The state field path 'g.Matricula' cannot be resolved to a valid type.
[363, 374] The state field path 'a.Matricula' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
jMarcel
  • 958
  • 5
  • 24
  • 54
  • If your join tables are in a different schema then you have to define that in the `@JoinTable` annotations (unless the default implied by the connection gets the right place). JPQL simply uses the entity mappings to generate SQL, but if you don't mention where some tables are then it will struggle. Post the SQL generated by the JPA provider –  Aug 27 '18 at 17:00
  • @Billy Frost, I've updated the question with the Exception and I also informed the schema in the `@JoinTable(name = "DemandaExecutivo", schema="dbo", joinColumns`, but the Exception continues. Since the Project don't compile, is there any other way to get the _SQL generated by the JPA provider_ ? – jMarcel Aug 27 '18 at 17:21
  • I dont use that provider so cant comment on the specifics of its errors. Is it just having a problem finding the ENTITIES `DemandaExecutivo`, `DemandaGerente` and `DemandaAssessor` ? Because your JPQL refers to them, and so they must be ENTITIES. JPQL does not refer to JOIN TABLES –  Aug 27 '18 at 17:59
  • As I've mentioned in the question, the problem is indeed what you are pointing, too and I don't know how to fix it. Since those junction tables are automattically generated, is there a problem if I (manually) create entity classes and map them ? – jMarcel Aug 27 '18 at 18:07
  • So are those names JOIN TABLES or ENTITIES???? They cannot be used in JPQL if they are NOT ENTITIES. –  Aug 27 '18 at 18:08

1 Answers1

1

I think you may have misinterpreted the error. The problem is with the JPQL not the SQL being used. JPQL requires entities and there aren't any for the DemandaExecutivo or e.Matricula field you have defined in the query. You would access the uorPosCollection, uorPosCollection1 and uorPosCollection1 collections and JPA would perform the joins necessary based on your mappings

The query should be closer to: "SELECT d FROM Demandas d join d.uorPosCollection e d.uorPosCollection1 g d.uorPosCollection2 a WHERE (d.prefixo.prefixo = :prefixo) AND d.status.idStatus IN (1,2,3,4,5,7) AND d.situacao.idSituacao IN (1,2,3,4,5) AND (e.matricula= :matricula OR g.matricula = :matricula OR a.matricula = :matricula)"

This assumes that the UorPos entity has a matricula field of the type you are passing in to the query.

IF this is not the case, you would need to use a native SQL query and pass in the tables and schema for it yourself.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • you are right! Thanks a ton for clarifying me on that. – jMarcel Aug 28 '18 at 21:05
  • For those who want to know, I've built the JPQL this way: `@NamedQuery(name = "Demandas.findAllByMatricula", query = "SELECT DISTINCT d FROM Demandas d INNER JOIN d.uorPosCollection e ON d.id = e.demandasCollection.id INNER JOIN d.uorPosCollection1 g ON d.id = g.demandasCollection1.id INNER JOIN d.uorPosCollection2 a ON d.id = a.demandasCollection2.id WHERE d.prefixo.prefixo = :prefixo AND (e.matricula = :matricula OR g.matricula = :matricula OR a.matricula = :matricula)"),`. – jMarcel Aug 28 '18 at 21:07