Search code examples
hibernateforeign-keyshqlnamed-query

Errors in named queries


I get this error message:

HibernateException: Errors in named queries: History.findByWeekId

My entity class looks like this:

    @Entity
        @Table(name = "xyz_History")
        @NamedQueries({
            @NamedQuery(name = "History.findByWeekId",
                        query = "SELECT h FROM History h WHERE h.weekId = :weekId")
        })

        public class History extends BaseEntity {

        @ManyToOne( fetch = FetchType.EAGER, cascade = CascadeType.ALL )
        @JoinColumn(name = "weekId", nullable = false )
        @ForeignKey(name = "FK_History_WeeklyOverview")
        private WeeklyOverview weeklyOverview;
...

The problem seems to be the weekId. If I try to use this instead of a named query:

        ...createQuery( "SELECT ah FROM History ah WHERE ah.weekId = :weekId " )
        .setParameter("weekId", weekId)
        .getResultList();

I get the error message: could not resolve property: weekId of: com.john.doe.History

The weekId passed in as parameter is a Long. The DB field is an int. Is that a problem?

Or how do I query a @JoinColumn correctly?

The WeeklyOverview entity:

@Entity
@Table(name = "xyz_WeeklyOverview")
public class WeeklyOverview {
@Id
@GeneratedValue
private Long id;
...
}

Solution

  • The error occured because you don't have a field named weekId in the History entity. The field you need is the weeklyOverview and the query would be the following:

    @NamedQuery(name = "History.findByWeekId",
               query = "select h from History h where h.weeklyOverview.id = :weekId")