Search code examples
hibernatejpanamed-query

NamedQuery for filtering a table based on its foreign key


I have the following two tables

@Entity
@Table(name = "userRequests")
@NamedQueries({

    @NamedQuery(
            name = "User.getByFK",
            query = "SELECT  u FROM User u WHERE u.FK_ID =:FK_ID")
})


public class UserR implements Comparable<User>, Serializable {

    @Id
    @JsonProperty
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    @JsonProperty
    @OneToOne(cascade= CascadeType.ALL)
    @JoinColumn(name="FK_ID")
    FK fk;
}

This gives me an error "could not resolve property: FK_ID"

How can I make this field accessible?

Creating an FK_ID filed in the User object gived a repeated mapping exception


Solution

  • you get that error because there is no filed name FK_ID in UserR class you will need to get the filed using the connection fk

    try to use it like this

     @NamedQueries({
        @NamedQuery(
                name = "User.getByFK",
                query = "SELECT  u FROM User u WHERE u.fk.FK_ID =:FK_ID")
    })