0

I have a polymorphic relationship represented by the interface commentable. Comments can target posts as well as other comments.

I'm able to represent the comment side of that relationship, retrieving the target post, but I'm unable to represent the post side- e.g. I can't get the comments from the post.

I'm using @Any for polymorphism, but if that's not the correct way to do it, then I'll happily change directions.

If I just have the Comment code, things work fine

// Comment.java
@Any(
        metaDef = "CommentableMetaDef",
        metaColumn = @Column(name = "commentable_entity")
)
@JoinColumn(name = "commentable_id")
private Commentable target;
// Post.java
@OneToMany(mappedBy = "target", targetEntity = Comment.class)
private Set<Comment> comments;

^^ This breaks.

In case it's relevant, here's my metaDef inside of a package-info.java

// package-info.java
@AnyMetaDef(name = "CommentableMetaDef", metaType = "string", idType = "int",
        metaValues = {
                @MetaValue(value = "COM", targetEntity = Comment.class),
                @MetaValue(value = "POS", targetEntity = Post.class)
        }
)
@AnyMetaDef(name = "ReactableMetaDef", metaType = "string", idType = "int",
        metaValues = {
                @MetaValue(value = "COM", targetEntity = Comment.class),
                @MetaValue(value = "POS", targetEntity = Post.class)
        }
)
Pinwheeler
  • 1,061
  • 2
  • 13
  • 26
  • 1
    I am afraid that `@Any` can be only unidirectional. See the [documentation](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#associations-any). – SternK Sep 05 '20 at 05:16
  • 1
    I think [this answer](https://stackoverflow.com/a/63417912/1092818) might be helpful – crizzis Sep 05 '20 at 09:19
  • @crizzis indeed- that solved my issue. Thanks for posting! – Pinwheeler Sep 08 '20 at 16:56

0 Answers0