【问题标题】:The persistence doesn't recognize the entity class持久化不识别实体类
【发布时间】:2020-02-13 03:04:43
【问题描述】:

我正在开发一个关于我的国家的一些彩票的 java 程序,它有一个包含乐透和其他号码的数据库。实体类没有问题,但是当我运行该问题时,我的持久性单元无法识别一个实体类。我是java初学者,先谢谢了。

这里是Numbers实体类:

@Entity
@Table(name = "NUMBERS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Numbers.findAll", query = "SELECT n FROM Numbers n")
    , @NamedQuery(name = "Numbers.findById", query = "SELECT n FROM Numbers n WHERE n.id = :id")
    , @NamedQuery(name = "Numbers.findByNum", query = "SELECT n FROM Numbers n WHERE n.num = :num AND n.lotto = :lotto")
    , @NamedQuery(name = "Numbers.findByAssociated", query = "SELECT n FROM Numbers n WHERE n.num = :num AND n.associated = :associated AND n.lotto = :lotto")
    , @NamedQuery(name = "Numbers.findByLotto", query = "SELECT n FROM Numbers n WHERE n.lotto = :lotto")})
public class Numbers implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NUM")
    private int num;
    @Basic(optional = false)
    @Column(name = "ASSOCIATED")
    private int associated;
    @Basic(optional = false)
    @Column(name = "LOTTO")
    private String lotto;

以及我用来在数据库中插入Numbers 的代码:

public static boolean insertAlert(EntityManager em, Numbers an) {
        boolean success = false;

        Alert alert = Check.createAlert(Check.CONFIRMATION, "Añadir números asociados", "¿Deseas añadir estos números?");
        Optional<ButtonType> result = alert.showAndWait();

        if (result.get() == ButtonType.OK) {
            em.getTransaction().begin();
            em.persist(an);

            if (em.isJoinedToTransaction()) {
                em.getTransaction().commit();
                System.out.println("NUMBERS SUCCESSFULLY ADDED!");
                alert = Check.createAlert(Check.INFORMATION, "OK", "Números añadidos!");
                alert.showAndWait();
                success = true;
            }
            
            em.clear();
        }

        return success;
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="LotterPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>lotter.lottoClasses.Gordo</class>
    <class>lotter.lottoClasses.Primitiva</class>
    <class>lotter.lottoClasses.Eurojackpot</class>
    <class>lotter.lottoClasses.Seis49</class>
    <class>lotter.lottoClasses.Superonce</class>
    <class>lotter.lottoClasses.Bonoloto</class>
    <class>lotter.lottoClasses.Siete39</class>
    <class>lotter.lottoClasses.Euromillones</class>
    <class>lotter.Repetition</class>
    <class>lotter.Numbers</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:DB;create=true"/>
      <property name="javax.persistence.jdbc.user" value=""/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

持久化单元识别我所有的实体类,但不是这个,我不知道如何解决这个问题......

Caused by: java.lang.IllegalArgumentException: Object: lotter.Numbers[ id=null ] is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
    at lotter.Numbers.insertAlert(Numbers.java:165)
    at lotter.Numbers.insertAssociated(Numbers.java:150)
    at lotter.Lotter.start(Lotter.java:41)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application lotter.Lotter

JPA 日志记录:

[EL Finer]: metadata: 2019-10-17 16:14:45.632--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--Searching for default mapping file in file:/Users/~/NetBeansProjects/Lotter/build/classes/ (There is no English translation for this message.)
[EL Finer]: metadata: 2019-10-17 16:14:45.643--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--Searching for default mapping file in file:/Users/~/NetBeansProjects/Lotter/build/classes/ (There is no English translation for this message.)
[EL Config]: metadata: 2019-10-17 16:14:45.803--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Bonoloto] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.825--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Euromillones] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.827--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.Repetition] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.832--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Gordo] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.833--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Seis49] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.834--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Primitiva] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.835--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Siete39] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.835--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Superonce] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.836--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The access type for the persistent class [class lotter.lottoClasses.Eurojackpot] is set to [FIELD].
[EL Config]: metadata: 2019-10-17 16:14:45.837--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Bonoloto] is being defaulted to: Bonoloto.
[EL Config]: metadata: 2019-10-17 16:14:45.868--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Euromillones] is being defaulted to: Euromillones.
[EL Config]: metadata: 2019-10-17 16:14:45.869--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.Repetition] is being defaulted to: Repetition.
[EL Config]: metadata: 2019-10-17 16:14:45.87--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Gordo] is being defaulted to: Gordo.
[EL Config]: metadata: 2019-10-17 16:14:45.871--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Seis49] is being defaulted to: Seis49.
[EL Config]: metadata: 2019-10-17 16:14:45.873--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Primitiva] is being defaulted to: Primitiva.
[EL Config]: metadata: 2019-10-17 16:14:45.874--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Siete39] is being defaulted to: Siete39.
[EL Config]: metadata: 2019-10-17 16:14:45.875--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Superonce] is being defaulted to: Superonce.
[EL Config]: metadata: 2019-10-17 16:14:45.878--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--The alias name for the entity class [class lotter.lottoClasses.Eurojackpot] is being defaulted to: Eurojackpot.
[EL Finest]: jpa: 2019-10-17 16:14:46.059--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--End predeploying Persistence Unit LotterPU; session file:/Users/user/NetBeansProjects/Lotter/build/classes/_LotterPU; state Predeployed; factoryCount 0
[EL Finer]: weaver: 2019-10-17 16:14:46.059--Thread(Thread[JavaFX Application Thread,5,main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: jpa: 2019-10-17 16:14:46.059--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--Begin predeploying Persistence Unit LotterPU; session file:/Users/user/NetBeansProjects/Lotter/build/classes/_LotterPU; state Predeployed; factoryCount 0
[EL Finest]: jpa: 2019-10-17 16:14:46.059--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--End predeploying Persistence Unit LotterPU; session file:/Users/user/NetBeansProjects/Lotter/build/classes/_LotterPU; state Predeployed; factoryCount 1
[EL Finest]: jpa: 2019-10-17 16:14:46.065--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--Begin deploying Persistence Unit LotterPU; session file:/Users/user/NetBeansProjects/Lotter/build/classes/_LotterPU; state Predeployed; factoryCount 1
[EL Finer]: 2019-10-17 16:14:46.088--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--Could not initialize Validation Factory. Encountered following exception: java.lang.NoClassDefFoundError: javax/validation/Validation
[EL Finest]: properties: 2019-10-17 16:14:46.092--ServerSession(1410760993)--Thread(Thread[JavaFX Application Thread,5,main])--property=eclipselink.logging.logger; value=org.eclipse.persistence.logging.DefaultSessionLog

【问题讨论】:

    标签: java jpa eclipselink compile-time-weaving


    【解决方案1】:

    我遇到了同样的问题,这里的答案都没有解决它。

    我的解决方案是将我的 EclipseLink 版本从 2.5.1 更新到 latest version 2.7.7,如其他 post 中所述。

    【讨论】:

      【解决方案2】:

      Persist 只接受实体而不是列表。

      所以你必须遍历数组并分别持久化每个 Numbers 对象。

       for (Numbers n : ans) {
           em.persist(ans);
       }
      

      【讨论】:

      • 我改了代码,之前试过不是问题...
      【解决方案3】:

      此错误的常见原因:

      1. 较旧的 persistence.xml 文件位于不 有你的 lotter.Numbers 条目。
      2. 用于获取 EntityManager 的类加载器不是同一个 加载了您传递给 em.persist 调用。

      第一个问题更常见,可以通过enabling EclipseLink logging 更容易地找到,然后在部署持久性单元时检查日志以查看加载了哪些类和映射。它的变体只是部署了错误的 jar,首先在类路径上有一个较旧的 jar 等等。

      EclipseLink 使用该类在哈希映射中查找元数据,因此如果使用不同的类加载器,它将找不到任何东西。这是一个不太常见的问题,取决于您如何加载 EntityManagerFactory 和容器类加载器结构。您可能会遇到不止一个 JPA 实体的问题,但这也取决于它们的来源。

      【讨论】:

      • 感谢您的回复。我已经测试过了,PU 没有映射 Numbers 类,它忽略了它。我已经删除了persistence.xml并创建了新的,与数据库相同,但问题仍然存在。我能做什么?
      • 它不会忽略所有实体中的一个。打开日志记录 - 在处理 numbers 类或记录 persistence.xml 和其他实体类所在的位置时,它会显示什么?您是否已验证类路径上只有一个 lotter.Numbers 类文件并且有您的 JPA 注释?
      • 我在帖子中添加了记录器显示,我没有发现问题,没有重复的存档或其他同名的jar。
      • 是否还有其他 persistence.xml 文件,可能在 /Users/~/NetBeansProjects/Lotter/build/classes/ 目录或 meta-inf 子目录中?
      • 不,没有。我已经做了三遍另一个相同的项目,但问题总是存在......
      猜你喜欢
      • 2014-11-27
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 2012-10-12
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      相关资源
      最近更新 更多