oracle jdbc驱动程序在具有主键的表上没有报告主键列

时间:2019-02-03 21:46:07

标签: oracle jdbc

这是HibernateTools逆向工程所报告的,但这似乎是事实。

oracle jdbc驱动程序在具有主键的表上没有报告主键列。

@Test 
public void checkTable() throws SQLException, IOException {

    System.out.println("in check table");
    assertNotNull(conn);



    Statement s = conn.createStatement();
    ResultSet rset = s.executeQuery("select user from dual");
    rset.next();
    String username = rset.getString(1);
    rset.close();
    try {
    s.execute("drop table " + username  + ".x");
    } catch (Exception e) {
        // nothing it might not exist
    }

    s.execute("create table "  + username + ".x (y number)");
    s.execute("alter table x add constraint x_pk primary key (y)");

    DatabaseMetaData meta = conn.getMetaData();
    final String[] tableTypes = new String[] { "TABLE", "VIEW" };
     ResultSet rs = meta.getTables(null, username, "X",tableTypes);
    rs.next();
    String table = rs.getString("table_name");
    System.out.println("table is " + table);
    rs.close();

    rs = s.executeQuery("select * from user_constraints where table_name = 'X'");
    rs.next();
    String type = rs.getString("constraint_type");
    assertEquals("P",type);  // primary key
    rs.close();

    rs = meta.getPrimaryKeys(null, username,    "X");
    rs.next();
    logger.info("getting pk");
    System.out.print("wtf");
    int colCount = 0;
    while (rs.next()) {
        final String pkName = rs.getString("pk_name");
        logger.info("pkName: {}", pkName);
        int keySeq = rs.getShort("key_seq");  // TODO should probably be column seq
        String columnName = rs.getString("column_name");
        logger.warn("seq: {}, columnName: {}, keySeq, columnName");
        colCount++;
    }
    System.out.println("colCount: " + colCount);
    assertEquals(1,colCount);



}

0 个答案:

没有答案