Kevin Lawrence
Kevin Lawrence

Reputation: 751

Using query.setParameter with a property name that's different than it's column name

I am using Java 1.7, and Hibernate 4.1.9. I'm relatively new to Hibernate, so if I left out any pivotal piece of information, just let me know. I have a javax.persistence.Entity in my class called Meeting that contains this:

@Column(name = "ballot_id")
private Long ballotId;

public Long getBallotId() {
    return ballotId;
}

public void setBallotId(Long ballotId) {
    this.ballotId = ballotId;
}

I am trying to construct a query like this:

Query query = session.createQuery("from Meeting m where m.ballotId=:ballotId");
query.setParameter("ballotId", someLongValue);
meeting = (Meeting) query.uniqueResult();

But I am getting a org.hibernate.exception.SQLGrammarException: Unknown column 'meeting0_.ballotId' in 'field list' error. It seems as though when building the query like this, Hibernate does not check the annotations that indicate that the database column name is different from the object's property name. Is there another way of doing this, or is there something I need to add for this? Maybe I missed something, or got the HQL wrong?

Thanks!

Upvotes: 0

Views: 1229

Answers (2)

acdcjunior
acdcjunior

Reputation: 135822

Exception:

  • org.hibernate.exception.SQLGrammarException: Unknown column 'someTable.someColumn' in 'field list'

  • org.hibernate.exception.SQLGrammarException: Column "someTable.someColumn" not found;

Problem: The column (not the field!) someColumn does not exist in the table (referenced by the someTable entity).

Make sure it exists.

If you have a @Column(name = "someColumn") annotation, make sure it is getting read (is it placed in the right place? field or getter? not both). You have to have to use AnnotationConfiguration when configuring your SessionFactory for them to work. Also, check if you also have a xml mapping file, it may be overwriting the annotations.

Upvotes: 0

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

Looks more like you need to move your annotation :

private Long ballotId;

@Column(name = "ballot_id")
public Long getBallotId() {
    return ballotId;
}

public void setBallotId(Long ballotId) {
    this.ballotId = ballotId;
}

according to this answer and your HQL should be ok.

Upvotes: 1

Related Questions