我正在尝试在JPA中创建一个基本的CRUD数据库,但我却陷入了'读'部分。我有一个客户端表创建了几个字段,用于存储客户端的信息,但由于某种原因,基本的JPA TypedQuery似乎无法查询它而不会被“无效标识符”命中。我在这里有创建代码,似乎没有什么不合适。
create table Client (
cid NUMBER PRIMARY KEY,
fname VARCHAR2(255) NOT NULL,
lname VARCHAR2(255) NOT NULL,
email VARCHAR2(255),
phone VARCHAR2(255),
address VARCHAR(255) NOT NULL,
age number NOT NULL
);
这里的代码我用来查询类,它只是一个基本的TypedQuery
。
TypedQuery<Client> query = em.createQuery("SELECT a FROM Client a", Client.class);
List<Client> results = query.getResultList();
输出(抛出到格式化片段中):
[EL Info]: 2017-11-29 14:56:23.809--ServerSession(1905114489)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2017-11-29 14:56:24.091--ServerSession(1905114489)--file:////COMPHOME/homedir$/X00136103/Documents/CA2BANKACCOUNT/build/classes/_CA2BANKACCOUNTPU login successful
[EL Warning]: 2017-11-29 14:56:24.356--UnitOfWork(48522228)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "PHONE": invalid identifier
Error Code: 904
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Call: SELECT CID, address, age, email, firstname, lastname, phone FROM Client
Query: ReadAllQuery(referenceClass=Client sql="SELECT CID, address, age, email, firstname, lastname, phone FROM Client")
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "PHONE": invalid identifier
Error Code: 904
Call: SELECT CID, address, age, email, firstname, lastname, phone FROM Client
Query: ReadAllQuery(referenceClass=Client sql="SELECT CID, address, age, email, firstname, lastname, phone FROM Client")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
at model.dbOperations.viewClients(dbOperations.java:21)
at test.Test.main(Test.java:18)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "PHONE": invalid identifier
Error Code: 904
Call: SELECT CID, address, age, email, firstname, lastname, phone FROM Client
Query: ReadAllQuery(referenceClass=Client sql="SELECT CID, address, age, email, firstname, lastname, phone FROM Client")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:682)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2738)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2691)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:495)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1168)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1751)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
... 3 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "PHONE": invalid identifier
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:776)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:897)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1034)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3867)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1502)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:1007)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:642)
... 23 more
无论我尝试做什么,似乎桌子都找不到'手机'。它一直无效,我不知道为什么。
答案 0 :(得分:0)
您写道:
我在这里有创作代码,似乎没有什么不合适。
但是当我比较你的创作剧本时:
create table Client (
cid NUMBER PRIMARY KEY,
fname VARCHAR2(255) NOT NULL,
lname VARCHAR2(255) NOT NULL,
email VARCHAR2(255),
phone VARCHAR2(255),
address VARCHAR(255) NOT NULL,
age number NOT NULL
);
和来自stacktrace的SELECT
语句:
Error Code: 904
Call: SELECT CID, address, age, email, firstname, lastname, phone FROM Client
他们并不完全相同。在stacktrace中,这就是EclipseLink生成的内容,您有firstname
和lastname
列,因为这些列名不在您的CREATE TABLE
语句中。我认为您必须编辑您的实体或更正您的DDL。
<强>更新强>
我不知道为什么它不适合你。我刚刚在Oracle 11和EclipseLink上完成了以下工作,一切正常:
实体:
@Entity
public class Client {
@Id
@GeneratedValue
private int cid;
@Column(name = "firstname", nullable = false)
private String fname;
@Column(name = "lastname", nullable = false)
private String lname;
private String email;
private String phone;
@Column(name = "address", nullable = false)
private String address;
int age;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
// rest of getters / setters
}
EclipseLink生成SQL:
CREATE TABLE CLIENT (
CID NUMBER(10) NOT NULL,
address VARCHAR2(255) NOT NULL,
AGE NUMBER(10) NULL,
EMAIL VARCHAR2(255) NULL,
firstname VARCHAR2(255) NOT NULL,
lastname VARCHAR2(255) NOT NULL,
PHONE VARCHAR2(255) NULL, PRIMARY KEY (CID))