rayman
rayman

Reputation: 21616

Unexpected JPA error:Invalid column name

Using jboss5.1.x, ejb3.0

This was driving me crazy for two day now:

I am trying simple where clauses query and I keep on getting that error:


this is what I get with JPSQL query:

       String queString ="select s from T04SysParms s WHERE s.key_Name='EXECUTION_TIME'"; 
        Query q = em.createQuery(queString); 
        List<T04SysParms> results = (List<T04SysParms>) q.getResultList(); 

exception I get:

org.hibernate.QueryException: could not resolve property: key_Name of: com.mirs.ecms.db2.T04SysParms [select s from com.mirs.ecms.db2.T04SysParms s WHERE s.key_Name='EXECUTION_TIME']


this is my mappe class:

@Entity
@Table(name = "T04_SYS_PARMS", schema = "ECMS")
public class T04SysParms implements java.io.Serializable
{

// Fields

private String key_Name;
private String value_Name;

// Constructors

/** default constructor */
public T04SysParms()
{
}

/** minimal constructor */
public T04SysParms(String keyName)
{
    this.key_Name = keyName;
}

/** full constructor */
public T04SysParms(String keyName, String valueName)
{
    this.key_Name = keyName;
    this.value_Name = valueName;
}

// Property accessors
@Id
@Column(name = "KEY_NAME", unique = true, nullable = false, length = 100)
public String getKeyName()
{
    return this.key_Name;
}

public void setKeyName(String keyName)
{
    this.key_Name = keyName;
}

@Column(name = "VALUE_NAME", length = 200)
public String getValueName()
{
    return this.value_Name;
}

public void setValueName(String valueName)
{
    this.value_Name = valueName;
}

}

The actualy table name in database: T04_SYS_PARMS the mapped table name is: T04SysParms

any idea? thanks, ray.

Upvotes: 0

Views: 13072

Answers (2)

Don Roby
Don Roby

Reputation: 41135

VALUE is a reserved word in the sql standard. Do you actually have a column named that? If so, you might need to quote it in the query. Or better, change it to a more meaningful name.

Based on comments, this was likely not the root problem, though I think it's healthy to avoid reserved words.

You've also been showing queries that get only the VALUE_NAME field, but then you're trying to pull a whole T04SysParms out of the result.

Based on the update including the mappings, a JPQL version of the query:

String queString ="select s from T04SysParms WHERE s.keyName='EXECUTION_TIME'";
Query q = em.createQuery(queString);
List<T04SysParms> results = (List<T04SysParms>) q.getResultList();

should give you back a list of T04SysParms objects conforming to your needs.

It should be possible to do something similar with the SQL form (adjusting it to get all fields), but to get entities rather than individual fields, JPQL is more appropriate.

Upvotes: 0

Bozho
Bozho

Reputation: 597254

The problem lies in one of the column names. Make sure you have both a VALUE column and a KEY_NAME column. Also take car for potential case-sensitivity.

Btw, I don't see why you need a native query here. Can't you use a JPQL query? Note that in JPQL queries you must refer to classes and fields rather than tables and columns. So it's not KEY but probably key.

By the way KEY is sometimes reserved by databases (like VALUE as others suggested). How about this - open some database administrator tool and try to run the query. Only after it works there move it to your code.

Upvotes: 1

Related Questions