java.sql.SQLSyntaxErrorException: 不是 table 中的列

java.sql.SQLSyntaxErrorException: is not a column in table

我正在使用 EJB 和 JPA 开发 JavaEE 应用程序

我的两个实体是:

@Entity
public class Utente implements Serializable {

private static final long serialVersionUID = 1L;

@Override
public String toString() {
    return "Utente [cod_utente=" + cod_utente + ", nome_utente="
            + nome_utente + ", morada_utente=" + morada_utente
            + ", dnasc_utente=" + dnasc_utente + ", tel_utente="
            + tel_utente + "]";
}

/**
 * 
 */

public Utente(String nome_utente, String morada_utente,
        GregorianCalendar dnasc_utente, int tel_utente) {
    super();
    this.nome_utente = nome_utente;
    this.morada_utente = morada_utente;
    this.dnasc_utente = dnasc_utente;
    this.tel_utente = tel_utente;
}

@Id
@GeneratedValue
private int cod_utente;
private String nome_utente;
private String morada_utente;
@Temporal(TemporalType.DATE)
private GregorianCalendar dnasc_utente;
private int tel_utente;
private List<GregorianCalendar> agenda;
@OneToMany
@JoinColumn(nullable=true)
private List<Prescricao> lista_presc;

public Utente(){
    super();
}

public List<Prescricao> getLista_presc() {
    return lista_presc;
}

public void setLista_presc(List<Prescricao> lista_presc) {
    this.lista_presc = lista_presc;
}

public List<GregorianCalendar> getAgenda() {
    return agenda;
}

public void setAgenda(List<GregorianCalendar> agenda) {
    this.agenda = agenda;
}

public int getCod_utente() {
    return cod_utente;
}

public void setCod_utente(int cod_utente) {
    this.cod_utente = cod_utente;
}

public String getNome_utente() {
    return nome_utente;
}

public void setNome_utente(String nome_utente) {
    this.nome_utente = nome_utente;
}

public String getMorada_utente() {
    return morada_utente;
}

public void setMorada_utente(String morada_utente) {
    this.morada_utente = morada_utente;
}

public GregorianCalendar getDnasc_utente() {
    return dnasc_utente;
}

public void setDnasc_utente(GregorianCalendar dnasc_utente) {
    this.dnasc_utente = dnasc_utente;
}

public int getTel_utente() {
    return tel_utente;
}

public void setTel_utente(int tel_utente) {
    this.tel_utente = tel_utente;
}   
}

和:

@Entity
public class FichaClinica implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */
@Id
@GeneratedValue
private int cod_ficha;
@OneToOne
@JoinColumn
private Utente utente;
@OneToMany
@JoinColumn(nullable=true)
private List <AtoEnfermagem> lista_atos_enf;
@OneToMany
@JoinColumn(nullable=true)
private List <AtoMedico> lista_atos_medicos;
@OneToMany
@JoinColumn(nullable=true)
private List<Consulta> lista_consultas;

public FichaClinica(){ //construtor vazio
    super();
}

public Utente getUtente() {
    return utente;
}

public void setUtente(Utente utente) {
    this.utente = utente;
}

@Override
public String toString() {
    return "FichaClinica [cod_ficha=" + cod_ficha + ", lista_atos_enf="
            + lista_atos_enf + ", lista_atos_medicos=" + lista_atos_medicos
            + ", lista_consultas=" + lista_consultas + "]";
}

public FichaClinica(Utente utente){
    super();
    this.utente=utente;
}

public FichaClinica(Utente utente,
        ArrayList<AtoEnfermagem> lista_atos_enf,
        ArrayList<AtoMedico> lista_atos_medicos,
        ArrayList<Consulta> lista_consultas) {
    super();
    this.lista_atos_enf = lista_atos_enf;
    this.lista_atos_medicos = lista_atos_medicos;
    this.lista_consultas = lista_consultas;
}

//Adicionar elementos às listas

public int getCod_ficha() {
    return cod_ficha;
}

public void setCod_ficha(int cod_ficha) {
    this.cod_ficha = cod_ficha;
}

public List<AtoEnfermagem> getLista_atos_enf() {
    return lista_atos_enf;
}

public void setLista_atos_enf(List<AtoEnfermagem> lista_atos_enf) {
    this.lista_atos_enf = lista_atos_enf;
}

public List<AtoMedico> getLista_atos_medicos() {
    return lista_atos_medicos;
}

public void setLista_atos_medicos(ArrayList<AtoMedico> lista_atos_medicos) {
    this.lista_atos_medicos = lista_atos_medicos;
}

public List<Consulta> getLista_consultas() {
    return lista_consultas;
}

public void setLista_consultas(ArrayList<Consulta> lista_consultas) {
    this.lista_consultas = lista_consultas;
}

public void adicionar_ato_medico(AtoMedico a_medico){
    lista_atos_medicos.add(a_medico);
}

public void adiconar_consulta(Consulta c){
    lista_consultas.add(c);
}

public void adicionar_ato_enf(AtoEnfermagem a_enf){
    lista_atos_enf.add(a_enf);
}
}

当我尝试执行以下方法时:

 @Override
 public String cria_utente(String nome_utente, String morada_utente,GregorianCalendar dnasc_utente, int tel_utente) {
    Utente u=new Utente(nome_utente,morada_utente,dnasc_utente,tel_utente);
    em.persist(u);
    FichaClinica fc=new FichaClinica(u);
    em.persist(fc);
    return "Utente "+Integer.toString(u.getCod_utente())+" (Ficha Clinica "+Integer.toString(fc.getCod_ficha())+")  cirado!";
 }

发生错误。这是完整的堆栈跟踪:

Exception in thread "main" javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:725)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
at com.sun.proxy.$Proxy238.cria_utente(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:490)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:719)
... 24 more
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: 'UTENTE_COD_UTENTE' is not a column in table or VTI 'APP.FICHACLINICA'.
Error Code: 20000
Call: INSERT INTO FICHACLINICA (COD_FICHA, UTENTE_COD_UTENTE) VALUES (?, ?)
bind => [2 parameters bound]
Query: InsertObjectQuery(FichaClinica [cod_ficha=10, lista_atos_enf={[]}, lista_atos_medicos={[]}, lista_consultas={[]}])
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.handleException(EntityManagerSetupImpl.java:696)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.handleException(AbstractSynchronizationListener.java:275)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:170)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:452)
... 26 more
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: 'UTENTE_COD_UTENTE' is not a column in table or VTI 'APP.FICHACLINICA'.
Error Code: 20000
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.ClientSession.executeCall(ClientSession.java:298)
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.insertObject(DatasourceCallQueryMechanism.java:377)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
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:1737)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(CommitManager.java:193)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:138)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4207)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3168)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:352)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:158)
... 28 more
Caused by: java.sql.SQLSyntaxErrorException: 'UTENTE_COD_UTENTE' is not a column in table or VTI 'APP.FICHACLINICA'.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1556)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1505)
at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:778)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:619)
... 58 more
Caused by: org.apache.derby.client.am.SqlException: 'UTENTE_COD_UTENTE' is not a column in table or VTI 'APP.FICHACLINICA'.
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
... 68 more

有人可以帮我找出这里出了什么问题吗?

出现错误是因为您可能没有在 table APP.FICHACLINICA 中创建列 UTENTE_COD_UTENTE。创建列(整数类型)应该可以解决问题。

代码:

@OneToOne
@JoinColumn
private Utente utente;

没有为连接列指定 名称,因此根据此规则(来自 @JoinColumn)创建默认连接列:

Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; ""; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; ""; the name of the referenced primary key column.

您可以像这样为连接列设置 名称

@OneToOne
@JoinColumn(name = "SOMETHING_UTENTE")
private Utente utente; 

但是你必须创建一个这样命名的列。

另请参阅:

  • JPA JoinColumn vs mappedBy