java - Hibernate错误分离实体传递到持久化

标签 java hibernate

所以我试图理解 bean 之间的关系并使用 Hibernate 将我的程序连接到数据库。

我收到此错误,我阅读了有关它的其他一些问题和答案,但仍然不明白如何解决它以及为什么会发生它。

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

我看到了根本原因,但仍然没有找到解决方案。

这是我的代码文件。 我的两个实体 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

}

此处的第二个实体:

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
}

我在相应的 DAOIMpl 类中实现了两个 DAO 接口(interface)。

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;
}

}

我的第二个 DAOImpl 类:

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;
}

}

我的主课

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");


}
}

我使用名为 bateumoussaillonJDBC 的数据库。 在这个数据库中我有两个表:

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;

最佳答案

您使用id创建构造函数,但它在实体类中自动递增。 变化:

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

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

主要内容:

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

关于java - Hibernate错误分离实体传递到持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61290057/

相关文章:

java - 根据字符串优先级减少对象列表

mysql - 用于获取表列数的 hibernate 查询

hibernate - Spring Boot 2.0.0 M6 - 添加 hibernate 拦截器

hibernate - 这种方法是单向的还是双向的

java - hibernate 注释 - 使用 @MappedSuperclass 继承 - 值未设置为基类字段 - 奇怪的错误

java - 编写一个程序,提示输入三角形的边长并报告三个角度

java - 为什么此代码不允许我输入新的内容?

java - 如何在 JAVA 中使用循环创建 FizzBu​​zz

java - 在java中处理n个if-else if的更好方法

java - for循环生成的多线程不能使用synchronized吗?