# Bidirectional @Any mapping?

**URL:** <https://discourse.hibernate.org/t/bidirectional-any-mapping/6893>\
**Category:** Hibernate ORM\
**Created:** [November 13, 2022, 8:38pm UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893 "2022-11-13T20:38:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stdh](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@stdh](https://discourse.hibernate.org/u/stdh)\
**Post date:** [November 13, 2022, 8:38pm UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893/1 "2022-11-13T20:38:44Z")

</div>

Is there a way to define the inverse of an `@Any` mapping, some kind of `@AnyToMany?`  
I’ve been looking for this for some time, but I may be missing something obvious.

The model concerns Tasks that need to be processed. These tasks contain Subtasks, and the processing may generate TaskErrors, on the level of both Tasks and Subtasks. It looks something like this:

```auto
public interface Excepted { ... }

@Entity
public class TaskException {
    @Any
    @AnyDiscriminator(DiscriminatorType.STRING)
	@AnyDiscriminatorValues({
			@AnyDiscriminatorValue(discriminator = "Task",
                                   entity = Task.class),
			@AnyDiscriminatorValue(discriminator = "SubTask",
                                   entity = SubTask.class)})
    @AnyKeyJavaClass(String.class)
    @Column(name = "excepted_type")
    @JoinColumn(name = "excepted_id")
	private Excepted excepted;

    ...
}

@Entity
public class Task implements Excepted {
    @OneToMany(mappedBy = "excepted")
    private List<TaskException> exceptions =
        new ArrayList<TaskException>();

    ...
}

@Entity
public class SubTask implements Excepted {
    @OneToMany(mappedBy = "excepted")
    private List<TaskException> exceptions =
        new ArrayList<TaskException>();

    ...
}

```

`TaskException.excepted` works, but the naive `@OneToMany` doesn’t, it yields a MappingException ( _collection foreign key mapping has wrong number of columns_). I have the feeling that the solution can’t be that hard, the SQL query to fetch a (Sub)Task’s exceptions is trivial. Some black magic involving `@Join...` may solve this, but I hope there’s a clean and compact annotation I’ve overlooked.

–

Some background: the application I’m working on is mostly written in PHP using Laravel, but we need to do some SOAP communication that we only know how to do in Java. The easiest solution we could think of is to have the PHP code write request data to the database, have a small Java program read these data and send the requests over SOAP and write the responses back to db, and further process the responses in PHP. So I need to design a database model that would be approached from two sides: by Eloquent (Laravel’s ORM) and by Hibernate. The bidirectional `@Any` I’m looking for is quite straightforward in Eloquent (morphMany method of the Model class), I would like to translate this to Java. 🙂

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 14, 2022, 9:18am UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893/2 "2022-11-14T09:18:53Z")

</div>

You can create a feature request for this. In the meantime, you just need to use the following annotations instead:

```java

@Entity
public class Task implements Excepted {
    @OneToMany
    @JoinColumn(name = "excepted_id")
    private List<TaskException> exceptions =
        new ArrayList<TaskException>();

    ...
}

```

---

<div class="post-metadata">

**Author:** ![stdh](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@stdh](https://discourse.hibernate.org/u/stdh)\
**Post date:** [November 14, 2022, 10:27pm UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893/3 "2022-11-14T22:27:51Z")

</div>

Thanks beikov, for the fast reply! Sure is a lot more compact than my rambling.

Anyway, this is almost what I need. The number of columns is correct now. But Task.exceptions now also contains the exceptions of a SubTask that happens to have the same primary key value. If I add `@Where(clause = "excepted_type = 'Task'")` it seems to work as intended. That’s the cleanest solution at this moment, right?

I’ll try to create a feature request tomorrow.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 15, 2022, 9:03am UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893/4 "2022-11-15T09:03:12Z")

</div>

Yes, that’s as good as it gets for now.

---

<div class="post-metadata">

**Author:** ![stdh](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@stdh](https://discourse.hibernate.org/u/stdh)\
**Post date:** [November 15, 2022, 3:36pm UTC](https://discourse.hibernate.org/t/bidirectional-any-mapping/6893/5 "2022-11-15T15:36:33Z")

</div>

The feature request is [here](https://hibernate.atlassian.net/browse/HHH-15722). Thanks again for your kind help!
