Hibernate Collection mit List

mahosand

Grünschnabel
Guten abend,

ich habe folgendes Problem, un zwar beim Zugriff auf Properties aus einem Set wird eine Exception geworfen.

Ein geküztes Programm-Code möchte ich hier unten vorstellen.

Erste Klasse : Patient

Code:
   // Generated 20.12.2006 20:30:40 by Hibernate Tools 3.1.0.beta5
  
  //imports …
  
  /**
   * Patient generated by hbm2java
   */
  public class Patient implements java.io.Serializable {
  
        // Fields    
  
        private int patientId;
  
        private Set<Feedback> feedbacks = new       

                            HashSet<Feedback>(0);
  
        private Set<Chemotest> chemotests = new 

                             HashSet<Chemotest>(0);
  
        // Constructors
  
        /** default constructor */
       public Patient() {
      }
  
  // Property accessors
       public int getPatientId() {
              return this.patientId;
        }
      ...

        public Set<Feedback> getFeedbacks() {
              return this.feedbacks;
        }
  
        public void setFeedbacks(Set<Feedback> feedbacks) {
              this.feedbacks = feedbacks;
        }
  
        public Set<Chemotest> getChemotests() {
              return this.chemotests;
        }
  
        public void setChemotests(Set<Chemotest> chemotests) {
  this.chemotests = chemotests;
        }
  
  }
Die zweite Klasse : Feedback

Code:
public class Feedback implements java.io.Serializable {
  
  // Fields    
  
        private int  feedbackId;
  
        private Patient   patient;
  
        private String   feedbackText;
  
        private byte type;
  
        private Date date;
  
        // Constructors
  
        /** default constructor */
        public Feedback() {
        }
  

        // Property accessors
        public int getFeedbackId() {
              return this.feedbackId;
        }
  
        …
  }
Meine Klassen sind wie folgt gemappt:

1.Klasse Patient

Code:
 <?xml version="1.0"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <!-- Generated 20.12.2006 20:30:41 by Hibernate Tools 3.1.0.beta5 -->
  <hibernate-mapping>
      <class name="ipis.domain.Patient" table="patient" catalog="ipis">
  <comment></comment>
          <id name="patientId" type="int">
              <column name="patient_id" />
              <generator class="assigned" />
  </id>
  
          <set name="feedbacks" inverse="true">
              <key>
                  <column name="patient_id" not-null="true">
                      <comment></comment>
                  </column>
              </key>
              <one-to-many class="ipis.domain.Feedback" />
          </set>
          <set name="chemotests" inverse="true">
              <key>
                  <column name="patient_id" not-null="true">
                      <comment></comment>
                  </column>
              </key>
              <one-to-many class="ipis.domain.Chemotest" />
          </set>
      </class>
  </hibernate-mapping>


2.Klasse Feedback

Code:
  <?xml version="1.0"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <!-- Generated 20.12.2006 20:30:41 by Hibernate Tools 3.1.0.beta5 -->
  <hibernate-mapping>
      <class name="ipis.domain.Feedback" table="feedback" catalog="ipis">
          <comment></comment>
          <id name="feedbackId" type="int">
              <column name="feedback_id" />
              <generator class="increment" />
          </id>
          <many-to-one name="patient" class="ipis.domain.Patient" fetch="select">
              <column name="patient_id" not-null="true">
                  <comment></comment>
              </column>
          </many-to-one>
          <property name="feedbackText" type="string">
              <column name="feedback_text" length="65535" not-null="true">
                  <comment></comment>
              </column>
          </property>
      </class>
  </hibernate-mapping>

In der Frontend-Schicht möchte ich die Daten über den Patienten und seine Feedbacks anzeigen, meine JSF-Seite sieht so aus:


Code:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  
  <html>
  <%@ include file="../base/includeHeader.jsp" %>
  <body>
  
  <f:view>
  <%@ include file="top.jsp"%>
   <h2>My data</h2>
    <h:form id="form">
     <table>
        <h:dataTable value="#{patientAction.currentPatient}" var="patient"
        border="1" >
  
       <h:column>
         <h:outputText value="Identifier" />
       </h:column>
  
       <h:column>
         <h:outputText value="#{patient.patientId}" />
       </h:column>
  
  
      <h:column>
        <h:dataTable value="#{patient.feedbacks}" var="feedbacks"
        border="1">
      <h:column>
        <h:outputText value="#{feedbacks.type}" />
      </h:column>
        </h:dataTable>
      </h:column>
     </h:dataTable>
  
  </table>
  </h:form>
  
  </f:view>
  
  </body>
  </html>

Schön wäre es wenn es so funktionieren würde, leider wird folgendes Exception geworfen:

Code:
  2007-01-22 15:26:02,317 ERROR [org.apache.myfaces.shared_impl.renderkit.RendererUtils] - Property not found - called by component : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /ipis-jsp/mydata.jsp][Class: javax.faces.component.html.HtmlForm,Id: form][Class: javax.faces.component.html.HtmlDataTable,Id: _idJsp5][Class: javax.faces.component.UIColumn,Id: _idJsp10][Class: javax.faces.component.html.HtmlDataTable,Id: _idJsp11][Class: javax.faces.component.UIColumn,Id: _idJsp12][Class: javax.faces.component.html.HtmlOutputText,Id: _idJsp13]}
  javax.faces.el.PropertyNotFoundException: Bean: org.hibernate.collection.PersistentSet, property: type
        at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(PropertyResolverImpl.java:483)
        at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(PropertyResolverImpl.java:454)
        at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:417)
        at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
        at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(ELParserHelper.java:532)
        at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
        at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:383)
        at javax.faces.component.UIOutput.getValue(UIOutput.java:77)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(RendererUtils.java:217)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderOutput(HtmlTextRendererBase.java:69)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:57)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:536)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:442)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChildren(RendererUtils.java:419)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:440)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.renderColumnBody(HtmlTableRendererBase.java:212)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.encodeColumnChild(HtmlTableRendererBase.java:181)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:167)
        at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:100)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:524)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:436)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChildren(RendererUtils.java:419)
        at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:440)
  
  …
  …
  …

Ich bin Absolut neu in JSF und JSP, ich weiss nicht wie ich dieses Problem überwende.
Vielleicht muss ich hier List statt Set benutzen, stimmt mir jemand zu? Wenn ja wie geht's das?

Ich würde mich sehr freune über eine Antwort.
Danke im Voraus.
 
Hallo!

Im Hibernate Mapping für Feedback fehelen die Properties für:
Code:
   private byte type;
  
        private Date date;

Weiterhin brauchst du dafür auch entsprechende (public) getter / setter Methoden in der Klasse Feedback, dann sollte es klappen.

Gruß Tom
 
Hallo Tom,
vielen dank für deine Antwort!
Es tut mir Leid ich wollte ein bißschen kürzen und habe ein paar Zeilen gelöscht :(
Hier ist der vollständige Code
Das hibernate mapping:
Code:
<hibernate-mapping>
    <class name="ipis.domain.Feedback" table="feedback" catalog="ipis">
        <comment></comment>
        <id name="feedbackId" type="int">
            <column name="feedback_id" />
            <generator class="increment" />
        </id>
        <many-to-one name="patient" class="ipis.domain.Patient" fetch="select">
            <column name="patient_id" not-null="true">
                <comment></comment>
            </column>
        </many-to-one>
        <property name="feedbackText" type="string">
            <column name="feedback_text" length="65535" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="type" type="byte">
            <column name="type" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="date" type="timestamp">
            <column name="date" length="19" not-null="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>
und die Feedback-Klasse

Code:
package ipis.domain;

// Generated 20.12.2006 20:30:40 by Hibernate Tools 3.1.0.beta5

import java.util.Date;

/**
 * Feedback generated by hbm2java
 */
public class Feedback implements java.io.Serializable {

    // Fields    

    private int feedbackId;

    private Patient patient;

    private String feedbackText;

    private byte type;

    private Date date;

    // Constructors

    /** default constructor */
    public Feedback() {
    }

    /** full constructor */
    public Feedback(int feedbackId, Patient patient, String feedbackText,
            byte type, Date date) {
        this.feedbackId = feedbackId;
        this.patient = patient;
        this.feedbackText = feedbackText;
        this.type = type;
        this.date = date;
    }

    // Property accessors
    public int getFeedbackId() {
        return this.feedbackId;
    }

    public void setFeedbackId(int feedbackId) {
        this.feedbackId = feedbackId;
    }

    public Patient getPatient() {
        return this.patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public String getFeedbackText() {
        return this.feedbackText;
    }

    public void setFeedbackText(String feedbackText) {
        this.feedbackText = feedbackText;
    }

    public byte getType() {
        return this.type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

}
Muss ich irgendwo irgenwelche Konifurationen machen?
Ich habe eine Datei : ApplictionContextJSF.xml die spring-Bean.Definitionen enthält, und wird in Faces-config.xml geladen:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    default-autowire="byType">

    
    <!-- Für die Fehlermeldungen und Meldungen -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>errors</value>
                <value>ipis.auth.messages</value>
            </list>
        </property>
    </bean>
    
    
    <bean id="feedbackAction" class="ipis.web.jsf.FeedbackAction">
        <property name="feedbackServiceI" ref="feedbackServiceI" />
        <property name="feedbackDaoI" ref="feedbackDaoI" />
        <property name="patientAuthService" ref="patientAuthService" />
    </bean>
    
    
    <bean name="patientAuthService" class="ipis.auth.PatientAuthServiceImpl">
        <property name="authenticationManager"
            ref="authenticationManager" />
        <property name="patientDaoI" ref="patientDaoI" />
    </bean>
    
    <bean id="patientAction"
        class="ipis.web.jsf.PatientAction">
        <property name="patientServiceI" ref="patientServiceI" />
        <property name="patientAuthService" ref="patientAuthService" />
        <property name="patientDaoI" ref="patientDaoI" />
        <property name="messageSource" ref="messageSource" />
    </bean>
    
     <!-- session scope -->
     <bean name="feedback"
        class="ipis.domain.Feedback"
        scope="session">
    </bean>
    
   
    <!-- request scope -->
    <bean name="patient"
        class="ipis.domain.Patient"
        scope="request">
    </bean>
  
</beans>
Ich weiss nicht warum die Properties von Feedback nicht erkannt werden, HILFE:(

Brauche ich irgenwie ein converter?

Gruß Hind
 
Noch eine Frage :
Ich habe in der Datenbank für das Attribut "gender" , eine 0 oder 1 gespeichert, wenn es eine 1 ist, dann heißt es, dass gender Männlich ist , und wenn 0 dann heißt es, dass gender Weiblich ist.
Ich möchte in der Ausgabe , statt 0 oder 1 , Männlich oder Weiblich ausgeben.
Mein Code sieht so aus:

Code:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  
  <html>
  <%@ include file="../base/includeHeader.jsp" %>
  <body>
  
  <f:view>
  <%@ include file="top.jsp"%>
   <h2>My data</h2>
    <h:form id="form">
     <table>
        <h:dataTable value="#{patientAction.currentPatient}" var="patient"
        border="1" >
  
       <h:column>
         <h:outputText value="Identifier" />
       </h:column>
  
       <h:column>
         <h:outputText value="#{patient.patientId}" />
       </h:column>
 
     <h:column>

    <% if(%>${patient.gender}<%==1) %>
                      
        <h:outputText value="Weiblich" />
                      
      </h:column>
     

  
      <h:column>
        <h:dataTable value="#{patient.feedbacks}" var="feedbacks"
        border="1">

      <h:column>
        <h:outputText value="#{feedbacks.type}" />
      </h:column>
        </h:dataTable>
      </h:column>

     </h:dataTable>
  
  </table>
  </h:form>
  
  </f:view>
  
  </body>
  </html>
Das in Blau markierte code war einer der Versuche. Bitte wenn jemand weiss wie es geht, bitte meldet euch.
 

Neue Beiträge

Zurück