1

我正在开发一个关于我国一些彩票的 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
4

3 回答 3

1

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

我的解决方案是将我的 EclipseLink 版本从 2.5.1 更新到最新版本2.7.7,如另一篇文章中所述。

于 2020-08-19T10:11:57.000 回答
0

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

因此,您必须遍历数组并分别保存每个 Numbers 对象。

 for (Numbers n : ans) {
     em.persist(ans);
 }
于 2019-10-16T11:44:32.057 回答
0

此错误的常见原因:

  1. 较旧的 persistence.xml 文件位于没有您的 lotter.Numbers 条目的类路径中。
  2. 用于获取 EntityManager 的类加载器与加载您传递给 em.persist 调用的“an” Answer 实例不同。

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

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

于 2019-10-16T15:08:09.713 回答