0

So i am using Hibernate, and i got this an entity called personAddress, this is an entity that exists on the database, since i am using microservices i use namedQuerys to do the joins, since there are some entities i need to join but i dont have, this is my native query in my hbm:

select address.*, cit.description cityName, sta.description stateName, ctry.description_iso_common countryName from PERSON_ADDRESS address
            inner join APP_CITIES cit on cit.id =address.city_id
            inner join APP_STATES sta on sta.id= address.state_id
            inner join APP_COUNTRY ctry on ctry.id = address.country_id
            and address.person_id = :param 

thing is , the cityName, stateName and countryName aren't on the database object since they come from a relation , so in my entity i have them as @Transient, but still , on persist , it tries to persist those columns so it returns persistance error (com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'cityName') if i set the getter of the class as @Transient, it doesnt give the persistance error but then the query doesn't print the city. This is my findByPersonIs function in my repository:

  public List<PersonAddress> findByPersonIs(String iso,String personId, int offset, int limit){

        Query<PersonAddress> namedQuery = null;

        namedQuery = sessionFactory.getCurrentSession().createNamedQuery("findByPersonIs", PersonAddress.class);
        namedQuery.setParameter("param", "%".concat(personId).concat("%").toLowerCase());
        // namedQuery.setParameter("codeIso", "%".concat(iso).concat("%").toLowerCase());


        namedQuery.setParameter("param", personId);
        //namedQuery.setParameter("codeIso", iso);
        namedQuery.setMaxResults(limit).setFirstResult(offset);
        return namedQuery.getResultList();
    }

Would appreciate help to solve this issue since i cannot bring the missing tables to the shared (DDD) nor doesn't allow me to retrieve the query into a DTO (not sure why)

I was expecting that as an entity has a @Transient field this doesn't go persisted, but somehow still would like to retrieve this value.

  • Forgot to say that i have the hbm.xml without the cityName... mapped and not using JPA , so i don't know why even Hibernate is trying to persist those fields. – Guillem Te May 30 '23 at 10:08

1 Answers1

0

The most efficient way to solve this issue was to create a result set Mapping , in my case i wanted to still work with the named query so i can keep it on my xml but would work as well if created a native query, this is my XML capturing the dto:

        <query>
            <![CDATA[
             select address.id id, address.person_id personId, address.broker_id brokerId, cit.description cityName,
                     sta.description stateName, ctry.description_iso_common countryName,
                      address.street_name streetName, address.street_number streetNumber, address.post_code postCode, def.description addressType
                      from PERSON_ADDRESS address
                      inner join APP_CITIES cit on cit.id =address.city_id
                      inner join APP_COUNTRY ctry on ctry.id= address.country_id
                      inner join APP_STATES sta on sta.id = address.state_id
                      inner join DEFLIST_DATA def on def.id =address.address_type
                      inner join app_language al on al.id =def.language_id
                      where address.person_id = :param AND al.code_iso = :iso
            ]]>
        </query>
    </named-native-query>
    <sql-result-set-mapping name="PersonAddressGridGetMapping">
        <constructor-result target-class="net.amantium.odiseo.address.domain.dto.PersonAddressGridGetResponse">
            <column name ="id" />
            <column name="personId"  />
            <column name="cityName" />
            <column name="stateName"  />
            <column name="countryName"  />
            <column name="streetName" />
            <column name="streetNumber"  />
            <column name="postCode"  />
            <column name="addressType"  />
        </constructor-result>
    </sql-result-set-mapping>```