简体   繁体   English

Hibernate 错误分离实体传递给坚持

[英]Hibernate Error detached entity passed to persist

SO I am trying to understand the relations between beans and connect my program to a DB using Hibernate.所以我试图了解 bean 之间的关系并使用 Hibernate 将我的程序连接到数据库。

I get this error, I read some other questions and answers about it but still don't understand how to solve it and why it happens.我收到此错误,我阅读了其他一些有关它的问题和答案,但仍然不明白如何解决它以及它为什么会发生。

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.thp.spring.simplecontext.entity.Bateau
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758)
at com.thp.spring.simplecontext.dao.impl.BateauDAOImpl.create(BateauDAOImpl.java:31)
at com.thp.spring.simplecontext.main.AppJavaConfigMain.main(AppJavaConfigMain.java:14)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.thp.spring.simplecontext.entity.Bateau
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:773)
... 3 more

I see the root cause but still not inding the solution.我看到了根本原因,但仍然没有找到解决方案。

Here are the files of my code.这是我的代码文件。 My two entities Moussaillon and Bateau:我的两个实体 Moussaillon 和 Bateau:

package com.thp.spring.simplecontext.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "moussaillon")
public class Moussaillon {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "config")
private String config;

/*
 * Many to one ralation This means that moussaillon entity will have a foreign
 * key column named "bateau_id" referring to primary attribute id of entity
 * bateau
 */
@ManyToOne
@JoinColumn(name = "bateau_id")
private Bateau bateau;

public Moussaillon() {
}

public Moussaillon(String firstName, String lastName, String config, Bateau bateau) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.config = config;
    this.bateau = bateau;
}

//getters and setters are automatically created here

}

Second entity here:这里的第二个实体:

package com.thp.spring.simplecontext.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
 import javax.persistence.Table;

@Entity
@Table(name = "bateau")
public class Bateau {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String nom;
@Column(name = "type")
private String type;
@Column(name = "size")
private double taille;
@OneToMany(mappedBy="bateau")
private List<Moussaillon> moussaillons;

public Bateau() {

}

public Bateau(int id, String nom, String type, double taille) {
    super();
    this.id = id;
    this.nom = nom;
    this.type = type;
    this.taille = taille;
}

//getters and setters are automatically created here
}

I have two DAO interfaces implemented in the corresponding DAOIMpl classes.我在相应的 DAOIMpl 类中实现了两个 DAO 接口。

package com.thp.spring.simplecontext.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.thp.spring.simplecontext.dao.BateauDAO;
import com.thp.spring.simplecontext.entity.Bateau;
import com.thp.spring.simplecontext.entity.Moussaillon;


@Repository
public class BateauDAOImpl implements BateauDAO{

EntityManagerFactory emf=Persistence.createEntityManagerFactory("persistence");
EntityManager em=emf.createEntityManager();

@Override
public int create(int id, String name, String type, double taille) {

    return 0;
}

@Override
public int create(Bateau bateau) {
    em.persist(bateau);
    return 0;
}

@Override
public Bateau update(Bateau bateau, String name, String type, double taille) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void delete(Bateau bateau) {
    // TODO Auto-generated method stub

}

@Override
public Bateau findById(int id) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Bateau findByMoussaillon(Moussaillon moussaillon) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Bateau> findAll() {
    // TODO Auto-generated method stub
    return null;
}

}

My second DAOImpl class:我的第二个 DAOImpl class:

package com.thp.spring.simplecontext.dao.impl;

//al

@Repository
public class MoussaillonDAOImpl implements MoussaillonDAO {

EntityManagerFactory emf=Persistence.createEntityManagerFactory("persistence");
EntityManager em=emf.createEntityManager();


@Override
public int create(Moussaillon moussaillon) {
    System.out.println("Starting Transaction");
    em.getTransaction().begin();
    em.persist(moussaillon);

    return 0;
}

@Override
public Moussaillon update(Moussaillon moussaillon, String firstName, String lastName, String config) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void delete(Moussaillon moussaillon) {
    // TODO Auto-generated method stub

}

@Override
public Moussaillon findById(int id) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Moussaillon> findAll() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Moussaillon> findAllByBateau(int bateauId) {
    // TODO Auto-generated method stub
    return null;
}

} }

My main class我的主class

package com.thp.spring.simplecontext.main;

import com.thp.spring.simplecontext.dao.impl.BateauDAOImpl;
import com.thp.spring.simplecontext.dao.impl.MoussaillonDAOImpl;
import com.thp.spring.simplecontext.entity.Bateau;
import com.thp.spring.simplecontext.entity.Moussaillon;

public class AppJavaConfigMain {
    public static voidmain(String[] args){
    Bateau b1=new Bateau(3,"carthage","barque",10);
    BateauDAOImpl b=new BateauDAOImpl();
    b.create(b1);
    Moussaillon m1 = new Moussaillon();

    m1.setFirstName("gy");
    m1.setLastName("yg");
    m1.setConfig("java");
    m1.setBateau(b1);       //m1.setId(1);
    MoussaillonDAOImpl mdao = new MoussaillonDAOImpl();
    System.out.println("persist new moussaillon");
    mdao.create(m1);
    System.out.println("persist finished");


}
}

I use a DataBase named bateaumoussaillonJDBC.我使用一个名为 bateaumoussaillonJDBC 的数据库。 In this DB I have two Tables:在这个数据库中,我有两个表:

CREATE DATABASE bateaumoussaillonJDBC;

CREATE TABLE bateaumoussaillonJDBC.bateau(
id int NOT NULL,
name CHAR(80) NOT NULL,
type CHAR(80) NOT NULL,
size DOUBLE NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB;

CREATE TABLE bateaumoussaillonJDBC.moussaillon(
id int NOT NULL AUTO_INCREMENT,
first_name CHAR(80) NOT NULL,
last_name CHAR(80) NOT NULL,
config CHAR(80) NOT NULL,
bateau_id INT NOT NULL,
PRIMARY KEY(id),
CONSTRAINT FK_BateauMoussaillon FOREIGN KEY (bateau_id)
REFERENCES bateaumoussaillonJDBC.bateau(id)
)ENGINE=InnoDB;

You create constructor with id but it is auto increment in your entity class.您使用id创建构造函数,但它在您的实体 class 中自动递增。 Change:改变:

public Bateau(int id, String nom, String type, double taille) {
    super();
    this.id = id;
    this.nom = nom;
    this.type = type;
    this.taille = taille;
}

To

public Bateau(String nom, String type, double taille) {
    this.nom = nom;
    this.type = type;
    this.taille = taille;
}

And in main:主要是:

 Bateau b1=new Bateau("carthage","barque",10); // without id number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM