JAVA解决主键唯一性冲突(自主封装)

public Long getPKS_Val(String tabId, String tableName) {
  Session session = dao.getSession();
  @SuppressWarnings("unchecked")
  List pksList = session.createSQLQuery("select t.g_value + 1 from pks_rmdm t where t.g_key='" + tableName + "'").list();
  if (pksList.size() == 0) {
//      String sql = "select max(t."+ tabId +") + 1 from " + tableName + "";
    @SuppressWarnings("unchecked")
    List maxIdList = session.createSQLQuery("select nvl(max("+ tabId +"), 0) + 1 from " + tableName + "").list();
//      List maxIdList = session.createSQLQuery("select max('t."+ tabId +"') + 1 from '" + tableName + "'").list();
    String maxId = String.valueOf(maxIdList.get(0));
    int in_value = Integer.parseInt(maxId) + 1;
    session.createSQLQuery("insert into PKS_RMDM(g_key, g_value) values('" + tableName + "', '"+ in_value +"')").executeUpdate();
    return Long.valueOf(maxId);
  } else {
    String pksId = String.valueOf(pksList.get(0));
    int up_value = Integer.parseInt(pksId) + 1;
    session.createSQLQuery("update pks_rmdm t set t.g_value=" + up_value + " where t.g_key='" + tableName + "'").executeUpdate();
    return Long.valueOf(pksId);
  }
}

你可能感兴趣的:(JAVA,PKS_RMDM,PKS)