Reputation: 1197
o-o!
I am trying to map the wp_posts table from WordPress database. I have left out information to simplify my question:
This is the entity:
@Entity
@Table(name = "wp_posts")
@Getter
@Setter
public class WPPosts {
@Id
@Column(name = "ID")
private long id;
@Column(name = "post_parent")
private long postParent;
@OneToMany(targetEntity = WPPosts.class, mappedBy = "postParent")
List<WPPosts> childPosts;
}
An example for the table could be (again: simplified!):
id --- post_parent
1 ---- null
2 ---- 1
3 ---- 1
4 ---- null
So entity with id 1 should get a list childPosts of size 2 (containing post 2 and 3).
Unfortunately this does not work. I do not get any exception, but the list stays empty.
Upvotes: 0
Views: 41
Reputation: 10716
mappedBy
is supposed to be referring to an entity, not an id. You need a @ManyToOne WPPosts postParent
field for your mapping to work.
Note that with such a mapping, WPPosts.postParent
is the owning side of the relationship, meaning that only changes to WPPosts.postParent
will be honored by JPA; changes to WPPosts.childPosts
will be ignored.
As far as accessing the list goes, you'll want to ensure to either only access the list within a transaction context, or declare the @OneToMany
as fetch=FetchType.EAGER
. Otherwise, you'll get lazy initialization exceptions.
Upvotes: 1