我正在努力解决Java项目中的错误。
在没有任何GitHub连接的本地项目中运行我的代码,一切正常。只要在连接到GitHub的项目中运行相同代码的Junit4测试,就会发生以下错误:
Java.lang.IllegalArgumentException
:在EntityManager
中创建查询时发生异常:异常说明:编译查询
SELECT entity FROM DoSomething entity
时出错。未知的实体类型DoSomething
。
我正在使用Eclipse,EclipseLink,PostgreSQL和JPA。数据库位于我的本地主机上。运行Git项目时可能有任何问题需要找到localhost吗?我有一个GitProject的本地存储库,所以我无法想象这确实是一个问题。
这是persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/myDatabase" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="secret" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<shared-cache-mode>NONE</shared-cache-mode>
<property name="eclipselink.logging.level" value="off"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
*编辑: 这是我的实体类。
import javax.persistence.*;
@Entity
@NamedQueries({
@NamedQuery(name = "findByName", query = "SELECT r FROM DoSomething r WHERE r.name = :name"),
@NamedQuery(name = "findByPreis", query = "SELECT r FROM DoSomething r WHERE r.preis = :preis"),
})
public class DoSomething {
@Id
@GeneratedValue
private int id;
private String name;
private double preis;
public DoSomething() {
}
public DoSomething(String name, double preis) {
this.name = name;
this.preis = preis;
}
public DoSomething(String name){
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPreis() {
return preis;
}
public void setPreis(double preis) {
this.preis = preis;
}}
我将实体类导出到jar文件中。然后我打开了一个引用jar文件的新项目。在这个项目中,有一个类将实体持久化到数据库中。由于性能要求,我必须将实体和持久化类分成两个项目。
这是坚持不懈的课程:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import demo.persistence.jpa.JpaUtil;
public class DoSomethingDAO {
public void save(DoSomethingDAO entity) throws Exception {
EntityManager em = JpaUtil.createEntityManager();
try {
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
} catch (Exception e) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
throw e;
} finally {
em.close();
}
}
public List<DoSomethingDAO> findByName(String name) throws Exception {
EntityManager em = Persistence.createEntityManagerFactory("myDatabase").createEntityManager();
List<DoSomethingDAO> bList = new ArrayList<DoSomethingDAO>();
try {
TypedQuery<DoSomethingDAO> tQuery = em.createNamedQuery("findByName", DoSomethingDAO.class);
tQuery.setParameter("name", name);
bList = tQuery.getResultList();
em.close();
} catch (Exception e) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
e.printStackTrace();
}
return bList;
}
}
答案 0 :(得分:0)
在测试中运行时,看起来你的persistence.xml不在类路径上。测试范围有另一个类路径,然后是主路径。但是我不知道如何在日食中设置它。
答案 1 :(得分:0)
我添加了
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<class>packagename.DoSomehting</class>
对于xml-File的所有类。现在工作正常。