Home » Java » Cannot pass object to database;detached entity passed to persist

Cannot pass object to database;detached entity passed to persist

Posted by: admin October 18, 2018 Leave a comment


I’m new in jpa and hibernate, and it took me a while to set up the xml file(it might not be whole), lower the errors, and try to figure it all out. I’m using mariadb and have a database called jpadb, which im trying create and pass an “Employee” object from java. I get the error

:org.hibernate.PersistentObjectException: detached entity passed to
persist: model.Employee

my code is as follows:


<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"

    <persistence-unit name="sample" transaction-type="RESOURCE_LOCAL">



            <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/jpadb" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />




<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





package model;
import javax.persistence.*;

@Table (name="user")
public class Employee {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int eid;

    @Column(name = "ename")
    private String ename;

    @Column(name = "salary")
    private double salary;

    @Column(name = "deg")
    private String deg;

    public Employee(int eid, String ename, double salary, String deg) {
        super( );
        this.eid = eid;
        this.ename = ename;
        this.salary = salary;
        this.deg = deg;

    public Employee( ) {

    public int getEid( ) {
        return eid;

    public void setEid(int eid) {
        this.eid = eid;

    public String getEname( ) {
        return ename;

    public void setEname(String ename) {
        this.ename = ename;

    public double getSalary( ) {
        return salary;

    public void setSalary(double salary) {
        this.salary = salary;

    public String getDeg( ) {
        return deg;

    public void setDeg(String deg) {
        this.deg = deg;

    public String toString() {
        return "Employee [eid=" + eid + ", ename=" + ename + ", salary=" + salary + ", deg=" + deg + "]";


package service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.Employee;

public class CreateEmployee {

    public static void main( String[ ] args ) {

        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "sample" );

        EntityManager entitymanager = emfactory.createEntityManager( );
        entitymanager.getTransaction( ).begin( );

        Employee employee = new Employee( );
        employee.setEid( 1201 );
        employee.setEname( "Maria" );
        employee.setSalary( 40000 );
        employee.setDeg( "Technical Manager" );

        entitymanager.persist( employee );
        entitymanager.getTransaction( ).commit( );

        entitymanager.close( );
        emfactory.close( );

I’m trying to go through tutorials, but due to hibernate-property version mismatches I’m having a really hard time. It took me a while to find the properties for mariadb in order for this to work. Thanks in advance.