There is a database with a schema and a table inside with id and url fields, trying to add information (id and url) to the PostgreSQL database via Hibernate, an error occurs:

org.hibernate.PersistentObjectException: detached entity passed to persist: Hibernate.model.UrlModel

UrlModel class:

import javax.persistence.*; @Entity @Table(name = "testbase", schema = "testschema") public class UrlModel { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String url; public UrlModel(int id, String url){ this.id = id; this.url = url; } public UrlModel() {} public int getId() { return id; } public String getUrl() { return url; } public void setId(int id) { this.id = id; } public void setUrl(String url) { this.url = url; } @Override public String toString() { return "UrlModel{" + "id=" + id + ", url='" + url + '\'' + '}'; } } 

TestHibernate class:

 import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class TestHibernate { public static void main(String[] args){ new TestHibernate().startTestHibernate(); } private void startTestHibernate(){ EntityManager entityManager = getEntityManager(); entityManager.getTransaction().begin(); UrlModel urlModel = new UrlModel(1, "https://www.github.com"); entityManager.persist(urlModel); entityManager.getTransaction().commit(); } private static EntityManager getEntityManager() { EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceTest"); return emf.createEntityManager(); } } 

persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="persistenceTest" transaction-type="RESOURCE_LOCAL"> <!--<description>Hibernate EntityManager Demo</description>--> <class>Hibernate.model.UrlModel</class> <!--<exclude-unlisted-classes>true</exclude-unlisted-classes>--> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/> <property name="javax.persistence.jdbc.user" value="postgres"/> <property name="javax.persistence.jdbc.password" value="admin"/> </properties> </persistence-unit> </persistence> 
  • First, add a constructor without parameters, and second, replace int with Integer (including getters and setters). After that, report the results - Dmitry
  • @ Dmitry A. look closely, a constructor without parameters is present! - not a Programmer

1 answer 1

 @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; 

By adding the @GeneratedValue annotation @GeneratedValue you @GeneratedValue Hibernate to generate an identifier. In the same time

 UrlModel urlModel = new UrlModel(1, "https://www.github.com"); 

You independently assign the identity of the entity when it is created. When calling the persist method

 entityManager.persist(urlModel); 

Hibernate sees that the entity already has an identifier and throws an error. All that needs to be done is not to assign a value for the id field and not to use a primitive type for the identifier:

 @Entity public class UrlModel { @Id @GeneratedValue private Long id; private String url; // Этот конструктор будет использовать только Hibernate protected UrlModel() {} public UrlModel(String url) { this.url = url; } // Геттеры, сеттеры // Метод setId() не нужен! }