Error "javax.persistence.PersistenceException Type xxx is not found (error 301)" while using ObjectDB (Java)

391 Views Asked by At

I'm working with ObjectDB at school, and, while doing homework I had a problem.

There is the problem:

Exception in thread "main" [ObjectDB 2.8.7] DELETE FROM  ==> Jugador <== 
javax.persistence.PersistenceException
Type Jugador is not found (error 301)
 (position 12)  at com.objectdb.jpa.JpaQuery.executeUpdate(JpaQuery.java:805)
    at com.mycompany.objectdb.NewMain.eliminarDatos(NewMain.java:89)
    at com.mycompany.objectdb.NewMain.main(NewMain.java:38)
Caused by: com.objectdb.o.TEX: Type Jugador is not found
    at com.objectdb.o.MSG.b(MSG.java:110)
    at com.objectdb.o.TRS.b(TRS.java:214)
    at com.objectdb.o.SYR.g(SYR.java:261)
    at com.objectdb.o.SYR.f(SYR.java:190)
    at com.objectdb.o.QRC.<init>(QRC.java:158)
    at com.objectdb.o.QRM.Z1(QRM.java:272)
    at com.objectdb.o.MST.Z1(MST.java:1026)
    at com.objectdb.o.WRA.Z1(WRA.java:313)
    at com.objectdb.o.WSM.Z1(WSM.java:117)
    at com.objectdb.o.QRR.k(QRR.java:260)
    at com.objectdb.o.QRR.i(QRR.java:154)
    at com.objectdb.jpa.JpaQuery.executeUpdate(JpaQuery.java:791)
    ... 2 more
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:764)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:711)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:289)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:78)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:567)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------

There is my Main and my class "Jugador", which is reference that is doesn't exist.

package com.mycompany.objectdb;

import javax.persistence.; import java.util.; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger;

public class NewMain {

public static EntityManagerFactory emf;
public static EntityManager em;
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int opcion = -1;
    int opcion3 = -1;
    
    while (opcion != 0) {
        System.out.println("0. Cerrar aplicación");
        System.out.println("1. Conexión a BBDD");
        System.out.println("2. Reiniciar datos");
        System.out.println("3. Aplicacion");
        System.out.println("");
        try {
            System.out.println("¿Qué desea hacer?");
            opcion = in.nextInt();
            System.out.println("");
            
            switch (opcion) {
                case 0:
                    break;
                case 1:
                    inicializaFactory();
                    break;
                case 2:
                    inicializaFactory();
                    eliminarDatos(em);
                    reiniciarDatos(em);
                case 3:
                    System.out.println("Has elegido aplicación");
                    System.out.println("Selecciona lo que desea hacer");
                    System.out.println("1. Insertar objeto");
                    System.out.println("2. Modificar objeto");
                    System.out.println("3. Borrar objeto");
                    System.out.println("4. Realizar consulta");
                    opcion3 = in.nextInt();
                        if (opcion3== 1) {
                            inicializaFactory();
                            Expulsion2OP2.insJPQLT2(em);
                        } else if (opcion3 == 2) {
                            inicializaFactory();
                            Expulsion2OP2.modifJPQLT2(em);
                        } else if (opcion3 == 3) {
                            inicializaFactory();
                            Expulsion2OP2.borradoJPTLT2(em);
                        } else if (opcion3 == 4) {
                            inicializaFactory();
                            Expulsion2OP2.consultaJPTLT2(em);
                        } else {
                            System.out.println("No existe ningún método asignado a ese número");
                            opcion3 = in.nextInt();
                        }
                    
                break;
                
                default:
                System.out.println("Escribe un número entre el 0 y el 3");
            }
        } catch (InputMismatchException e) {
            System.out.println("Inserta un valor válido");
            in.next();
        }
  }
    if (em != null){
        em.close();
        emf.close();
    }
}
public static void inicializaFactory(){
    emf=Persistence.createEntityManagerFactory("./db/proyecto.odb");
    em=emf.createEntityManager();
    System.out.println("Conexión exitosa");
    System.out.println("");
}
public static void eliminarDatos(EntityManager em){
    // Eliminar
    em.getTransaction().begin();
    em.createQuery("DELETE FROM Jugador").executeUpdate();
    em.createQuery("DELETE FROM Partido").executeUpdate();
    em.createQuery("DELETE FROM Expulsion").executeUpdate();
    em.getTransaction().commit();
    System.out.println("");
    System.out.println("Datos eliminados con éxito");
}
public static void reiniciarDatos(EntityManager em){
    //Crear
    em.getTransaction().begin();
    Jugador j1 = new Jugador("8","Griezmann","Atlético de Madrid","8","Delantero");
            em.persist(j1);
    Jugador j2 = new Jugador("23","Pedri","F.C Barcelona","16","Delantero");
            em.persist(j2);
    Jugador j3 = new Jugador("55","Pau Torres","Villarreal","4","Defensa");  
            em.persist(j3);
    Partido p1 = new Partido("442","Atlético de Madrid","Osasuna","Semifinal Copa del Rey","Wanda Metropolitano");
            em.persist(p1);
    Partido p2 = new Partido("452","F.C Barcelona","Betis","Jornada 8 Liga","Camp Nou");
            em.persist(p2);
    Partido p3 = new Partido("447","Sevilla","Villarreal","Jornada 11 Liga","Sanchez Pizjuan");
            em.persist(p3);
    Expulsion e1 = new Expulsion("120","28","Patada en el tobillo",j1,p1);
            em.persist(e1);
    Expulsion e2 = new Expulsion("124","88","Falta por detrás",j2,p2);
            em.persist(e2);
    Expulsion e3 = new Expulsion("126","41","Cabezazo en el pecho",j3,p3);
            em.persist(e3);
    em.getTransaction().commit();
    System.out.println("Los datos han sido reiniciados con éxito");
}

 }

Jugador

package com.mycompany.objectdb;

import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id;

@Entity

public class Jugador implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "IDJUGADOR")
private String idjugador;
@Column(name = "NOMBRE")
private String nombre;
@Column(name = "EQUIPO")
private String equipo;
@Column(name = "DORSAL")
private String dorsal;
@Column(name = "POSICION")
private String posicion;

public Jugador() {
    
}

public Jugador(String idjugador){
    this.idjugador = idjugador;
}

public Jugador(String idjugador, String nombre, String equipo, String dorsal, String posicion) {
    this.idjugador = idjugador;
    this.nombre = nombre;
    this.equipo = equipo;
    this.dorsal = dorsal;
    this.posicion = posicion;
}

public String getIdjugador() {
    return idjugador;
}

public void setIdjugador(String idjugador) {
    this.idjugador = idjugador;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getEquipo() {
    return equipo;
}

public void setEquipo(String equipo) {
    this.equipo = equipo;
}

public String getDorsal() {
    return dorsal;
}

public void setDorsal(String dorsal) {
    this.dorsal = dorsal;
}

public String getPosicion() {
    return posicion;
}

public void setPosicion(String posicion) {
    this.posicion = posicion;
}

@Override
public String toString() {
    return "Jugador{" + "idjugador=" + idjugador +", nombre=" + nombre + ", equipo=" + equipo + ", dorsal=" + dorsal + ", posicion=" +

posicion + '}'; }

}

Thank you!

1

There are 1 best solutions below

0
ObjectDB On

JPA requires listing all the entity classes in a persistence.xml file, so this is the standard solution to make all the entity classes known in queries even before persisting their first instances.

However, the solution suggested by Panagiotis Bougioukos in a comment should also work (and is simpler), i.e. calling em.getMetamodel().entity(Jugador.class); before executing the query, on the same database, and then the class becomes also known in queries and the exception should not be thrown.