错误“javax.persistence.PersistenceException 未找到类型 xxx(错误 301)”使用 ObjectDB (Java) 时
我在学校使用 ObjectDB,在做作业时遇到了问题。
存在问题:
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
------------------------------------------------------------------------
我的 Main 和我的类“Jugador”,这是不存在的引用。
包 com.mycompany.objectdb;
导入 javax.persistence.;导入java.util.;进口 java.text.ParseException;导入 java.text.SimpleDateFormat;进口 java.util.logging.Level;导入java.util.logging.Logger;
公共类 NewMain {
public static EntityManagerFactory emf; 公共静态EntityManager em; 公共静态无效主(字符串[] args){ 扫描仪输入 = new Scanner(System.in); int选项=-1; int option3 = -1; while (选项!= 0) { System.out.println("0.Cerrar 应用程序"); System.out.println("1. 连接 BBDD"); System.out.println("2. Reiniciar 数据"); System.out.println("3.应用"); System.out.println(""); 尝试 { System.out.println("¿Qué desea hacer?"); opcion = in.nextInt(); System.out.println(""); 开关(选项){ 案例0: 休息; 案例1: inializaFactory(); 休息; 案例2: inializaFactory(); 消除Datos(em); reiciarDatos(em); 案例3: System.out.println("有挽歌应用"); System.out.println("选择你想要的"); System.out.println("1.插入对象"); System.out.println("2.修改对象"); System.out.println("3. Borrar 对象"); System.out.println("4.实现咨询"); opcion3 = in.nextInt(); if (选项3== 1) { inializaFactory(); Expulsion2OP2.insJPQLT2(em); } 否则 if (opcion3 == 2) { inializaFactory(); Expulsion2OP2.modifJPQLT2(em); } 否则 if (opcion3 == 3) { inializaFactory(); Expulsion2OP2.borradoJPTLT2(em); } 否则 if (opcion3 == 4) { inializaFactory(); Expulsion2OP2.consultaJPTLT2(em); } 别的 { System.out.println("不存在指定数字的方法"); opcion3 = in.nextInt(); } 休息; 默认: System.out.println("写出数字 0 y el 3"); } } catch (InputMismatchException e) { System.out.println("插入勇气值"); 在.next(); } } 如果(em!= null){ em.close(); emf.close(); } } 公共静态无效 inicializaFactory(){ emf=Persistence.createEntityManagerFactory("./db/proyecto.odb"); em=emf.createEntityManager(); System.out.println("退出连接"); System.out.println(""); } 公共静态无效 elimarDatos(EntityManager em){ // 消除 em.getTransaction().begin(); em.createQuery("从 Jugador 删除").executeUpdate(); em.createQuery("从 Partido 删除").executeUpdate(); em.createQuery("从驱逐中删除").executeUpdate(); em.getTransaction().commit(); System.out.println(""); System.out.println("数据被删除"); } 公共静态无效reiniciarDatos(EntityManager em){ //清除 em.getTransaction().begin(); Jugador j1 = new Jugador("8","格里兹曼","马德里竞技","8","德兰特罗"); em.坚持(j1); Jugador j2 = new Jugador("23","佩德里","巴塞罗那足球俱乐部","16","德兰特罗"); em.坚持(j2); Jugador j3 = new Jugador("55","保罗·托雷斯","比利亚雷亚尔","4","德芬萨"); em.坚持(j3); Partido p1 = new Partido("442","马德里竞技","奥萨苏纳","国王杯半决赛","万达大都会"); em.坚持(p1); Partido p2 = new Partido("452","巴塞罗那足球俱乐部","贝蒂斯","Jornada 8 Liga","诺坎普球场"); em.坚持(p2); Partido p3 = new Partido("447","塞维利亚","比利亚雷亚尔","Jornada 11 Liga","桑切斯·皮斯胡安"); em.坚持(p3); 驱逐 e1 = new 驱逐("120","28","Patada en el tobillo",j1,p1); em.坚持(e1); 驱逐 e2 = new 驱逐("124","88","Falta por detrás",j2,p2); em.坚持(e2); 驱逐 e3 = new 驱逐("126","41","Cabezazo en el pecho",j3,p3); em.坚持(e3); em.getTransaction().commit(); System.out.println("Los datos han sido reiniciados con éxito"); } }
Jugador
包 com.mycompany.objectdb;
导入java.io.Serialized;导入 javax.persistence.Basic;进口 javax.persistence.Column;导入 javax.persistence.Entity;进口 javax.persistence.Id;
@实体
公共类 Jugador 实现可序列化{
私有静态最终长serialVersionUID = 1L; @ID @Basic(可选=假) @Column(名称 = "IDJUGADOR") 私有字符串idjugador; @Column(名称 = "NOMBRE") 私有字符串名称; @Column(名称 = "EQUIPO") 私有字符串设备; @Column(名称 = "DORSAL") 私人弦背; @Column(名称=“位置”) 私有字符串位置; 公共 Jugador() { } 公共 Jugador(字符串 idjugador){ this.idjugador = idjugador; } 公共 Jugador(字符串 idjugador,字符串名称,字符串装备,字符串背侧,字符串位置){ this.idjugador = idjugador; this.nombre = 名称; this.equipo = 设备; this.dorsal = 背侧; this.posicion = 位置; } 公共字符串 getIdjugador() { 返回伊朱加多尔; } 公共无效setIdjugador(字符串idjugador){ this.idjugador = idjugador; } 公共字符串 getNombre() { 返回编号; } 公共无效setNombre(字符串名称){ this.nombre = 名称; } 公共字符串 getEquipo() { 返回设备; } 公共无效setEquipo(字符串equipo){ this.equipo = 设备; } 公共字符串 getDorsal() { 返回背侧; } 公共无效setDorsal(字符串背侧){ this.dorsal = 背侧; } 公共字符串 getPosicion() { 返回位置; } 公共无效setPosicion(字符串位置){ this.posicion = 位置; } @覆盖 公共字符串 toString() { 返回“Jugador{”+“idjugador=”+idjugador+”,nombre=”+ nombre+”,equipo=”+equipo+”,dorsal=”+dorsal+”,posicion=”+
位置+'}'; }
}
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPA 要求在 persistence.xml 文件中列出所有实体类,因此,这是标准解决方案,即使在保留第一个实例之前,也可以在查询中了解所有实体类。
然而,Panagiotis Bougioukos 在评论中建议的解决方案也应该有效(而且更简单),即调用
em.getMetamodel().entity(Jugador.class);
before在同一个数据库上执行查询,然后该类在查询中也变得已知,并且不应引发异常。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.