Mapping issue-upgrading hibernate from 5.6.15 to 6.5.2 – Component’s property with same name as one of entity’s property was not mapped in new version

  Kiến thức lập trình

Facing the below issue with ORM mapping of hbm.xml files after upgrading from 5.6.15 to 6.5.2

Below are the the POJOs

public class IDEntity
{
    protected T id;
    public T getId() {return id;}
    public void setId(T id){ this.id = id; }
}

public class Address extends IDEntity 
{
    protected String address;
    protected String postalCode;
    protected String town;

    public String getAddress() { return address1; }
    public void setAddress(String address1) {this.address1 = address1;}
    public String getPostalCode(){return postalCode;}
    public void setPostalCode(String postalCode){this.postalCode = postalCode;}
    public String getTown(){return town;}
    public void setTown(String town){this.town = town;}
}

public class Account extends IDEntity 
{
    protected String accountName;
    public String getAccountName(){return accountName;}
    public void setAccountName(String accountName){this.accountName = accountName;}
}

public class User extends IDEntity 
{
    protected String userName;
    protected Address addr;
    protected Account acct;

    public String getUserName(){return userName;}
    public void setUserName(String userName){this.userName = userName;}
    public String getAddr(){return addr;}
    public void setAddr(String addr){this.addr = addr;}
    public String getAcct(){return acct;}
    public void setAcct(String acct){this.acct = acct;}
}


Below is the User.hbm.xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="org.example.User" table="payment" polymorphism="explicit">
      <id name="id" type="string" column="id" length="64">
            <generator class="org.example.KeyGenerator">
            </generator>
      </id>
      
      <property name="userName" column="user_name" type="string" length="64"/>
      
      <component name="addr" class="org.example.Address">
        <property column="address_id" name="id" type="string" length="64"/>
      </component>

      <component name="acct" class="org.example.Account">
        <property column="account_id" name="id" type="string" length="64"/>
      </component>

   </class>
</hibernate-mapping>

Issue:
After parsing User.hbm.xml, User’s id and addr are missing from the mapping. However, acct was added with this component’s property name – id. CriteriaAPI queries that were running fine with previous version (5.6.15) are failing with the new version because of the missing component names (addr and acct).
Error thrown: org.hibernate.query.sqm.PathElementException: Could not resolve attribute ‘addr’ of ‘org.example.Account’

I had gone through the documentation and searched in different forums and SO but didn’t find any documentation on this or posts with similar issue.

I debugged and found declaredSingularAttributes of AbstractManagedType does not have these mappings. Attributes are added to declaredSingularAttributes with name as the key.

All the attributes are considered normal properties of User, even the properties under Components. User’s id, addr, and acct all have the property name ‘id’, hence only one mapping exists, the latest parsed attribute.

Is there any workaround to this issue? Also, is this a bug in the newer versions from version 6.

New contributor

user26617849 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT