0

I'm trying to remove a list that is in an embeddable by removing the embeddable from the entity. This isn't working.. All the entities in the list are still in de database.

Code:

Method where it all starts:

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

Ship:

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

   @Embedded
   private Itinerary itinerary;


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

}

Itinerary:

@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

1
  • I've finally figured out what I was doing wrong.. I was always saving a new Itinerary() to the ship. The error actually isn't in this code. For others: The combination of orphanRemoval and cascade.update does the trick.
    – GregD
    Jun 29, 2015 at 14:58

3 Answers 3

0

If the itinerary is already saved to the database, you need to delete it. Eg, something like

public void removeItinerary() {
   getEntityManager().remove(this.itinerary);
   this.itinerary = null;
}
3
  • It's not possible to remove something that isn't an entity.
    – GregD
    Jun 29, 2015 at 14:50
  • @NeilStockton that's great but then the entity manager doesn't know about it. Workarounds are never solutions
    – Gimby
    Jun 29, 2015 at 14:52
  • Then ignore the previous downvote.. But as @Gimby says indeed.. That was what I was going to reply now ^^
    – GregD
    Jun 29, 2015 at 14:53
0

Try to merget the ship entity after the call at removeItinerary method.

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

i think this is not possible... can you try to clear the stops list before you set the itinerary to null... ? (in combination with orphanRemoval on the @JoinColumn...)

!! i have never testet this... !!

something like this:

public void removeItinerary() {
   this.itinerary.stops.clear();
   this.itinerary = null;
}
1
  • I've tried it without succes. Forgot to mention this in the question
    – GregD
    Jun 29, 2015 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.