在jpa 2.1中使用ManyToMany时的分离实体

问题描述 投票:1回答:1

我正在创建一个应用程序,我在RecipeIngredients之间有一个ManyToMany关系,但是当我尝试使用JPA 2.1在RecipeIngredient中插入新行时,我得到了:

引起:javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:传递给persist的分离实体。

这是我的实体:`

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@CrossCheck
public class Ingredient implements ValidEntity {

static final String PREFIX = "com.architech.sales.ingredient.entity.";
public static final String FIND_ALL = PREFIX + "FIND_ALL";
public static final String FIND_BY_ID = PREFIX + "FIND_BY_ID";

@Id
@GeneratedValue
private long id;

@NotNull
private String label;
private String description;

// added just to reply to @Maciej-Kowalski
@OneToMany(targetEntity = RecetteIngredient.class, mappedBy = "ingredient")
private List<RecetteIngredient> recetteIngredients;

@Version
private long version;
// Getter / Setter

}

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@CrossCheck
public class Recette implements ValidEntity{

static final String PREFIX = "sales.recette.entity.Recette.";
public static final String FIND_ALL = PREFIX + "FIND_ALL";
public static final String FIND_BY_ID = PREFIX + "FIND_BY_ID";

@Id
@GeneratedValue
private long id;

@Size(min = 1, max = 256)
private String label;
private String description;

@OneToMany(targetEntity = RecetteIngredient.class, mappedBy = "recette")
private List<RecetteIngredient> recetteIngredients;

@Version
private long version;

// Getter/Setter
}
`

`

@Entity
@IdClass(RecetteIngredientPK.class)
public class RecetteIngredient implements Serializable{

@Id
private long id;
private double qte;

@Id
@ManyToOne(targetEntity = Ingredient.class)
private Ingredient ingredient;

@Id
@ManyToOne(targetEntity = Recette.class)
private Recette recette;

// Getter/Setter
}
`

和我的持久单位

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="prod" transaction-type="JTA"> <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <!--<property name="hibernate.enable_lazy_load_no_trans" value="true" />--> </properties> </persistence-unit> </persistence>

这是堆栈跟踪:

`

    Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.architech.sales.ingredient.entity.Ingredient
    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.merge(AbstractEntityManagerImpl.java:1171)
    at org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:565)
    at com.architech.sales.recette_ingredient.boundary.RecetteIngredientManager.save(RecetteIngredientManager.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
`

这就是我试图坚持Recette Ingredient的方式:

`
@Stateless
@Interceptors(BoundaryLogger.class)
public class RecetteIngredientManager {

@PersistenceContext
EntityManager em;

public RecetteIngredient findById(long id) {
    return this.em.find(RecetteIngredient.class, id);
}

public void delete(long id) {
    try {
        RecetteIngredient reference = this.em.getReference(RecetteIngredient.class, id);
        this.em.remove(reference);
    } catch (EntityNotFoundException e) {
        //we want to remove it...
    }
}

public RecetteIngredient save(RecetteIngredient recetteIngredient) {
    return this.em.merge(recetteIngredient);
}

}

`

java hibernate jpa many-to-many jpa-2.1
1个回答
0
投票

在您的链接类中,在@ManyToOne映射上添加merge cascade选项:

@Entity
@IdClass(RecetteIngredientPK.class)
public class RecetteIngredient implements Serializable{

@Id
private long id;
private double qte;

@Id
@ManyToOne(targetEntity = Ingredient.class, cascade = CascadeType.MERGE)
private Ingredient ingredient;

@Id
@ManyToOne(targetEntity = Recette.class, cascade = CascadeType.MERGE)
private Recette recette;

这将建议PersistenceContext同时合并Ingredient和Recette实体,如果它们恰好在当时被分离。

如果您不允许更改注释,则必须在持久化链接表之前手动保留/合并Ingredient和Recette实体:

public RecetteIngredient save(RecetteIngredient recette) {
  this.em.merge(recette.getIngredient());
  this.em.merge(recette.getRecette());

  return this.em.merge(recette);
}

当然这是一种解决方法,第一种方法肯定更优雅,更可取。

© www.soinside.com 2019 - 2024. All rights reserved.