2013-03-18 29 views
1

堆棧跟蹤:WARN org.hibernate.util.JDBCExceptionReporter - SQL錯誤:-5501,SQLState:42501 錯誤org.hibernate.util.JDBCExceptionReporter - 用戶缺少特權或找不到對象:TEST_CASEorg.hibernate.util.JDBCExceptionReporter - 用戶缺少權限或對象未找到

我的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.rakuten.hashi.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作爲「創建」) 我的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) { 

     } 
    } 
} 

回答

1

來自TestCase.java

我刪除了@Table中的模式,它工作。我也刪除了在DDL和類似引用中創建模式。

所以更新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; 
    } 
} 
0

你的數據庫URL屬性是不對的內存數據庫。和MEM:一個文件正確的使用性能的數據庫如下:

<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 =真用MEM:數據庫中的所有數據,只要最後一個打開的連接關閉刪除。使用文件數據庫,所有數據都會保存。

+0

嗨Fredt,我刪除了停機和WRITE_DELAY,但仍然得到錯誤org.hibernate.util.JDBCExceptionReporter - 用戶缺少特權或找不到對象:TEST_CASE – 2013-03-19 05:58:04

相關問題