简体   繁体   中英

JPA mapping: Reusing entities in multiple relationships

Let's say I have:

class Unit {
    private TextContainer source;
    private List<TextContainer> targets;
}

Can I annotate class TextContainer in such a way that it works within both relationships? TextContainer must be either source or target.

You need to annotate the relations with TextContainer in the Unit class. Something like this:

class Unit {

    @ManyToOne(cascade=CascadeType.ALL) 
    private TextContainer source;

    @OneToMany(cascade=CascadeType.ALL) 
    private List<TextContainer> targets;
}

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