Emmanuel Touzery
Emmanuel Touzery

Reputation: 9173

hibernate JPA query works only with namespace

I have a JAR which contains only my model which are annotated classes. Then the application which loads the JAR.

In the persistence.xml of the app I have:

<jar-file> my jar </jar-file>

and in the properties:

<property name="hibernate.archive.autodetection" value="class" />

That works. The entities are detected and this query works fine:

List<Event> events = em.createQuery(
"from com.my.namespace.model.Event", Event.class).getResultList();

However I absolutely need to have the namespace in the query... If I do:

List<Event> events = em.createQuery("from Event", Event.class).getResultList();

Then I get this exception:

Event is not mapped [from Event]

What is even more frustrating is that I give to JPA the class, Event.class as the second argument of my call. The Java codes imports the right package.

How do I get the JPAQL/HQL to "import" the right package to see that entity without needing to explicitely type the namespace?

Update: here is Event.java if it helps.. it is as trivial as it gets:

package com.my.namespace.model;

import javax.persistence.*;

@Entity(name="events")
@SequenceGenerator(name="events_id_seq", sequenceName="events_id_seq")
public class Event {

    @Id @Column(name="id") @GeneratedValue(generator="events_id_seq")
    private Long mId;

    @Column(name="title")
    private String mTitle;

    public Long getId() {
        return mId;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }
}

Upvotes: 0

Views: 822

Answers (2)

Aleksander Blomsk&#248;ld
Aleksander Blomsk&#248;ld

Reputation: 18552

Since your name of the entity is "events" (from @Entity(name="events")) you should select from "events" not "event".

List<Event> events = em.createQuery("from events", Event.class).getResultList();

should therefore work

Upvotes: 2

Subin Sebastian
Subin Sebastian

Reputation: 10997

@Entity(name="events") is the culprit

it has to be

          @Entity
          @Table(name="events")

If you intenteded events as db table name. Otherwise use events in your query instead of Event

Upvotes: 2

Related Questions