简体   繁体   中英

jsf & hibernate integration - picklist values from database

i am new to hibernate and jsf technologies and i am trying to have a picklist which retrieves and updates (if user alters its values) values from database.

My database schema is as follows:

I have two tables (entities) CRImageType(ImTypeId(PK),ImTypeName,ImTypeDescription) & CRVariable(VarId(PK),VarName,VarDescription) which have a many to many relation through the table 'imtype_variable' implemented in my Hibernate Data Model.

The Model seems to work fine. I am also able to use jsf elements (datatables with insert, edit and delete buttons + corresponding dialogboxes + bean classes) in order to read and write to tables CRVariable & CRImageType.

I want to have also a picklist in order to be able to associate CRImageType with CRVariable records. My business scenario is to be able to re-associate an image type with one or more variables (medical term) so i am using a jsf picklist:

<p:dialog id="dialog-associatevariables" header="Associate Variables" widgetVar="dlg7"     dynamic="true" showEffect="fade">
<h:panelGrid id="associateVariables" columns="1" cellpadding="4">
<p:pickList id="pickList" 
                value="#{reportConfiguratorBean.getAssocImTypVariables(reportConfiguratorBean.selectedCRImageType.imTypeId)}"
                var="cRVariable" 
                itemValue="#{cRVariable}" 
                itemLabel="#{cRVariable.varName}">
    <f:facet name="sourceCaption">Available Variables</f:facet>
        <f:facet name="targetCaption">Associated Variables</f:facet>
</p:pickList>
</h:panelGrid>  
</p:dialog>

In My 'reportConfigurator' backbean i have implemented the following method in order to populate the picklist (following the online primefaces demo):

@SuppressWarnings("unchecked")
public DualListModel<CRVariable> getAssocImTypVariables(Long imTypeId)     {

List<CRVariable> source1;
List<CRVariable> target;
...         
String hq3 = "select distinct v from CRVariable v join v.crimagetypes t where t.id in (:itid)";
Query query3 = session.createQuery(hq3);
query3.setParameter("itid",imTypeId);
target = query3.list();
System.out.println("Associated Variables with Id: " + target);          
String hq4 = "select v FROM CRVariable v WHERE v.id not in (" +
            "select distinct v1.id " +
            "from CRVariable v1 " +
            "join v1.crimagetypes t2 " +
            "where t2.id in (:itid))";
Query query4 = session.createQuery(hq4);
query4.setParameter("itid",imTypeId);
source = query4.list();
System.out.println("Non Associated Variables with Id: " + source);
String hq5 = "FROM CRVariable";
Query query5 = session.createQuery(hq5);
source1 = query5.list();
System.out.println("Non Associated Variables with Id: " + source1);         
dualListVars = new DualListModel<CRVariable>(source1, target);          
System.out.println("Dual List Model: " + dualListVars);
...
{
session.close();
}
return dualListVars;
}

Back bean method '' seem to work fine since in my console the correct list item object are populated (source1 & target) and it returnes a Dual List Model object as shown in the following lines (console output):

Associated ImageType with Id: 26
...
Associated Variables with Id: [varId : 75, varName : Ki67(X), varDescription : Ki67(X)]
Non Associated Variables with Id: [varId : 71, varName : ER (X), varDescription : ER (X), varId : 72, varName : HER2(X), varDescription : HER2(X), varId : 73, varName : IHC, varDescription : IHC, varId : 74, varName : FISH, varDescription : FISH, varId : 76, varName : PTEN(X), varDescription : PTEN(X), varId : 77, varName : Histology (X), varDescription : Histology (X)]
...
Non Associated Variables with Id: [varId : 71, varName : ER (X), varDescription : ER (X), varId : 72, varName : HER2(X), varDescription : HER2(X), varId : 73, varName : IHC, varDescription : IHC, varId : 74, varName : FISH, varDescription : FISH, varId : 75, varName : Ki67(X), varDescription : Ki67(X), varId : 76, varName : PTEN(X), varDescription : PTEN(X), varId : 77, varName : Histology (X), varDescription : Histology (X)]
...

Τhe PROBLEM is that **when i call the picklist dialog, picklist columns does not populate correctly ...

After some debugging/research i think that it has to do with the way i am calling my picklist method "reportConfiguratorBean.getAssocImTypVariables(...)" .

If i use "reportConfiguratorBean.selectedCRImageType.imTypeId" as method argument it is passed as ZERO value in the backbean !!!

If i hardcode an integer number as an argument eg 25 the picklist is populated correctly but in the console i can see the following exception:

ERROR class java.lang.IllegalArgumentException: can't parse argument number reportConfiguratorBean.getAssocImTypVariables(25): javax.el.PropertyNotWritableException: /views/report-configurator.xhtml @271,10 value="#{reportConfiguratorBean.getAssocImTypVariables(25)}": Illegal Syntax for Set Operation

Any ideas of what i have been missing???

After some research and debugging it seems that my picklist component could not accept (value="#{reportConfiguratorBean.getAssocImTypVariables(reportConfiguratorBean.selectedCRImageType.imTypeId)}") a method which requires an argument to be passed... I re-implemented "" method (my back bean is now Session scoped) as:

public DualListModel<CRVariable> getAssocImTypVariables() {
    Long imTypeId = Long.parseLong(virtualId);
...

and my picklist is populated ...

Where virtualId is a global String variable populated with selectedItem Id ...

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