1

I have a department table having below structure

Field       Type            Null    Key     Default     Extra
id          int(11)         NO      PRI                 auto_increment
name        varchar(45)     NO      UNI     
description varchar(100)    YES         

Note that id is auto_increment(auto number) and the database that I'm using is MySQL.

During insert, if I provide the the value for id it takes that id and inserts a record. Below statement insert a record with id=9.

insert into department values(9, "dept09", "department 09");

If I do not provide the value for id, it takes the next value and inserts a record. In this case id would be 10 . Below statement inserts a record with id=10.

insert into department(dept_name, description) values("dept10", "department 10");

My Department class in Java looks like below. I'm using hibernate/JPA as persistence provider I'm using GenerationType.IDENTITY as strategy to generate the identifier for the object.

@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private String description;
....

}

I'm persisting department as below:

// create and persist an Department
em.getTransaction().begin();
Department dept = new Department();
dept.setId(13);
dept.setName("dept13");
dept.setDescription("Department 13");
em.persist(dept);
em.getTransaction().commit();

Since I'm providing the value for id as 13, I'm expecting that a record with id=13 would be persisted in database, but hibernate/JPA is giving the error below:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.mycompany.app.Department
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
    at com.mycompany.services.DepartmentService.createDepartment(DepartmentService.java:21)

Why is it saying the newly created department as detached entity? If I comment out "dept.setId(13)", it works fine and a department with (max id value in DB)+1 as id value.

Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

0

As per you entity department you have id field marked as identity with generation type identity. When hibernate see this while inserting it will automatically increment the id by 1. If you don't want this you have to remove the generation auto thing and then you can setid to what ever you need. The below link gives you more information about how to choose the generation type in hibernate

How to choose the id generation strategy when using JPA and Hibernate

Community
  • 1
  • 1
LearningPhase
  • 1,277
  • 1
  • 11
  • 20
  • I want hibernate to provide the id(max id +1) if the id is not provided or is 0. But if the id is provided, I want hibernate to use that. This is exactly the behavior of MySQL. – Dinesh Ranawat Apr 24 '16 at 11:13
  • I don't know mate if this is possible as I am also a beginner in hibernate. Can you please post a new question asking this. Someone with more experience might have a answer for this. – LearningPhase Apr 24 '16 at 13:32