I have one entity that have two foreign keys on the same table, for example:
class A {
B _source;
B _target;
}
class B {
List<A> _as;
}
My question is how to map all "as" by source and target? I'm using hibernate 4
You cannot map all but you can achieve something like this:
public class A {
@ManyToOne
B _source;
@ManyToOne
B _target;
}
public class B {
@OneToMany(mappedBy = "_source")
List<A> sources;
@OneToMany(mappedBy = "_target")
List<A> targets;
public List<A> getSources() {
return sources;
}
public List<A> getTargets() {
return targets;
}
public List<A> getAll() {
return Stream.concat(getSources().stream(), getTargets().stream()).collect(Collectors.toList());
}
}
with the getAll
method. (You may also need to apply distinct
to that stream)
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.