有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用JAR文件中的实体时出现java JUnit错误“未知实体类型”

我正在努力解决Java项目中的一个错误

在没有任何GitHub连接的本地项目中运行我的代码,一切正常。在连接到GitHub的项目中运行相同代码的Junit4测试后,就会出现以下错误:

Java.lang.IllegalArgumentException: An exception occured while creating a query in EntityManager:

Exception Description: Error compiling the query SELECT entity FROM DoSomething entity. Unknown entity type DoSomething.

我正在使用Eclipse、EclipseLink、PostgreSQL和JPA。数据库位于我的本地主机上。在运行Git项目时查找localhost可能会有问题吗?我有一个GitProject的本地存储库,所以我无法想象这是一个真正的问题

这就是坚持。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;
    }
}

共 (2) 个答案

  1. # 1 楼答案

    看起来像是你的坚持。在测试中运行时,xml不在类路径上。Tests作用域有另一个类路径,然后是main路径。但是我不知道如何在eclipse中设置这个

  2. # 2 楼答案

    我补充说

    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <class>packagename.DoSomehting</class>
    

    对于所有类,将其复制到xml文件。现在一切正常