2015-08-26 25 views
1

假設我有接下來的兩個實體:的ResultSet沒有實體測試過程中正確定位

@Entity 
public class Column { 
.... 
} 

@Enity 
public class Table { 
    ... 
    @OneToMany(fetch=FetchType.EAGER) 
    @JoinColumn 
    private List<Column> column; 
} 

我有一個服務類,與此兩個實體的工作原理:

@Service 
public class JDBCServiceImpl { 
    public void writeColToFile(DataSource dataSource, Table table) { 
    try (Connection connection = dataSource.getConnection()) { 
     int countColumns = table.getColumn.size(); 
     List<String> columnLst = new ArrayList<>(countColumns); 
     for (int i = 0; i < countColumns; i++) { 
      columnLst.add(table.getColumn().get(i).getName()); 
     } 
     ......... 
     ResultSet resultSet = preparedStatement.executeQuery(); 
     for (int i = 0; i < countColumns; i++) { 
      columnFiles[i] = new File(tableDir.getAbsolutePath() + "\\" + 
      columnLst.get(i) + ".txt"); // here I receive exception 
    } 
    } 
} 

我也想考我的服務通過JUnit。

public class TestService { 
     @Test 
     public testJDBCService() { 
      Column col1 = new Column; 
      col1.setName("a"); 
      ColumnInfo col2 = new ColumnInfo(); 
      col2.setName("b"); 
      List<Column> allCols = new ArrayList<>(); 
      allCols.add(col1); 
      allCols.add(col2); 
      // 
      Table table = new Table(); 
      table.setName("table1"); 
      table.setColumn(allCols); 
      ... 
      jdbcService.writeColToFile(dataSource, table); 
     } 
    } 

所以,當我跑我的測試我receice一個異常:

org.postgresql.util.PSQLException:ResultSet的不正確定位, 也許你下一步需要調用。在 org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2888) 在 org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1963) 在 service.loader.impl.JDBCServiceImpl.writeColToFile( JDBCServiceImpl.java:73)

從代碼中,我看到的例外是罰球,而我嘗試遍歷包含我column.getName()對象(此字符串列表:columnLst.get(i))。那麼如何解決這個問題呢?提前致謝。

回答

0

對不起,此數組columnFiles未正確初始化,現在一切正常。