1

I am developing a java program about some lotteries of my country, it has a database with the lottos and other numbers. There is no problem with the entity classes, but when I run the problem my persistence unit does not recognize one entity class. I am a beginner in java, thanks in advance.

Here is the Numbers entity class:

@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;

And the code that I use to insert Numbers in the database:

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;
}

The 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>

The persistence unit recognizes all my entity classes but not this one, I don't know how to resolve this...

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 Logging:

[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
rMonteiro
  • 1,371
  • 1
  • 14
  • 37
rtc__
  • 11
  • 3

3 Answers3

1

I had the same problem and none of the answers here fixed it.

My solution was to updated my EclipseLink version from 2.5.1 to the latest version 2.7.7 as described in this other post.

0

Persist only takes on entity not a list of.

So you have to iterate over the array and persist each Numbers object separately.

 for (Numbers n : ans) {
     em.persist(ans);
 }
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

Common reasons for this error:

  1. An older persistence.xml file is on the class path that doesn't have your lotter.Numbers entry.
  2. The class loader used to obtain the EntityManager isn't the same one that loaded the 'an' Answer instance you are passing to the em.persist call.

The first issue is far more common, and can be found more easily by enabling EclipseLink logging and then checking the log when it deploys the persistence unit to see what classes and mappings are loaded. Variations of it are just deploying the wrong jar, having an older jar on the class path first etc.

EclipseLink uses the class to lookup the metadata in a hash map, so if different class loaders are used, it won't find anything for it. This is a less common issue that depends on how you are loading your EntityManagerFactory and the container classloader structure. You would have problems with more then just one of your JPA entities, but it would depend on where they are coming from too.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • Thanks for your response. I have tested it, the PU doesn't map Numbers class, it's ignoring it. I have deleted the persistence.xml and created new, and the same with the database, but the problem still exists. What can I do? – rtc__ Oct 16 '19 at 18:28
  • It won't just ignore one entity out of all the ones there. Turn on logging -what does it show when it comes time to process the numbers class, or log for where the persistence.xml and other entity classes are located? Have you verified that there is only a single lotter.Numbers class file on the class path and has your JPA annotations? – Chris Oct 16 '19 at 22:22
  • I've added in my post that the logger shows, I don't find the problem, there is not a duplicated archive or other jar with the same name. – rtc__ Oct 17 '19 at 14:30
  • are there other persistence.xml files, possibly in the /Users/~/NetBeansProjects/Lotter/build/classes/ directory or a meta-inf subdirectory? – Chris Oct 17 '19 at 15:54
  • No, there aren't. I've made another equal project three times and the problem it's always there... – rtc__ Oct 17 '19 at 16:16
  • Change the name of the persistence-unit in the xml and see if you get an exception stating that "LotterPU" doesn't exist. If you don't, then you know it is picking up a different file. Also try removing other entities from the file and see what happens, as it doesn't make any sense that it would load some but not all listed entities without giving any warning or error message. – Chris Oct 20 '19 at 18:31
  • The problem was in a method, I had a "functional operation" in a method, when I replaced it to a "for loop", the PU recognized the class and it worked fine. It was hard to find because I didn't know that it was a mistake. Thanks for your help! – rtc__ Oct 21 '19 at 11:39