How to replace @AnyMetaDef in Hibernate6

Hey all,

so my question is regarding the migration from Hibernate 5 to Hibernate 6. Before I state my question, I will give some context about the software in which Hibernate is used.

It’s a Java/Hibernate/PostgreSQL application for book keeping. That application allows the user to keep track of several book keeping related stuff like receipts, employees, working contracts, budgets, etc… As you can all imagine, a lot of paperwork comes with book keeping and that’s why it’s also possible to associate those things with documents. The user can upload any document he wants (.pdf mostly) and connect it to the aforementioned objects (there’s also a lot of other classes which can also be connected to documents, but I want to keep it simple).

So, for this connection we’re using the @AnyMetaDef annotation. We wanted to create an easy to use system for handling documents so that other classes can easily be connected to documents as well as the need arises (easily meaning that the java class only needs to implement a certain interface and no new tables, constraints, etc. are needed).

There is a table for every class (contracts, people, budgets, etc.) in the database as well as a single table containing all documents. The connection between those is modelled by another table “assocs” which contains the ID of the object (every ID is unique DB wide), the ID of the document as well as the type of the object, where the type equals the name of the table in which the object connected to the document is stored. And this is where the @AnyMetaDef annotation comes into play:

@AnyMetaDef(name = "DocuAssocMetaDef", idType = "integer", metaType = "string",
        metaValues = { 
                @MetaValue(targetEntity = Person.class, value = "persons"),
                @MetaValue(targetEntity = Contract.class, value = "contracts"),
                @MetaValue(targetEntity = Budget.class, value = "budgets"),
        }
)

The type column in the assocs table is used to determine to which class the ID in the assoc table belongs.

So my question is: how can we reach the same result in Hibernate 6? We are aware that we can remodel the way we are handling the documents (e.g. don’t let Hibernate handle them but load them via an extra query) but we want to solve this issue with as little remodelling as possible.

I already checked the migration guide which led me to the user guide but both guides didn’t help with my issue.

If you need any more information, just let me know. Thanks in advance.

Did you read the documentation? Just change the mapping:

@Any
@AnyDiscriminator(DiscriminatorType.STRING)
@AnyKeyJavaClass(Integer.class)
@AnyDiscriminatorValue(discriminator = "persons", entity = Person.class)
@AnyDiscriminatorValue(discriminator = "contacts", entity = Contract.class)
@AnyDiscriminatorValue(discriminator = "budgets", entity = Budget.class)
@Column(name = "entity_type")
@JoinColumn(name = "entity_id")