用户缺少权限或找不到对象

Posted

技术标签:

【中文标题】用户缺少权限或找不到对象【英文标题】:user lacks privilege or object not found 【发布时间】:2016-04-08 17:16:14 【问题描述】:

所以我已经在 Google 和堆栈中进行了搜索,我发现了一堆具有相同错误的 ppl,但似乎没有一个解决方案可以解决我的问题。

我正在使用什么:

Java 8 JavaEE7 TomEE 7.0 M3 JPA 2.0 ORM 1.0 Postgres

我认为问题出在命名查询和多对多关系上。

下面是代码

实体

用户.java

@Entity
@Table(name ="system_user")
public class User 
    @Id
    @Column
    private int id;
    @Column
    private String login;
    @Column
    private String password;

    @ManyToMany
    @JoinTable(
            name="user_role_relation",
            joinColumns=@JoinColumn(name="user_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="role_id", referencedColumnName="id"))
    private List<SystemRole> rolesRelation;

    @Transient
    private List<Role> roles;

    public User()

    public List<Role> getRoles()
        if(this.roles == null)
            List<Role> roles = new ArrayList<>(); 
            for(SystemRole relation : rolesRelation)
                roles.add(relation.getCode());
            
            this.roles = roles;
        
        return new ArrayList<>(this.roles);
    

    public String getLogin()
        return this.login;
    

    public String getPassword()
        return this.password;
    

SystemRole.java

@Entity
@Table(name="system_role")
public class SystemRole
    @Id
    private int id;
    @Column
    private String name;

    @Column
    @Enumerated(EnumType.STRING)
    private Role code;

    @ManyToMany(mappedBy="rolesRelation")
    private List<User> users;

    public SystemRole()

    Role getCode()
        return this.code;
    

    public static  enum Role 
        MANAGER, EXECUTIVE, HRBP, ADMIN, USER;
    

XML 配置

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="Pulse-PU" transaction-type="JTA">

        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>java:global/jdbc/postgresds</jta-data-source>

        <mapping-file>META-INF/user-orm.xml</mapping-file>

        <class>com.appus.pulse.jaas.entity.SystemRole</class>
        <class>com.appus.pulse.jaas.entity.User</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.show_sql" value="true" />
            <property name="eclipselink.logging.level" value="ALL" />
        </properties>

    </persistence-unit>

</persistence>

用户orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
    version="1.0">
    <named-query name="User.getByLogin">
        <query>
            SELECT 
               user
            FROM User user 
            JOIN FETCH user.rolesRelation rr
            WHERE user.login = :login
        </query>
    </named-query>
</entity-mappings>

context.xml

<Context>
      <Resource name="jdbc/postgresds"
        auth="Container"
        type="javax.sql.DataSource"
        initialSize="5"
        maxActive="55"
        maxIdle="21"
        minIdle="3"
        timeBetweenEvictionRunsMillis="34000"
        minEvictableIdleTimeMillis="55000"
        validationQuery="SELECT 1"
        validationInterval="34"
        testOnBorrow="true"
        removeAbandoned="true"
        removeAbandonedTimeout="233"
        driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://localhost:5432/pulse"
        username="postgres"
        password=""/>
</Context>

我用来返回用户的示例代码

资源

TestResource.java

@Path("/user-test")
@Produces("plain/text")
@RequestScoped
public class TestResource 

    @EJB(beanName="userdao")
    private UserRepository userRepository;

    @GET
    public void getTest()
        userRepository.getByLogin("admin");
        System.out.println("teste");
    


接口和DAO

UserRepository.java

@Local
public interface UserRepository 
    public User getByLogin(String login);

UserDAO.java

@Stateless(name="userdao")
public class UserDAO implements UserRepository

    @PersistenceContext(unitName = "Pulse-PU")
    private EntityManager  entityManager;


    public User getByLogin(String login) 
        return entityManager.createNamedQuery("User.getByLogin", User.class)
                            .setParameter("login", login)
                            .getSingleResult();
    


最后是例外

Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: SYSTEM_ROLE
Error Code: -5501
Call: SELECT t1.ID, t1.LOGIN, t1.PASSWORD, t0.ID, t0.CODE, t0.NAME FROM system_role t0, user_role_relation t2, system_user t1 WHERE ((t1.LOGIN = ?) AND ((t2.user_id = t1.ID) AND (t0.ID = t2.role_id)))
    bind => [admin]
Query: ReadAllQuery(name="User.getByLogin" referenceClass=User sql="SELECT t1.ID, t1.LOGIN, t1.PASSWORD, t0.ID, t0.CODE, t0.NAME FROM system_role t0, user_role_relation t2, system_user t1 WHERE ((t1.LOGIN = ?) AND ((t2.user_id = t1.ID) AND (t0.ID = t2.role_id)))")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:684)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:560)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2055)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2740)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2693)
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:559)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1175)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1134)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:460)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1222)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:521)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400)
    at com.appus.pulse.dao.UserDAO.getByLogin(UserDAO.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
    at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
    at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:236)
    at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:203)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:265)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:260)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:89)
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:347)
    at com.sun.proxy.$Proxy113.getByLogin(Unknown Source)
    at com.appus.pulse.ws.TestResource.getTest(TestResource.java:21)
    at com.appus.pulse.ws.TestResource$$OwbNormalScopeProxy0.getTest(com/appus/pulse/ws/TestResource.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.openejb.server.cxf.rs.PojoInvoker.performInvocation(PojoInvoker.java:43)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189)
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99)
    at org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:68)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:253)
    at org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:237)
    at org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.openejb.server.httpd.WebBeansFilter.doFilter(WebBeansFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    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:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    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)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: SYSTEM_ROLE
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
    at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
    at com.sun.proxy.$Proxy114.prepareStatement(Unknown Source)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1565)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1514)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:778)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:621)
    ... 88 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SYSTEM_ROLE
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.SchemaManager.getTable(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source)
    at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
    at org.hsqldb.Session.compileStatement(Unknown Source)
    at org.hsqldb.StatementManager.compile(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 102 more

很抱歉,这篇文章很长,但我想把所有重要的代码都写进去。因此,如果遗漏了任何信息,请告诉我。 我真的不知道问题出在哪里,我认为问题在于没有公共设置器,但据我所知,JPA 不需要使用设置器。

【问题讨论】:

您是否可以使用同一个帐户手动查询该表? @Todd 是的,我正在使用用户 postgres 连接数据库,如果我在那里显示查询并在 Pgadmin 中执行它会返回正确的结果。 什么是“javax.persistence.jdbc.show_sql”?没有这样的标准属性 您说您使用的是 Postgresql,但在该堆栈跟踪中使用的是 HSQLDB!建议你先解决这个问题 ;-) @KimAragonEscobar 不,这不是一个有效的属性。查看 JPA SPEC 而不是谁知道的教程。也许如果您查看 EclipseLink LOG,您会看到 HSQLDB 的来源,因为这里没有任何人可以说发布的内容 【参考方案1】:

所以,我已经解决了这个问题,这是一个配置问题。我注意到在 persistence.xml 中它指向 java:global/jdbc/postgresds 并且我在 context.xml 中定义资源,该资源位于应用程序目录的 META-INF 中。资源的名称是 jdbc/postgresds,所以我只是删除了 jta-data-source 中的 global 并不得不添加资源 JtaManaged="true"。

现在一切正常。

感谢所有评论这篇文章的人。

【讨论】:

以上是关于用户缺少权限或找不到对象的主要内容,如果未能解决你的问题,请参考以下文章

是啥导致错误“用户缺少权限或找不到对象:FieldName”

用户缺少权限或找不到对象:XXXX

HSQLDB:用户缺少权限或找不到对象错误

HSQLDB:用户缺少权限或找不到对象:USERS

SpringBoot 用户缺少权限或找不到对象 HSQLDB

用户缺少权限或找不到对象