Domanda

I am facing one problem regarding Many to One relationship in JPA. The problem I am getting at this moment is to get data by using where clause.

Skills class:

    @Id
    private long skillsID;
    @Basic
    private String longDescription;
    @Basic
    private String shortDescription;
    @Basic
    private String colOrder;
    @Basic
    private String isActive;
    @Basic
    private String changeDate;
    @ManyToOne(fetch=FetchType.EAGER)
    private Category category;

Second Entity is:

Category class:

    @Id
    private long categoryID;
    @Basic
    private String name;
    @Basic
    private String colOrder;
    @Basic
    private String isActive;
    @Basic
    private String changedDate;
    @OneToMany(mappedBy = "category")
    private Collection<Skills> skills;

So in JPA, when i excute following code, it throw me exception like:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 

Exception Description: Problem compiling [SELECT s.longDescription  from Skills s, Category c  where s.categoryID =  c.categoryID]. 

[57, 69] The state field path 's.categoryID' cannot be resolved to a valid type.

JPA Code is:

Query q = em .createQuery("SELECT s.longDescription  from Skills s, Category c "+
"where s.categoryID =  c.categoryID");

So, how can I get data from Skills to Category based on the foreign key?

È stato utile?

Soluzione

There is no field categoryID in Skills entity.

Just use it's category field in the Query:

Query q = em .createQuery("SELECT s.longDescription from Skills s, Category c " +
                          "WHERE s.category =  c");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top