Hibernate can’t find an existing column, ERRORCODE=-4460

Asked

Viewed 190 times

1

I am having a difficulty in my system where SQL with Hibernate can not find a specific column by Java, the same query returns the right result when rotated by SQL. IDE used is Netbeans.

The query is the following:

SELECT DE_CONTEUDO FROM ASP.PDI_EMAIL_MODELO WHERE CD_EMAIL_MODELO = 1;

When run by SQL: inserir a descrição da imagem aqui

When run by Java and Hibernate the program issues the following error:

Parâmetro inválido: nome de coluna desconhecido CD_EMAIL_MODELO. ERRORCODE=-4460, SQLSTATE=null

The Java method is this:

public List pesquisar(){
    String sql = "SELECT DE_CONTEUDO FROM ASP.PDI_EMAIL_MODELO WHERE CD_EMAIL_MODELO = 1";
    Session session = HibernateUtil.open();
    List query = session.createSQLQuery(sql).addEntity(EmailModelo.class).list();
    session.close();    
    return query;
}

The Entity is as follows:

@Entity
@Table(name = "ASP.PDI_EMAIL_MODELO")
public class EmailModelo implements Serializable {

    @Id
    @Column(name = "CD_EMAIL_MODELO")
    private long cdEmailModelo;

    @Column(name = "DE_CONTEUDO")
    private String conteudo;

    // Getters e setters simples ...
}

And Hibernate.cfg is mapped this way:

<mapping class="persistencia.EmailModelo"/>

Additional details:

  • Searches with non-existent rows in the table return an empty list without error in the program. Ex: CD_EMAIL_MODELO = 20
  • I’ve had problems with querys in this IDE that the solution was to literally write the same query in low box but tried this case and did not solve
  • Which database are you using? Which driver are you using to connect to the database?

  • The database is the IBM DB2 and the driver is the 'IBM DB2 Universal Driver'

  • What version of Hibernate? What version of the driver are you using? There are problems reading the column names related to the version of Hibernate and the connection driver for Db2 in a combination of older versions of both

1 answer

1

There are some Hibernate version combinations with the DB2 connection driver version that may generate this error.

The explanation is that older versions of Hibernate (3.x) read the table metadata column name using the field columnName, where the JDBC specification requires the field to be used columnLabel.

Older versions of JDBC were unclear as to this distinction, so it was normal for the drivers to be implemented differently across databases. IBM changed this behavior in driver version 9.5.

The solution, it seems, are two: upgrade Hibernate to a 4.x version or update the connection driver. If for compatibility reasons the driver update is not possible, parameter useJDBC4ColumnNameAndLabelSemantics=2 can be used with the newest connection driver to resolve this issue and maintain compatibility.

  • My version of Hibernate is 4.3.11 but I think the problem is this since when I search a query that returns the entire row (SELECT * FROM) it returns without problems and when I ask for a specific column (SELECT DE_CONTEUDO FROM) it gives this error. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.