# @OneToOne to Interface

**URL:** <https://discourse.hibernate.org/t/onetoone-to-interface/5436>\
**Category:** Hibernate ORM\
**Created:** [June 16, 2021, 8:13am UTC](https://discourse.hibernate.org/t/onetoone-to-interface/5436 "2021-06-16T08:13:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [June 16, 2021, 8:13am UTC](https://discourse.hibernate.org/t/onetoone-to-interface/5436/1 "2021-06-16T08:13:19Z")

</div>

Hello,

I have a solution to deal with this case but I’m not sure it’s correct or there is an easier solution.  
I am using the @Any annotation …  
Is that correct in your opinion?

Otherwise I could declare the interface as an @Entity but I don’t know all the classes that implement the interface

```auto
public class Contact extends PersonnePhysique {
  @Any(metaDef = "ACTEUR", metaColumn = @Column(name = "OBJETMAITRE_ROLE"))
  @JoinColumn(name = "OBJETMAITRE_ID", columnDefinition = "varchar2(25 char)")
  private IPossedeContacts objetMaitre;
...
}

public interface IPossedeContacts
}

```

---

<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:** [June 17, 2021, 7:06am UTC](https://discourse.hibernate.org/t/onetoone-to-interface/5436/2 "2021-06-17T07:06:21Z")

</div>

What do you mean you don’t know the classes? AFAIK you have to specify all classes in the `@AnyMetaDef(metaValues)`, so you have to know the classes. You could also use the table per class inheritance strategy for which you really don’t need to specify all possible tables/entities, but the final SQL query for the data will then use a big union over all possible tables. Not sure if that is acceptable for you. Anyway, I have no idea what your exact requirement is, so anything could be correct. The mapping looks ok to me though.

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [June 17, 2021, 8:44am UTC](https://discourse.hibernate.org/t/onetoone-to-interface/5436/3 "2021-06-17T08:44:40Z")

</div>

Ah if sorry, I did not put everything, I know the list of classes, see below.

But I was wondering what the most common practice is for an interface.  
Ok for the interface, it works with @Any.

But for inheritance, we should not use the interface, right?

```auto
@AnyMetaDef(
  name = "ACTEUR",
  idType = "string",
  metaType = "string",
  metaValues = {
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Acteur", targetEntity = Acteur.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.ActeurGenerique", targetEntity = ActeurGenerique.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.ActeurInterne", targetEntity = ActeurInterne.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.AgenceBancaire", targetEntity = AgenceBancaire.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Agent", targetEntity = Agent.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Collectivite", targetEntity = Collectivite.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Commune", targetEntity = Commune.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Contact", targetEntity = Contact.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.EntiteAdministrative", targetEntity = EntiteAdministrative.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Fournisseur", targetEntity = Fournisseur.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.GestionnaireConcession", targetEntity = GestionnaireConcession.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Intercommunalite", targetEntity = Intercommunalite.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.Partenaire", targetEntity = Partenaire.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.PersonneMorale", targetEntity = PersonneMorale.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.PersonneMoraleAbstraite", targetEntity = PersonneMoraleAbstraite.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.PersonnePhysique", targetEntity = PersonnePhysique.class),
    @MetaValue(value = "com.hermes.ref.acteur.businessobject.SignataireDestinataire", targetEntity = SignataireDestinataire.class)
  }
)

```

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [June 21, 2021, 7:36am UTC](https://discourse.hibernate.org/t/onetoone-to-interface/5436/4 "2021-06-21T07:36:16Z")

</div>

I have defined the targetEntity with the parent class “Acteur.class” which contains all the classes that implement the IPossedeContacts interface. It works.

So I was able to remove the @Any to use the @ManyToOne.

```auto
  @ManyToOne(targetEntity = Acteur.class)
  @JoinColumn(table = "TPERSONNEPHYSIQUE", name = "OBJETMAITRE_ID")
  private IPossedeContacts objetMaitre;

```
