Is there someone that could answer questions concerning the HibernateTravelApp tutorial. I have written it a couple times per the tutorial and I am presently remodeling it to fit my own MySQL database. If there is a convenient way to transfer this data base along with the .zip of the netbeans project I would help me to be able to have someone look at it
I can .zip the project but it is possible to send the MySQL database?

If not I have the session bean for the HibernateTravelApp tutorialbelow and I am trying to understand how the drop down list is populated and how the query is configured to display the table data.

/*
 * SessionBean1.java
 *
 * Created on Aug 21, 2008, 7:41:35 AM
 */
package hibernatetravelapp;

import com.sun.rave.web.ui.appbase.AbstractSessionBean;
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 travel.HibernateUtil;
import travel.Person;
import travel.Trip;

/**
 * <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 {
    }
    // </editor-fold>
    /**
     * <p>Construct a new session data bean instance.</p>
     */
    public SessionBean1() {
    }
    private Option[] personOptions;
    private Integer selectedPersonId;
    private Trip[] trips4Person;

    /**
     * Get the value of trips4Person
     *
     * @return the value of trips4Person
     */
    public Trip[] getTrips4Person() {
        return trips4Person;
    }

    /**
     * Set the value of trips4Person
     *
     * @param trips4Person new value of trips4Person
     */
    public void setTrips4Person(Trip[] trips4Person) {
        this.trips4Person = trips4Person;
    }

    /**
     * Get the value of selectedPersonId
     *
     * @return the value of selectedPersonId
     */
    public Integer getSelectedPersonId() {
        return selectedPersonId;
    }

    /**
     * Set the value of selectedPersonId
     *
     * @param selectedPersonId new value of selectedPersonId
     */
    public void setSelectedPersonId(Integer selectedPersonId) {
        this.selectedPersonId = selectedPersonId;
        updateTrips4Person();
    }

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

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

    /**
     * <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() {
        super.init();

        try {
            _init();
        } catch (Exception e) {
            log("SessionBean1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        // Fill in the personOptions[]
        buildPersonOptions();

    }

    /**
     * <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 buildPersonOptions() {
        List<Person> personList = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Person");
            personList = (List<Person>) q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }

        personOptions = new Option[personList.size()];
        int i = 0;
        for (Person person : personList) {
            Option opt = new Option(person.getPersonId(), person.getName());
            personOptions[i++] = opt;
        }
    }

    private void updateTrips4Person() {
        if (selectedPersonId == null) {
            trips4Person = new Trip[1];
            trips4Person[0] = new Trip();
            return;
        }

        Set personTrips = null;
        try {
            Session session =
                    HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            Person person = (Person) session.load(Person.class, selectedPersonId);
            personTrips = (PersistentSet) person.getTrips();

        } catch (Exception e) {
            e.printStackTrace();
        }

        trips4Person = (Trip[]) personTrips.toArray(new Trip[0]);
    }
}

I see in the JSF it controls the table rows

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    Document   : Page1
    Created on : Aug 21, 2008, 7:41:36 AM
    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:messageGroup id="messageGroup1" style="position: absolute; left: 384px; top: 432px"/>
                        <webuijsf:table augmentTitle="false" id="table1" style="left: 144px; top: 168px; position: absolute; width: 0px" title="Table" width="0">
                            <webuijsf:tableRowGroup id="tableRowGroup1" rows="10" sourceData="#{SessionBean1.trips4Person}" sourceVar="currentRow">
                                <webuijsf:tableColumn headerText="tripId" id="tableColumn1" sort="tripId">
                                    <webuijsf:staticText id="staticText1" text="#{currentRow.value['tripId']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="depCity" id="tableColumn2" sort="depCity">
                                    <webuijsf:staticText id="staticText2" text="#{currentRow.value['depCity']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="depDate" id="tableColumn3" sort="depDate">
                                    <webuijsf:staticText id="staticText3" text="#{currentRow.value['depDate']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="destCity" id="tableColumn4" sort="destCity">
                                    <webuijsf:staticText id="staticText4" text="#{currentRow.value['destCity']}"/>
                                </webuijsf:tableColumn>
                                <webuijsf:tableColumn headerText="tripTypeId" id="tableColumn5" sort="tripTypeId">
                                    <webuijsf:staticText id="staticText5" text="#{currentRow.value['tripTypeId']}"/>
                                </webuijsf:tableColumn>
                            </webuijsf:tableRowGroup>
                        </webuijsf:table>
                        <webuijsf:dropDown binding="#{Page1.dropDown1}" id="dropDown1" items="#{SessionBean1.personOptions}"
                            onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'dropDown1');" selected="#{SessionBean1.selectedPersonId}" style="position: absolute; left: 120px; top: 72px"/>
                    </webuijsf:form>
                </webuijsf:body>
            </webuijsf:html>
        </webuijsf:page>
    </f:view>
</jsp:root>

maybe I should start by understanding elements in the xml Person.hbm

<?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="travel.Person" optimistic-lock="version"
      polymorphism="implicit" select-before-update="false" table="PERSON">
        <id column="PERSONID" name="personId">
            <generator class="increment"/>
        </id>
        <property column="NAME" name="name"/>
        <property column="JOBTITLE" name="jobTitle"/>
        <property column="FREQUENTFLYER" name="frequentFlyer"/>
        <set cascade="all-delete-orphan" inverse="true" lazy="true" name="trips" table="TRIP">
            <key column="PERSONID"/>
            <one-to-many class="travel.Trip"/>
        </set>
    </class>
</hibernate-mapping>

in my MySQL database I used the same structure as above.
I have this mapping structure for 3 classes and the HibernateUtil.class
for instance my database uses a Teacher.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schedule;

/**
 *
 * @author depot
 */
public class Teacher {
    private int instrNum;
    private String instructor;
    private String state;

    public int getInstrNum() {
        return instrNum;
    }

    public void setInstrNum(int instrNum) {
        this.instrNum = instrNum;
    }

    public String getInstructor() {
        return instructor;

    }

    public void setInstructor(String instructor) {
        this.instructor = instructor;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
    

}

this is the .hbm.xml

<?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">
       <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="instructorId" table="booking">
          <key column="instr_num"/>
          <one-to-many class="schedule.Booking"/>
      </set>
      
  </class>
</hibernate-mapping>

in my MySQL database I have a Instr_num field that relates to the Teacher.instrNum yet this field from the db Instr_num is not AUTO_INCREMENT so I was questioning the use of

<id column="instr_num" name="instructorId">
            <generator class="increment"/>
        </id>

I am not sure why this would be different from the other properties

and concerning the code

<set cascade="all-delete-orphan" inverse="true" lazy="true" name="instructorId" table="booking">
          <key column="instr_num"/>
          <one-to-many class="schedule.Booking"/>
      </set>

I believe this is a type of list that will only except Teacher.instructor with no duplication

ultimately I would like to have the drop down list select a Teacher and display info from the booking class concerning dates and times with students
Is this xml capable of this?

I know my line of questions are scattered so any input on any subject would be apreciated.

I think my first concern is to understand how the Option[] and the q.query and other areas are working. from the tutorial HibernateTravelApp

I introduced a lot of code that I will try to formulate more spicific questions to understand the tutorial if any one is able to help.
Thanks
-Steve

Recommended Answers

All 2 Replies

Has any one worked with the HibernateTravelApp tutorial
-Steve

commented: Posting a thread on the same topic as a previous thread that you posted within 12 hours is wrong +0

i think this thread has something on it: LINK

come to think of it, you started it, 8 hours ago (7 from when you posted this). and if noone replied they either don't know, or haven't seen it yet. you should wait at least a day, and post exactly what problems your having, what exceptions are thrown, why you don't understand, at least in your last thread you did post what you knew, but this one is very vague. in the first thread you said you would formulate more specific questions, which if you did you should post in the same thread that you had started earlier.

my intent is not to be harsh, i just think that posting another thread, when you already have one on the topic is not very good. be a little more patient when posting a new thread about the same topic. wait a a day, i haven't had the problem that i needed answered quickly, but without urgency, i wait until the previous thread is off of the first page page of the board, or "bump" my old thread.

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.