0

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!

Kevin Lawrence
  • 741
  • 9
  • 18

2 Answers2

1

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.

Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Thanks for your reply Alexandre. I had tried that previously, but it produces this error: `org.hibernate.QueryException: could not resolve property: ballot_id`, which seems to be a step back. – Kevin Lawrence May 16 '13 at 03:09
  • Well the annotation is for the column name, I had to search because I'm still using hbm.xml files. In your case, your HQL should stay ballotId and only changing annotation to the getter. – Alexandre Lavoie May 16 '13 at 03:25
  • Alexandre, that was the problem! I had left an annotation on one of the getters, and thus Hibernate only used getter annotations and disregarded the ones on my variable declarations. acdcjunior, you're right, it does work. It was just one of those scenarios where it would've be verbose posting the whole class because it's quite large, however I would've had to for anyone to see the problem. Many thanks guys! – Kevin Lawrence May 16 '13 at 04:04
0

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.

Community
  • 1
  • 1
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • It exists. The problem is that the table columns do not have the naming convention that Java uses. So despite having the annotation `@Column(name="ballot_id")`, it still looks for a column with name `ballotId` – Kevin Lawrence May 16 '13 at 03:57
  • They do not need to follow any convention, I have tested right now with a `@Column(name="ballot_id")` and it works. Are you sure this annotation is `javax.persistence.Column`? – acdcjunior May 16 '13 at 04:05
  • What I meant is Java has a naming convention for variables, and by default Hibernate uses the variable name when it refers to a database column, unless specified by this `@Column` annotation. Anyways, as mentioned in the comments above, the problem ended up being that there was an annotation on a getter, which resulted in the annotations on the variable declarations being disregarded. – Kevin Lawrence May 16 '13 at 04:36
  • Oh, yeah, the field names follow the bean convention. I meant the column names (of the table in the database) can be anything. All you have to do is annotate with `@Column(name="database_crazy_column_name")`. – acdcjunior May 16 '13 at 04:37