2017-04-21 56 views
0

我是JavaEE中的新成員,很難在Intellij中使用JPA控制檯。浪費了大量的時間,同時執行JPQL查詢擺脫這種錯誤的:無法在JPA控制檯中執行JPQL查詢Intellij

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] 

我在MySQL創建簡單的JavaEE持久性項目和簡單的數據庫。 我在Intellij中添加了DataSource並生成了持久化映射。 我導入了必要的庫,並擺脫了所有編譯錯誤。 我可以看到我的Database in Database工具窗口並且可以執行SQL查詢。 但由於這個令人討厭的錯誤,我無法執行JPQL查詢:PersonEntity未映射。

的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; 
    } 
} 

數據庫結構: DB structure

項目結構: Project structure

任何想法來解決這個問題?

回答

1

自己管理解決問題。

  1. 我授予的權限,以root用戶的MySQL數據庫通過執行這個查詢: GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost'

  2. 在我persistense.xml我已經添加數據庫用戶憑據: <property name="hibernate.connection.username" value="dbuser"/> <property name="hibernate.connection.password" value="password"/>

  3. 我創建以編程方式執行JPQL查詢的主類:

`

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(); 
    } 
}` 
  • JPA控制檯僅主要方法執行之後工作。在此之前,我得到一個錯誤:訪問被拒絕用戶'''本地主機'。
  • 希望它能幫助您節省時間!