我正在尝试使用JPA选择查询,但我收到了一些错误:
这是我的CountryDto.java
文件
@Entity
@Table(name = "COUNTRY")
public class CountryDto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="COUNTRY_ID")
private int Country_Id;
@Column(name="COUNTRY")
private String Country;
// Getters & Setters go here
public CountryDto() {
// TODO Auto-generated constructor stub
}
public CountryDto(int country_Id, String country) {
// super();
Country_Id = country_Id;
Country = country;
}
}
My Persistence.xml文件如下:
<persistence-unit name="texttiledb" transaction-type="RESOURCE_LOCAL">
<class>com.textileworld.mill.Dto.CountryDto</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/texttiledb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
这是我试图运行的代码
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("texttiledb");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction( ).begin( );
Query query = entitymanager.createQuery("SELECT c FROM COUNTRY c",CountryDto.class);
List<CountryDto> rol= (List<CountryDto> ) query.getResultList();
for (CountryDto con : rol){
System.out.println(con);
}
entitymanager.getTransaction( ).commit( );
entitymanager.close( );
emfactory.close( );
但是我不知道为什么它显示错误,他抽象模式类型'COUNTRY'是未知的。
这是我得到的错误:
Exception Description: Problem compiling [SELECT c FROM COUNTRY c]
[14, 21] The abstract schema type 'COUNTRY' is unknown.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1625)
at com.textileworld.mill.Dao.CountryDao.main(CountryDao.java:24)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT c FROM COUNTRY c].
[14, 21] The abstract schema type 'COUNTRY' is unknown.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
答案 0 :(得分:1)
试试这个......首先你应该使用NamedQuery来提高性能。
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("texttiledb");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction( ).begin( );
Query query = entitymanager.createNamedQuery("selectAllCountry");
List<CountryDto> rol= (List<CountryDto> ) query.getResultList();
for (CountryDto con : rol){
System.out.println(con);
}
entitymanager.getTransaction( ).commit( );
entitymanager.close( );
emfactory.close( );
在CountryDto中定义您的命名查询
@Entity
@NamedQueries({
@NamedQuery(name = "selectAllCountry", query = "SELECT C FROM CountryDto C")
})
@Table(name = "COUNTRY")
public class CountryDto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="COUNTRY_ID")
private int Country_Id;
@Column(name="COUNTRY")
private String Country;
public String getCountry() {
return Country;
}
public int getCountry_Id() {
return Country_Id;
}
public void setCountry(String country) {
Country = country;
}
public void setCountry_Id(int country_Id) {
Country_Id = country_Id;
}
public CountryDto() {
// TODO Auto-generated constructor stub
}
public CountryDto(int country_Id, String country) {
// super();
Country_Id = country_Id;
Country = country;
}
答案 1 :(得分:1)
尝试使用此查询:
"SELECT c FROM CountryDto c"
而不是:
"SELECT c FROM COUNTRY c"
@Table(name = "COUNTRY")
注释用于DB。
答案 2 :(得分:0)
按如下方式更改查询。
Query query = entitymanager.createQuery("SELECT c FROM CountryDao c ",CountryDto.class);
请记住以下内容。
@Table
是可选的。
将POJO类注释为实体
@Entity
如果实体创建如下,
@Entity(name="MyEntityName")
@Table(name="MyEntityTableName")
class MyEntity {
然后创建名为 MyEntityTableName 的表格,实体名称为 MyEntityName 。
您的JPQL查询将是:
select * from MyEntityName
或者,如果实体创建如下,
@Entity
class MyEntity {
将创建名称为 MyEntity 的表格,实体名称将为 MyEntity 。
您的JPQL查询将是:
select * from MyEntity
希望这有帮助。