Search code examples
jpaintellij-ideajpql

Cannot execute JPQL query in JPA console Intellij


I'm new in JavaEE and have a hard time using JPA console in Intellij. Wasted lot of time to get rid of this error while executing JPQL query:

jpa-ql> SELECT p FROM PersonEntity p
[2017-04-21 18:44:46] using C:\Users\admin\.IntelliJIdea2017.1\system\compiler\jpaproject4.0.82b748b3\.generated\Jpa_Console\JpaProject4.0-PU-1492789486636\META-INF\persistence.xml
[2017-04-21 18:44:48] PersonEntity is not mapped [SELECT p FROM PersonEntity p]

I created simple JavaEE Persistence project and simple database in MySQL. I added DataSource and generated persistence mappings in Intellij. I imported necessary libraries and got rid of all compile errors. I can see my Database in Database tool window and can execute SQL queries. But I cannot execute JPQL queries due to this annoying error: PersonEntity is not mapped.

persistence.xml:

<persistence-unit name="NewPersistenceUnit">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>models.PersonEntity</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sample"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value=""/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

PersonEntity.java:

package models;


import javax.persistence.*;

@Entity
@Table(name = "person", schema = "sample", catalog = "")
public class PersonEntity {
    private int id;
    private String name;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PersonEntity that = (PersonEntity) o;

        if (id != that.id) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

Database structure: DB structure

Project structure: Project structure

Any ideas to resolve this issue?


Solution

  • Managed to solve problem by myself.

    1. I granted privileges to root user in MySQL database by execution this query: GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost'

    2. In my persistense.xml I've added database user credentials: <property name="hibernate.connection.username" value="dbuser"/> <property name="hibernate.connection.password" value="password"/>

    3. I created main class for performing JPQL queries programmatically:

    `

    import models.PersonEntity;
    import org.hibernate.jpa.HibernatePersistenceProvider;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.spi.PersistenceProvider;
    import java.util.HashMap;
    import java.util.List;
    
    public class App {
        public static final String QUERY = "SELECT p FROM PersonEntity p";
        public static void main(String[] args) {
            PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
            EntityManagerFactory factory = persistenceProvider.createEntityManagerFactory("NewPersistenceUnit", new HashMap());
            EntityManager em = factory.createEntityManager();
            List<PersonEntity> resultList = em.createQuery(QUERY, PersonEntity.class).getResultList();
            System.out.println(resultList);
            em.close();
        }
    }`
    
    1. JPA console worked only after execution of main method. Before that i was getting an error: Access denied for user ''@'localhost'.

    Hope it helps you saving time!