The error occurs because the object’s ID is set. Hibernate distinguishes between transient and detached objects and persist
works only with transient objects. If persist
concludes the object is detached (which it will because the ID is set), it will return the “detached object passed to persist” error. You can find more details here and here.
However, this only applies if you have specified the primary key to be auto-generated: if the field is configured to always be set manually, then your code works.
Related Contents:
- java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities
- How can I avoid the Warning “firstResult/maxResults specified with collection fetch; applying in memory!” when using Hibernate?
- Difference between a “jta-datasource” and a ” resource-local ” datasource?
- use of entityManager.createNativeQuery(query,foo.class)
- How can I make a JPA OneToOne relation lazy
- How to call a stored procedure from Java and JPA
- Spring Boot + JPA : Column name annotation ignored
- How to create and handle composite primary key in JPA
- Confusion: @NotNull vs. @Column(nullable = false) with JPA and Hibernate
- Spring Boot – Cannot determine embedded database driver class for database type NONE
- How to store Java Date to Mysql datetime with JPA
- Setting default values for columns in JPA
- Hibernate: Automatically creating/updating the db tables based on entity classes
- Configure hibernate (using JPA) to store Y/N for type Boolean instead of 0/1
- How to properly express JPQL “join fetch” with “where” clause as JPA 2 CriteriaQuery?
- JPA/Hibernate Native Queries do not recognize Parameters
- How to solve the LazyInitializationException when using JPA and Hibernate
- Hibernate – @ElementCollection – Strange delete/insert behavior
- How do I update an entity using spring-data-jpa?
- JPA OneToMany not deleting child
- What is the easiest way to ignore a JPA field during persistence?
- No Dialect mapping for JDBC type: 1111
- Spring CrudRepository findByInventoryIds(List inventoryIdList) – equivalent to IN clause
- How to remove entity with ManyToMany relationship in JPA (and corresponding join table rows)?
- What is the difference between Unidirectional and Bidirectional JPA and Hibernate associations?
- When to use EntityManager.find() vs EntityManager.getReference() with JPA
- Getting Database connection in pure JPA setup
- JPA getSingleResult() or null
- JPA, How to use the same class (entity) to map different tables?
- Why does Hibernate disable INSERT batching when using an IDENTITY identifier generator
- Java8 Collections.sort (sometimes) does not sort JPA returned lists
- No Persistence provider for EntityManager named X
- Can I combine a @Query definition with a Specification in a Spring Data JPA repository method?
- Eager / auto loading of EJB / load EJB on startup (on JBoss)
- How to use @Transactional with Spring Data?
- JPA or JDBC, how are they different?
- Specifying an Index (Non-Unique Key) Using JPA
- Deploying application using Hibernate JPA 2.1 to IBM WebSphere gives NullPointerException
- Is there such thing CASE expression in JPQL?
- MappedSuperclass – Change SequenceGenerator in Subclass
- Multiple fetches with EAGER type in Hibernate with JPA
- persistence.xml different transaction-type attributes
- How to map a map JSON column to Java Object with JPA
- Spring JpaRepository – Detach and Attach entity
- spring data jpa @query and pageable
- Spring Data JPA and Exists query
- JPA: difference between @JoinColumn and @PrimaryKeyJoinColumn?
- When to use DiscriminatorValue annotation in hibernate
- Why do we use @Embeddable In Hibernate
- Adding entity classes dynamically at runtime