我一直在尝试执行原生UPDATE查询,但我一直收到ORA-01747错误。
public void editemployee (String id, String fname, String lname, String email, String phone,
String hdate, String job, String salary, String comm, String dept) {
StringBuilder editquery = new StringBuilder();
editquery.append("UPDATE EMPLOYEES ");
editquery.append("SET ");
editquery.append(" FIRST_NAME = '"+fname+"', ");
editquery.append(" LAST_NAME = '"+lname+"', ");
editquery.append(" EMAIL = '"+email+"', ");
editquery.append(" PHONE_NUMBER = '"+phone+"', ");
editquery.append(" HIRE_DATE = TO_DATE('"+hdate+"', 'YYYY/MM/DD'), ");
editquery.append(" SALARY = "+salary+", ");
editquery.append(" COMMISSION_PCT = "+comm+", ");
editquery.append(" JOB_ID = (SELECT JOB_ID FROM JOBS WHERE JOB_TITLE = '"+job+"'), ");
editquery.append(" DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME = '"+dept+"'), ");
editquery.append(" MANAGER_ID = (SELECT MANAGER_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME = '"+dept+"') ");
editquery.append("WHERE ");
editquery.append(" EMPLOYEE_ID = "+id);
JPA.em().createNativeQuery(editquery.toString()).executeUpdate();
}
这是针对培训活动的,因此我们需要使用StringBuilders来格式化我们的查询。
这是控制台错误日志的一部分。
2016-08-19 16:16:45,054 - [INFO] - from play in play-internal-execution-context-1
Application started (Dev)
2016-08-19 16:16:47,177 - [ERROR] - from org.hibernate.util.JDBCExceptionReporter in play-akka.actor.default-dispatcher-38
ORA-01747: invalid user.table.column, table.column, or column specification
2016-08-19 16:16:47,178 - [ERROR] - from play in play-internal-execution-context-1
Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
2016-08-19 16:16:47,181 - [ERROR] - from application in play-internal-execution-context-1
至于数据库本身,列名是正确的,因为我能够使用这些列名执行其他本机查询。
public void addemployee (String fname, String lname, String email, String phone,
String hdate, String job, String salary, String comm, String dept) {
StringBuilder addquery = new StringBuilder();
addquery.append("INSERT INTO ");
addquery.append(" EMPLOYEES( ");
addquery.append(" EMPLOYEE_ID, ");
addquery.append(" FIRST_NAME, ");
addquery.append(" LAST_NAME, ");
addquery.append(" EMAIL, ");
addquery.append(" PHONE_NUMBER, ");
addquery.append(" HIRE_DATE, ");
addquery.append(" SALARY, ");
addquery.append(" COMMISSION_PCT, ");
addquery.append(" JOB_ID, ");
addquery.append(" DEPARTMENT_ID, ");
addquery.append(" MANAGER_ID ) ");
addquery.append("VALUES ( ");
addquery.append(" (SELECT max(EMPLOYEE_ID) FROM EMPLOYEES) + 1, ");
addquery.append(" '"+fname+"', ");
addquery.append(" '"+lname+"', ");
addquery.append(" '"+email+"', ");
addquery.append(" '"+phone+"', ");
addquery.append(" TO_DATE('"+hdate+"', 'YYYY/MM/DD'), ");
addquery.append(" "+salary+", ");
addquery.append(" "+comm+", ");
addquery.append(" (SELECT JOB_ID FROM JOBS WHERE JOB_TITLE = '"+job+"'), ");
addquery.append(" (SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME = '"+dept+"'), ");
addquery.append(" (SELECT MANAGER_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME = '"+dept+"')) ");
JPA.em().createNativeQuery(addquery.toString()).executeUpdate();
}
这个特殊的INSERT查询执行得很好,所以我不完全确定如何以及为什么我在UPDATE查询中获得ORA-01747。我还在我的Oracle SQL Developer上测试了这两个查询,他们都在执行。
我确实试图寻找类似的问题,但我看到的大部分内容都是针对INSERT查询的,这让我更加困惑为什么错误发生了。