On the execution of “root.get(“contractId”);” , I get the following error message:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [contractId] on this ManagedType [Contract]
at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:148) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:119) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
I think my mapping is correct because I have used it multiple times for other purposes and it works as expected. If I change the property “contractId” from inside the Properties tag to Outside it, the error no longer shows and the property is found.
So my questions are, why the code “root.get(“contractId”);” can’t find the property with the current mapping, is there a way(without changing the mapping) to retrieve the property to use in the following code:
The <properties/> tag is usually used to declare a virtual property, but the JPA metamodel doesn’t know anything about such virtual properties. So unless the fully qualified property access root.get("uniqueContract").get("contractId") works, you will have to move the property out of the <properties/> tag.
The uniqueContract properties is also not visible when executing root.get("uniqueContract").
So I suppose that the only option is to move it outside the <properties/> tag.
Another question surges, is there a problem repeating the property leaving it inside and outside the tag <properties/>? (I don’t want the lose the uniqueness configured in the properties)
In terms of mapping does it stay almost the same?
The hbm.xml model is very limited regarding such mappings. I would recommend you to switch to annotations. With annotations you can specify dedicated indexes and mark them as unique, so you don’t need these virtual properties for that. You can map properties multiple times, but you will have to make sure only one property is insertable="true" updatable="true"
I would swap the configuration i.e. make the property in the virtual property insert="false" update="false" instead, but other than that, this looks ok to me.