Hello.
I have built a hibernate project and the dropdown list and the table load data to the web page but the drop down list is not poulated with the names (derived from the xml id)
There is a lot of code to paste. Is there someone that could help me with a project of this nature? What code will you need from me?
Thanks
-Steve

Recommended Answers

All 6 Replies

What exactly "isn't happening"?
Check the generated html. Is the data there but isn't it shown?
If it is your html generation is at fault. If it isn't, you are almost certainly having an error in a loop somewhere in your code, causing the output of the list to be skipped.

I am very new at this hibernate animal but I will post the code that seems relevant to what your questions seek. Oh and all I would like to do at this point is somehow us the teachers id to aquire all the teacher name put them in <Teacher>list and then an array any way basically load the teacher names in the drop down list. At one point I had the id's in there and now there is nothing

the dropdown in bound to the instructorOptions(which I just noticed may be spelled wrong) items bound to instructorsOptions and selected property targets selectedInstrId


page.html code (and I'll throw in the desing if it permits)
person.hbm.xml
sessionBean1(note the two method at the end.
Thanks
-Steve

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    Document   : Page1
    Created on : Aug 23, 2008, 11:34:03 PM
    Author     : depot
-->
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
    <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
    <f:view>
        <webuijsf:page id="page1">
            <webuijsf:html id="html1">
                <webuijsf:head id="head1">
                    <webuijsf:link id="link1" url="/resources/stylesheet.css"/>
                </webuijsf:head>
                <webuijsf:body id="body1" style="-rave-layout: grid">
                    <webuijsf:form id="form1">
                        <webuijsf:dropDown binding="#{Page1.dropDown1}" id="dropDown1" items="#{SessionBean1.instructorOtions}"
                            onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'dropDown1');" selected="#{SessionBean1.selectedInstrId}" style="position: absolute; left: 72px; top: 48px"/>
                        <webuijsf:table augmentTitle="false" id="table1" itemsText="#{SessionBean1.instructorOtions}"
                            style="left: 48px; top: 168px; position: absolute; width: 0px" title="Table" width="0">
                            <webuijsf:tableRowGroup id="tableRowGroup1" rows="10" sourceData="#{Page1.bookingDataProvider}" sourceVar="currentRow">
                                <webuijsf:tableColumn headerText="book_num" id="tableColumn1" sort="booking.book_num">
                                    <webuijsf:staticText id="staticText1" text="#{currentRow.value['booking.book_num']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="book_start_date" id="tableColumn2" sort="booking.book_start_date">
                                    <webuijsf:staticText id="staticText2" text="#{currentRow.value['booking.book_start_date']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="book_start_time" id="tableColumn3" sort="booking.book_start_time">
                                    <webuijsf:staticText id="staticText3" text="#{currentRow.value['booking.book_start_time']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="book_end_time" id="tableColumn4" sort="booking.book_end_time">
                                    <webuijsf:staticText id="staticText4" text="#{currentRow.value['booking.book_end_time']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="stu_num" id="tableColumn5" sort="booking.stu_num">
                                    <webuijsf:staticText id="staticText5" text="#{currentRow.value['booking.stu_num']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="instr_num" id="tableColumn6" sort="booking.instr_num">
                                    <webuijsf:staticText id="staticText6" text="#{currentRow.value['booking.instr_num']}"/>
                                </webuijsf:tableColumn>
                            </webuijsf:tableRowGroup>
                        </webuijsf:table>
                        <webuijsf:messageGroup id="messageGroup1" style="position: absolute; left: 264px; top: 480px"/>
                    </webuijsf:form>
                </webuijsf:body>
            </webuijsf:html>
        </webuijsf:page>
    </f:view>
</jsp:root>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" 
  mutable="true" name="schedule.Teacher" optimistic-lock="version" 
  polymorphism="implicit" select-before-update="false" table="instructor">
       <id column="instr_num" name="instructorId">
            <generator class="increment"/>
        </id>
     <!-- <property column="instr_num" name="instructorId">     -->
      <property column="instr_lname" name="instructor"/>
      <property column="instr_state" name="state"/>
      <set cascade="all-delete-orphan" inverse="true" lazy="true" name="instructorBook" table="booking">
          <key column="instr_num"/>
          <one-to-many class="schedule.Booking"/>
      </set>
      
  </class>
</hibernate-mapping>
/*
 * SessionBean1.java
 *
 * Created on Aug 23, 2008, 11:34:03 PM
 */
package musicmagazineapp;

import com.sun.rave.web.ui.appbase.AbstractSessionBean;
import com.sun.sql.rowset.CachedRowSetXImpl;
import com.sun.webui.jsf.model.Option;
import java.util.List;
import java.util.Set;
import javax.faces.FacesException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.collection.PersistentSet;
import schedule.Booking;
import schedule.HibernateUtil;
import schedule.Teacher;

/**
 * <p>Session scope data bean for your application.  Create properties
 *  here to represent cached data that should be made available across
 *  multiple HTTP requests for an individual user.</p>
 *
 * <p>An instance of this class will be created for you automatically,
 * the first time your application evaluates a value binding expression
 * or method binding expression that references a managed bean using
 * this class.</p>
 *
 * @author depot
 */
public class SessionBean1 extends AbstractSessionBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        bookingRowSet.setDataSourceName("java:comp/env/jdbc/musicschooldb1_MySQL");
        bookingRowSet.setCommand("SELECT * FROM booking");
        bookingRowSet.setTableName("booking");
    }
    private CachedRowSetXImpl bookingRowSet = new CachedRowSetXImpl();

    public CachedRowSetXImpl getBookingRowSet() {
        return bookingRowSet;
    }

    public void setBookingRowSet(CachedRowSetXImpl crsxi) {
        this.bookingRowSet = crsxi;
    }
    // </editor-fold>
    /**
     * <p>Construct a new session data bean instance.</p>
     */
    public SessionBean1() {
    }
    private Option[] instructorOtions;
    private Integer selectedInstrId;
    private Booking[] book4Instructor;

    /**
     * Get the value of book
     *
     * @return the value of book
     */
    public Booking[] getBook() {
        return book4Instructor;
    }

    /**
     * Set the value of book
     *
     * @param book new value of book
     */
    public void setBook(Booking[] book4Instructor) {
        this.book4Instructor = book4Instructor;
    }

    /**
     * Get the value of selectedInstrId
     *
     * @return the value of selectedInstrId
     */
    public Integer getSelectedInstrId() {
        return selectedInstrId;
    }

    /**
     * Set the value of selectedInstrId
     *
     * @param selectedInstrId new value of selectedInstrId
     */
    public void setSelectedInstrId(Integer selectedInstrId) {
        this.selectedInstrId = selectedInstrId;
        updateBooking4Instructor();
    }

    /**
     * Get the value of instructorOtions
     *
     * @return the value of instructorOtions
     */
    public Option[] getInstructorOtions() {
        return instructorOtions;
    }

    /**
     * Set the value of instructorOtions
     *
     * @param instructorOtions new value of instructorOtions
     */
    public void setInstructorOtions(Option[] instructorOtions) {
        this.instructorOtions = instructorOtions;
    }

    /**
     * <p>This method is called when this bean is initially added to
     * session scope.  Typically, this occurs as a result of evaluating
     * a value binding or method binding expression, which utilizes the
     * managed bean facility to instantiate this bean and store it into
     * session scope.</p>
     * 
     * <p>You may customize this method to initialize and cache data values
     * or resources that are required for the lifetime of a particular
     * user session.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("SessionBean1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
        try{
            _init();
        }catch(Exception e){
            log("SessionBean1 Initialization Failure",e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        //fill in the instrOptions[]
        buildInstrOptions();
    }

    /**
     * <p>This method is called when the session containing it is about to be
     * passivated.  Typically, this occurs in a distributed servlet container
     * when the session is about to be transferred to a different
     * container instance, after which the <code>activate()</code> method
     * will be called to indicate that the transfer is complete.</p>
     * 
     * <p>You may customize this method to release references to session data
     * or resources that can not be serialized with the session itself.</p>
     */
    @Override
    public void passivate() {
    }

    /**
     * <p>This method is called when the session containing it was
     * reactivated.</p>
     * 
     * <p>You may customize this method to reacquire references to session
     * data or resources that could not be serialized with the
     * session itself.</p>
     */
    @Override
    public void activate() {
    }

    /**
     * <p>This method is called when this bean is removed from
     * session scope.  Typically, this occurs as a result of
     * the session timing out or being terminated by the application.</p>
     * 
     * <p>You may customize this method to clean up resources allocated
     * during the execution of the <code>init()</code> method, or
     * at any later time during the lifetime of the application.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }
    private void buildInstrOptions(){
        List<Teacher> teacherList=null;
        try{
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx=session.beginTransaction();
            Query q=session.createQuery("from Teacher");
            teacherList=(List<Teacher>) q.list();
        }catch(Exception e){
            e.printStackTrace();
        }
        instructorOtions=new Option[teacherList.size()];
        int i=0;
        for(Teacher teacher:teacherList){
            Option opt=new Option(teacher.getInstrNum(),teacher.getInstructor());
            instructorOtions[i++]=opt;
        }
        
    }

    private void updateBooking4Instructor() {
       if(selectedInstrId==null){
           book4Instructor=new Booking[1];
           book4Instructor[0]=new Booking();
           return;
       }
       Set instructorBooking=null;
       try{
           Session session=HibernateUtil.getSessionFactory().getCurrentSession();
           Transaction tx =session.beginTransaction();
           Teacher teacher=(Teacher) session.load(Teacher.class,selectedInstrId);
           instructorBooking=(PersistentSet)teacher.getInstructorBook();
       }catch(Exception e){
           e.printStackTrace();
       }
       book4Instructor=(Booking[]) instructorBooking.toArray(new Booking[0]);
    }
   
}

and also this is booking information need that allow a join between teachers and students.
the studens xml. .jav etc not included.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" mutable="true" 
  name="schedule.Booking" optimistic-lock="version" polymorphism="implicit" 
  select-before-update="false" table="booking">
      <id column="book_num" name="bookNum">
            <generator class="increment"/>
        </id>
      <property column="book_num" name="bookNum"/>
      <property column="book_start_date" name="bookStartDate"/>
      <property column="book_star_time" name="bookStartTime"/>
      <property column="book_end_time" name="bookEndTime"/>
      <property column="instr_num" name="instrNum"/>
      <property column="stu_num" name="stuNum"/>
  </class>
</hibernate-mapping>

I have entity classes too. (getters,settersj)
Thanks again
-Stev

guess what, you never fill the array from which the dropdown is filled...

interesting. I wish I knew which array it was. Odds are I'll find it. is it where it says opt++;?

instructorOtions=new Option[teacherList.size()];
        int i=0;
        for(Teacher teacher:teacherList){
            Option opt=new Option(teacher.getInstrNum(),teacher.getInstructor());
            instructorOtions[i++]=opt;
        }

Could someone show me some techniques for debugging this code. I can't seem to think of a way to see what is in the list and arrays
-Steve

I found the prob.

public void prerender() {
    try {
        if (dropDown1.getSelected() == null ) {
            Option opt = getSessionBean1().getInstructorOptions()[0];
            getSessionBean1().setSelectedPersonId((Integer)firstInstructor.getValue());
        }
    } catch (Exception ex) {
        log("Error Description", ex);
        error(ex.getMessage());
    }
}

I needed to build the prerender();
Thanks
-Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.