如何通过不同的数据库模式使用JPQL?

时间:2018-08-27 16:21:53

标签: java jpa eclipselink jpql junction-table

在SQL Server数据库中,我有一个映射到JPA的表:

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

junction tables这三个表( DemandaExecutivo,DemandaGerente,DemandaAssessor )是由ORM在不同模式( dbo , SQL Server中的默认架构),将其映射到项目中所有其他表的架构(架构 ade ,如上面的代码所示):

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

我建立了一个JPQL来查询带有一个( ade )表的那3个( dbo )表:

@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)"),

但是由于未映射 dbo 模式,因此无法编译:

  

抽象模式类型[表名]未知

有人知道如何解决它,以便可以使用上面的JPQL吗?

谢谢。

-编辑:

尝试运行项目时,有一个例外:

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)

1 个答案:

答案 0 :(得分:1)

我认为您可能误解了该错误。问题出在JPQL而不是正在使用的SQL。 JPQL需要实体,您在查询中定义的DemandaExecutivo或e.Matricula字段没有任何实体。您将访问uorPosCollection,uorPosCollection1和uorPosCollection1集合,并且JPA将根据您的映射执行必要的联接

查询应该更接近: “从需求中选择d并加入d.uorPosCollection和d.uorPosCollection1 g d.uorPosCollection2一个WHERE(d.prefixo.prefixo =:prefixo)和d.status.idStatus IN(1,2,3,4,5,7) AND d.situacao.idSituacao IN(1,2,3,4,5)AND(e.matricula =:matricula或g.matricula =:matricula或a.matricula =:matricula)“

这假设UorPos实体具有您要传递给查询的类型的矩阵字段。

如果不是这种情况,则需要使用本机SQL查询并自己为其传递表和模式。