【问题标题】:org.hibernate.util.JDBCExceptionReporter - user lacks privilege or object not foundorg.hibernate.util.JDBCExceptionReporter - 用户缺少权限或找不到对象
【发布时间】:2013-03-06 21:02:43
【问题描述】:

堆栈跟踪:警告 org.hibernate.util.JDBCExceptionReporter - SQL 错误:-5501,SQLState:42501 错误 org.hibernate.util.JDBCExceptionReporter - 用户缺少权限或找不到对象:TEST_CASE

我的 applicationContext-junit-test.xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="defaultAutoCommit" value="false">
            </property>
            <property name="driverClassName">
                <value>org.hsqldb.jdbcDriver</value>
            </property>
            <property name="url">
                <value>jdbc:hsqldb:mem:testcasedb;shutdown=true;hsqldb.write_delay=false;</value>
            </property>
            <property name="username">
                <value>sa</value>
            </property>
            <property name="password">
                <value></value>
            </property>
    
        </bean>
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    
                    <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
                    <prop key="hibernate.connection.autocommit">true</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>com.org.team.testcaseapi.dto.request.TestCase</value>
                </list>
            </property>
        </bean>
    </beans>

TestCase.java file:


    @Entity
    @Table(name = "test_case", schema = "testcase")
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    @XmlRootElement
    public class TestCase implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID", nullable = false)
        private Long Id;
    
        public Long getId() {
            return Id;
        }
        public void setId(Long id) {
            Id = id;
        }
    }

**DBUtil.java** runs the DDL and DML hsql files and DB is created successfully(I have printed DB on console).

      public class DBUtil {
                public static void createTestSchema() {
                    String sqlPath = "src/test/resources/sql";
                        ApplicationContext dbCtx = new ClassPathXmlApplicationContext("applicationContext-junit-test.xml");
                BasicDataSource ds = dbCtx.getBean(BasicDataSource.class);
                Connection conn = ds.getConnection();
                        Statement st = null;
                    try {
                        System.out.println("Creating Schema !");
                        File dir = new File(sqlPath);
                        File[] files = dir.listFiles();
                        SqlFile sqlFile;
                        for (File file : files) {
                                sqlFile = new SqlFile(file);
                                sqlFile.setConnection(conn);
                                sqlFile.execute();
                            }
                        }
    }

DaoImpl.java
我的 HQL 查询获取零结果(如果我将 hbm2ddl.auto 设置为“create”) 我的 HQL 查询抛出 JDBCExceptionReporter:用户缺少权限或找不到对象(如果我不使用 hbm2ddl.auto 属性)

public class DaoImpl implements Dao {
    @Autowired
    private SessionFactory sessionFactory;

    public void getAllData(){

        String hql = "FROM TestCase";
        Session session = sessionFactory.openSession();
        List<TestCase> results;
        try {
            Query query = session.createQuery(hql);
            results = query.list();  // list is empty : []
            
        } catch (Exception e) {
            
        }
    }
}

【问题讨论】:

    标签: junit hsqldb


    【解决方案1】:

    来自 TestCase.java

    我删除了 @Table 中的架构并且它起作用了。我还删除了 DDL 中的 Create Schema 和类似的参考。

    所以更新后的 TestCase.java 将如下所示:

    @Entity
    @Table(name = "test_case")
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    @XmlRootElement
    public class TestCase implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID", nullable = false)
        private Long Id;
    
        public Long getId() {
            return Id;
        }
        public void setId(Long id) {
            Id = id;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的数据库 URL 属性对于内存数据库是错误的。正确使用file:和mem:database的属性如下:

          <property name="url">
              <value>jdbc:hsqldb:file:testcasedb;shutdown=true;hsqldb.write_delay=false;</value>
      
          <property name="url">
              <value>jdbc:hsqldb:mem:testcasedb</value>
      

      如果您将 shutdown=true 与 mem: 数据库一起使用,则所有数据都会在最后一个打开的连接关闭后立即删除。使用文件数据库,保存所有数据。

      【讨论】:

      • 嗨 Fredt,我删除了关机和 write_delay,但仍然收到错误 org.hibernate.util.JDBCExceptionReporter - 用户缺少权限或找不到对象:TEST_CASE
      猜你喜欢
      • 1970-01-01
      • 2017-06-14
      • 2019-05-31
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 2019-01-15
      • 1970-01-01
      相关资源
      最近更新 更多