简体   繁体   中英

Retrieving a Set in Hibernate mapping in many-to-many

I want to get the outcomes set from the Complaint class which is a hibernate entity from the Complaints Table. This table has a many-to-many relationship with a Table named Complaint_Outcome_Type and a join table named Complaint_Outcome.

Here is the Complaint and ComplaintOutcomeType classes

public class Complaint extends ComplaintBase{

    private set<ComplaintOutcomeType> outcomes;

}

public class ComplaintOutcomeType {
    private String code;
}

In ComplaintBase.hbm.xml I have

<class name="registrationdb.domain.ComplaintBase" table="COMPLAINT_BASE">
...
<joined-subclass name="registrationdb.domain.Complaint" table="COMPLAINT">
        <key> 
            <column name="COMPLAINT_ID"/>
        </key>
...
<set name="outcomes" table="COMPLAINT_OUTCOME" >
    <key column="COMPLAINT_ID" not-null="true"/>
    <many-to-many column="CODE" class="registrationdb.domain.reference.ComplaintOutcomeType" />
</set>
...
</class>

In ComplaintOutcomeType.hbm.xml I have

<class     name="registrationdb.domain.reference.ComplaintOutcomeType"     table="COMPLAINT_OUTCOME_TYPE">
    <id name="code" type="string" column="CODE" />
    <property name="description" type="string" column="DESCRIPTION" />
    <property name="displaySequence" type="integer" column="DISPLAY_SEQUENCE" />
</class>

Here is my query string

    String queryString = "select complaint.id, complaint.subject, complaint.complainant, complaint.openedDate, complaint.closedDate ";
    queryString = queryString + "from registrationdb.domain.Complaint as complaint ";
    queryString = queryString + "where complaint.subject.id = :complaintSubjectId and complaint.outcomes";

What I want to do is get the set of complaint outcomes for each complaint, so I can check if there is a complaint outcome of a particular type.

eg complaint.getOutcomes().contains(ComplaintType ...);

What do you use? If you use JPA, you can just get the list of complaint first like this:

String queryString = "select complaint from Complaint where complaint.subject.id = :complaintSubjectId";

it will return you a list of complaint. For each complaint, you can get the outcome set in the code:

complaint.getOutcomes()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM