Search code examples
javaxmlhibernateone-to-manyhibernate-onetomany

How do I get an object in the "many" part from a One-To-Many unidirectional relationship?


I don't know how to exactly express what I'm looking for, so I'll explain what I have and then what I'm trying to do.

I have a Class model, which has 2 classes with a One-To-Many unidirectional relationship.

public class TaxType extends Entity implements java.io.Serializable {
    //stuff
    private Set<TaxTypeAttribute> listTaxTypeAttribute = new HashSet<>(0);
}
public class TaxTypeAttribute extends Entity implements java.io.Serializable {
    private String attributeName;
    //stuff, but no reference to TaxType
}

Entity Class is like a primary key standard, we call it "OID design pattern", but no clue if it's like that in english.

public class Entity {
    private String oid;
    //constructor, get and set
}

On the mapping, it goes like this:

<class name="entity.TaxType" table="taxttype" catalog="tax_type" optimistic-lock="version">
    <id name="oid" type="string">
        <column name="OIDtt" length="50" />
        <generator class="uuid2" />
    </id>        
    <set name="listAtributoTipoImpuesto">
        <key column="OIDtt" not-null="true"/>
        <one-to-many class="entidades.AtributoTipoImpuesto" />
    </set>
</class>
<!-- two separated files, this is just for showing -->
<class name="entity.TaxTypeAttribute" table="taxtypeattribute" catalog="tax_type" optimistic-lock="version">
    <id name="oid" type="string">
        <column name="OIDtta" length="50" />
        <generator class="uuid2" />
    </id>
    <property name="attributeName" type="string">
        <column name="attributeName" length="50" not-null="true" />
    </property>
</class>

In one step of the program, I have a TaxType and the attributeName from a TaxTypeAttribute, but I need to get the full TaxTypeAttribute. I'm making querys through Criteria API. I could do taxType.getListTaxTypeAttribute(); and do a loop until I find the object, but I would like to know if there's a way to do it using some Hibernate querys.

I've tried doing taxType.getOid(); and then using that and the attributeName but it throws an exception:

Exception in thread "main" org.hibernate.QueryException: could not resolve property: OIDtt of: entity.TaxTypeAttribute

Any clues? Thanks you, and excuse me for my bad english

EDIT: In order to follow design patterns, we use this method to do SELECT querys: Awful thing we use for querys. The way I did it is this:

ArrayList<DTOCriteria> criteriaList = new ArrayList<>();
DTOCriteria c1 = new DTOCriteria();
c1.setAttribute("OIDtt");
c1.setOperation("=");
c1.setValue(taxType.getOID());
criteriaList.add(c1);
ArrayList<Object> found = search("TaxTypeAttribute");

I could add another DTOCriteria if I want ("attributeName";"=";attributeName, for example), but if the former doesn't works it's kind of useless. I also tried (just because it's free) using "TaxType" as attribute and the TaxType object as a value, but didn't work either.

PS: the code works. I use it for other querys and works, it just doesn't work for this one, or I don't know how to make it work. May be you can't do that kind of search, I don't know.


Solution

  • From an HQL/JPQL perspective, you could write your query as:

    SELECT tta FROM TaxType tt JOIN tt.listTaxTypeAttribute tta
     WHERE tt.oid = :oid
       AND tta.attributeName = :attributeName
    

    This query will return you TaxTypeAttribute instances that match the specified criteria. How you translate that into your query language is something which I cannot aid with.