我正在创建一个简单的应用程序,只需在表中插入一行(如果表不存在,创建它)使用
Java JPA.
我已经在google上看过,在这里,我仍然不明白这是什么异常.即使这里有与我同样的问题的问题,但我没有得到解决方案.我还是Java JPA的新手,所以请光着我.
我附加了一些代码来运行它的例子.
回到这个问题,这是我得到的异常和stacktrace:
EXCEPTION -- > org.hibernate.PersistentObjectException: detached entity passed to persist: view.Person javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: view.Person at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187) at view.TestJPA.main(TestJPA.java:34) Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: view.Person at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) ... 1 more
这里是我的代码:
主班:
package view; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class TestJPA { public static void main(String[] args) { Person p = new Person(1,"Peter","Parker"); EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("TesePersistentUnit"); EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); entityManager.persist(p); entityManager.getTransaction().commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } System.out.println("EXCEPTION -- > " + e.getMessage()); e.printStackTrace(); } finally { if (entityManager != null) { entityManager.close(); } } } }
和人类:
package view; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "People") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private String lastName; public Person(int id,String name,String lastName) { this.id = id; this.name = name; this.lastName = lastName; } public Person() { } }
这是我的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="TesePersistentUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>view.Person</class> <properties> <!-- sql dialect --> <property name="hibernate.dialect" value="org.hibernate.dialect.MysqLDialect"/> <property name="javax.persistence.jdbc.url" value="jdbc:MysqL://localhost:3306/tese_tabelas?zeroDateTimeBehavior=convertToNull"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.driver" value="com.MysqL.jdbc.Driver"/> <property name="javax.persistence.jdbc.password" value=""/> <!-- Create/update tables automatically using mapping Metadata --> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
———————–编辑————————– –
我只是将提供程序更改为EclipseLink,而无需进一步更改.我现在很困惑.为什么它与EclipseLink一起工作,但是使用Hibernate会产生异常?
解决方法
尝试使用下面的代码,然后,它将允许您手动设置ID.
只需使用@Id注释即可定义哪个属性是您实体的标识符.如果您不希望hibernate为您生成此属性,则不需要使用@GeneratedValue注释.
分配 – 允许应用程序在调用save()之前为对象分配一个标识符.这是默认策略,如果没有< generator>元素被指定.
package view; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "People") public class Person { @Id //@GeneratedValue(strategy = GenerationType.AUTO) // commented for manually set the id private int id; private String name; private String lastName; public Person(int id,String lastName) { this.id = id; this.name = name; this.lastName = lastName; } public Person() { } }