删除嵌入字段(JPA)

时间:2015-06-29 14:37:51

标签: java jpa

我正在尝试通过从实体中删除嵌入来删除可嵌入的列表。这不起作用。列表中的所有实体仍在de数据库中。

代码:

一切都开始的方法:

 //attached entity
 Ship ship = findShip();
 ship.removeItinerary();

船舶:

@Entity
@Table(name = "SHIP")
public class Ship extends Domain {

   @Embedded
   private Itinerary itinerary;


   public void removeItinerary() {
      this.itinerary = null;
   }    

}

行程:

@Embeddable
public class Itinerary implements Serializable {

   //tried orphanRemoval and cascade but without luck 
   //(since I'm not removing the Ship it's actually logic that it's not working..)
   @OneToMany
   @JoinColumn(name = "SHIP_ID", referencedColumnName = "ID")
   private List<Stop> stops = new ArrayList<>();

}

JPA 2.0

3 个答案:

答案 0 :(得分:0)

如果行程已保存到数据库,则需要delete。例如,

public void removeItinerary() {
   getEntityManager().remove(this.itinerary);
   this.itinerary = null;
}

答案 1 :(得分:0)

尝试在removeItinerary方法调用后合并ship实体。

Ship ship = findShip();
ship.removeItinerary();
em.merge(ship).

答案 2 :(得分:0)

我认为这是不可能的...你可以在将行程设置为空之前尝试清除停止列表吗? (与@JoinColumn上的orphanRemoval结合使用......)

!!我从来没有试过这个...... !!

类似的东西:

public void removeItinerary() {
   this.itinerary.stops.clear();
   this.itinerary = null;
}