简体   繁体   中英

JPA 2.1 ManyToOne, delete child (one side) if is last entity

An Article has a unidirectional ManyToOne relationship to Medium , which is working fine with the code below: saving and deleting articles succeeds.

I wonder if JPA has an elegant way to delete the last child entity, medium in this case. Atm, if I delete all articles with a specific medium, the medium is not removed from the DB. I could check manually of course, but I presume there's a better way.

@Entity
public class Article {

    @ManyToOne(cascade = CascadeType.PERSIST)
    private Medium medium; // e.g. media website, facebook, twitter...

}


@Entity
public class Medium {

    ...

}

Have a look at this question . In short it is not possible because because JPA does not know about the number of children, thus you have to check it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM