1

次のエンティティがあります。CarとManyToManyの関係がありHumanます。ユーザーの間で補助クラスAssignを使用しています

@Entity
public class Car implements Serializable
{
   //...

   @LazyCollection(LazyCollectionOption.TRUE)
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "car")
   private Set<Assign> cars = new HashSet<Assign>();
   //...
}  

@Entity
class Assign implements Serializable 
{
   //...

   @LazyCollection(LazyCollectionOption.FALSE)
   @ManyToOne
   @JoinColumn(name = "HUMAN_CAR", nullable = false)
   private Human human;

   @LazyCollection(LazyCollectionOption.FALSE)
   @ManyToOne
   @JoinColumn(name = "CAR_HUMAN", nullable = false)
   private Car car;
   //..

}  

@Entity
public class Human implements Serializable
{
    //...

   @LazyCollection(LazyCollectionOption.TRUE)
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "human")
   private Set<Assign> cars = new HashSet<Assign>();  
   // ...  
}

今、私はコンテナ管理トランザクション内の車を削除しようとしています

   public void deleteCar(final long id)
   {      
      final Car car = entityManager.find(roleId, Car.class);
      entityManager.remove(car);  
   }  

しかし、私は得る

Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.dto.Assign#<null>]
4

2 に答える 2

1

を削除する前に、まず削除するcar必要があります。carAssign

ここには別の設計上の問題があります。これは、Car とその担当者が構成関係 (単なる集約/関連付け関係ではなく) を持っていることを意味しますCascadeType.ALL。一方、人間とそのアサイメントの間にも同様の関係が存在します。割り当ては明らかに車と人間の間で共有できますが、合成関係は非共有セマンティクスに従います。車を削除し、その割り当てをカスケード削除すると、サイドで共有されているものは孤立します。carsCarcarsperson

于 2013-01-25T14:09:23.043 に答える
1

削除するAssign前に -s を削除しますCar

public void deleteCar(final long id)
{      
  final Car car = entityManager.find(roleId, Car.class);
  for(Assign assign : car.getAssigns()) {
     entityManager.remove(assign);
  }
  entityManager.remove(car);  
} 

ゲッターの名前が異なる可能性があります。そのコードはわかりません。

于 2013-01-25T14:17:16.450 に答える