0

I have two Projects, one a Maven-Dependancy of the other. Both have seperate PostgreSQL DBs. If I run the Application, an "Not an entity" Error is thrown as soon as I try to use an Entity of the dependend Project in the dependend Project.

I tried already to chance the transaction-type to JTA or RESSOURCE-LOCAL or none at all. I also have generated the Entities again from scrap, using the Eclipse JPA Tools.

User.java:

/**
 * The persistent class for the "Users" database table.
 * 
 */
@Entity
@Table(name = "\"Users\"")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Integer id;

    private String mail;

    private String password;

    // bi-directional many-to-many association to Role
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "\"User_Role\"", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
            @JoinColumn(name = "role_id") })
    private Set<Role> roles;

    // bi-directional many-to-many association to Permission
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "\"User_Permission\"", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
            @JoinColumn(name = "permission_id") })
    private Set<Permission> permissions;

    public User() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMail() {
        return this.mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<Permission> getPermissions() {
        return this.permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    public Set<Role> getRoles() {
        return this.roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

persistence.xml:

    <persistence-unit name="Authentication" transaction-type="JTA">
        <class>com.auticon.learning.authentication.entities.User</class>
        <class>com.auticon.learning.authentication.entities.Permission</class>
        <class>com.auticon.learning.authentication.entities.Role</class>

        <properties>
        <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"></property>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Credentials"></property>
            <property name="javax.persistence.jdbc.user" value="postgres"></property>
            <property name="javax.persistence.jdbc.password" value="******"></property>
        </properties>
    </persistence-unit>

Class where the Error is thrown:

    public static User findUser(String username) {
        CriteriaBuilder builder = SessionConfig.getSession().getCriteriaBuilder();
        CriteriaQuery<User> criteriaQuery = builder.createQuery(User.class);

//Error is thrown when I try to get the Root Parameter of the Entity
        Root<User> root = criteriaQuery.from(User.class);

        criteriaQuery.where(builder.equal(root.get(User_.MAIL), username));
        Query<User> query = SessionConfig.getSession().createQuery(criteriaQuery);

        try {
            return query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

Calling Class in other Project, which is dependend on above Project (yes mostly plain from Primefaces Showcase :P ):

    public void login() {
        FacesMessage message = null;
        boolean loggedIn = false;

//Login.checkPW() first action is calling findUser()
        if (Login.checkPW(username, password)) {
            loggedIn = true;
            message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
        } else {
            loggedIn = false;
            message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "Invalid credentials");
        }

        FacesContext.getCurrentInstance().addMessage(null, message);
        PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
    }

Screen of the PostgreSQL DB (I am not allowed to Post Images in the Post directly)

JPA realising that I have annoteted the Entity correctly, listed it in the persitence.xml and the Table REALLY exists in the DB.

Complete Stacktrace:

Caused by: java.lang.IllegalArgumentException: Not an entity: class com.auticon.learning.authentication.entities.User at org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:536) at org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:126) at org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:153) at com.auticon.learning.authentication.client.DBOperations.findUser(DBOperations.java:22) at com.auticon.learning.authentication.api.Login.checkPW(Login.java:9) at de.auticon.views.UserLoginView.login(UserLoginView.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181) at com.sun.el.parser.AstValue.invoke(AstValue.java:289) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65) at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66) ... 58 more

Thomas
  • 13
  • 7

1 Answers1

0

Dear kindly check the entry of your Entity bean in your configuration file(hibernate.xml or persistence.xml). Actually, our persistence context is built on the basis of our configuration file, so if your Entity file is not registered in that the framework will not treat it as an Entity bean/class.

harit
  • 33
  • 6
  • I listed the persistence.xml above. What is wrong with it? – Thomas Jul 04 '19 at 12:30
  • You have to put that class (which is the cause of throwing the exception not an entity class ) in your persistence class like- e.g- com.demo.DemoEntity – harit Jul 05 '19 at 04:35