-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: New to Hibernate AND Java! Displaying a Hashset
PostPosted: Mon Dec 10, 2007 8:44 am 
Newbie

Joined: Mon Dec 10, 2007 8:18 am
Posts: 5
Hello.

I have been given the task up updating a working application which currently uses Hibernate and Struts. To give a short background on the app, I have a group which can have many events. There is a one to many relationship which I believe I have covered in the mapping files (posted below). So lets say I have the group which has 3 events. On the JSP, I would like to display all the events for that group and eventually have edit/add/deletes added, but I first need to figure out how to display them! I am passing all the events to the form in a hashset. I can't figure out how to iterate through them to display the events. I think it may be a struts iterate command? I'm not sure.

Hibernate version: 3

Mapping documents:
Group.hbm.xml:
Code:
<?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 name="bo.groupLocator" table="grouplocator" lazy="false">

<id name="groupCode" column="group_code" type="java.lang.String">
      <generator class="assigned">
      </generator>
</id>

<property name="groupName" column="group_name" type="java.lang.String" not-null="true"/>

<property name="distCode" column="distcode" type="java.lang.String" not-null="true"/>
<property name="area" column="area" type="java.lang.String" not-null="true"/>
<property name="company" column="company" type="java.lang.String" not-null="true"/>

    <component name="address" class="bo.Address" >
       <property name="line1" column="address_line_1" type="java.lang.String" not-null="true"/>
       <property name="line2" column="address_line_2" type="java.lang.String" />
       <property name="city" column="city" type="java.lang.String" not-null="true" />
       <property name="state" column="state" type="java.lang.String" not-null="true"/>
       <property name="zipCode" column="zip_code" type="java.lang.String" not-null="true"/>
    </component>

<property name="contactName" column="contact_name" type="java.lang.String" not-null="true"/>
<property name="contactTitle" column="contact_title" type="java.lang.String" />
<property name="contactPhoneNum" column="contact_phone" type="java.lang.String" not-null="true"/>
<property name="contactFaxNum" column="contact_fax" type="java.lang.String" />
<property name="contactEmail" column="contact_email" type="java.lang.String" />

<property name="contactName2" column="contact_name_2" type="java.lang.String" />
<property name="contactTitle2" column="contact_title_2" type="java.lang.String" />
<property name="contactPhoneNum2" column="contact_phone_2" type="java.lang.String" />
<property name="contactEmail2" column="contact_email_2" type="java.lang.String" />

<property name="url" column="url" type="java.lang.String" />

    <set name="events" inverse="true">
        <key column="group_code"/>
        <one-to-many class="bo.groupEventLocator"/>
    </set>

</class>
</hibernate-mapping>


Group.java:
Code:
package bo;

import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;

public class groupLocator {

   private static Logger log = Logger.getLogger(groupLocator.class);
      
   private String m_groupName = "";
   private String m_groupCode;
   private String m_distCode = "";
   private String m_area = "";
   private String m_company = "";

   private Address m_address;

   private String m_contactName;
   private String m_contactTitle;
   private String m_contactPhoneNum;
   private String m_contactFaxNum = "";
   private String m_contactEmail;
   
   private String m_contactName2;
   private String m_contactTitle2;
   private String m_contactPhoneNum2;
   private String m_contactEmail2;
   
   private String m_url = "";
   
   private Set events = new HashSet();
      
   /**
    *  Gets the groupName for the groupLocator.
    *
    * @return The groupName.
    */
   public String getgroupName() {
      return m_groupName;
   }
   
   /**
    *  Gets the groupCode for the groupLocator.
    *
    * @return The groupCode.
    */
   public String getgroupCode() {
      return m_groupCode;
   }

   /**
    *  Sets the groupName for the groupLocator.
    *
    * @param p_groupNmae The groupName.
    */
   public void setgroupName(String p_groupName) {
      m_groupName = p_groupName;
   }

   /**
    *  Sets the groupCode for the groupLocator.
    *
    * @param p_groupCode The groupCode.
    */
   public void setgroupCode(String p_groupCode) {
      m_groupCode = p_groupCode;
   }

   /**
    *  Gets the distCode for the groupLocator.
    *
    * @return The distCode.
    */
   public String getDistCode() {
      return m_distCode;
   }

   /**
    *  Sets the distCode for the groupLocator.
    *
    * @param p_groupNmae The distCode.
    */
   public void setDistCode(String p_distCode) {
      m_distCode = p_distCode;
   }


   /**
    *  Gets the area for the groupLocator.
    *
    * @return The area.
    */
   public String getArea() {
      return m_area;
   }

   /**
    *  Sets the area for the groupLocator.
    *
    * @param p_groupNmae The area.
    */
   public void setArea(String p_area) {
      m_area = p_area;
   }

   /**
    *  Gets the company for the groupLocator.
    *
    * @return The company.
    */
   public String getCompany() {
      return m_company;
   }

   /**
    *  Sets the company for the groupLocator.
    *
    * @param p_groupNmae The company.
    */
   public void setCompany(String p_company) {
      m_company = p_company;
   }
   
   /**
    *  Gets the address for the groupLocator.
    *
    * @return The address.
    */   
   public Address getAddress() {
      return m_address;
   }

   /**
    *  Sets the address for the groupLocator.
    *
    * @param p_address The address.
    */
   public void setAddress(Address p_address) {
      m_address = p_address;
   }


   /**
    *  Gets the contactName for the groupLocator.
    *
    * @return The contactName.
    */
   public String getContactName() {
      return m_contactName;
   }

   /**
    *  Sets the contactName for the groupLocator.
    *
    * @param p_groupNmae The contactName.
    */
   public void setContactName(String p_contactName) {
      m_contactName = p_contactName;
   }


   /**
    *  Gets the contactTitle for the groupLocator.
    *
    * @return The contactTitle.
    */
   public String getContactTitle() {
      return m_contactTitle;
   }

   /**
    *  Sets the contactTitle for the groupLocator.
    *
    * @param p_groupNmae The contactTitle.
    */
   public void setContactTitle(String p_contactTitle) {
      m_contactTitle = p_contactTitle;
   }

   /**
    *  Gets the contactPhoneNum for the groupLocator.
    *
    * @return The contactPhoneNum.
    */
   public String getContactPhoneNum() {
      return m_contactPhoneNum;
   }

   /**
    *  Sets the contactPhoneNum for the groupLocator.
    *
    * @param p_groupNmae The contactPhoneNum.
    */
   public void setContactPhoneNum(String p_contactPhoneNum) {
      m_contactPhoneNum = p_contactPhoneNum;
   }

   /**
    *  Gets the contactFaxNum for the groupLocator.
    *
    * @return The contactFaxNum.
    */
   public String getContactFaxNum() {
      return m_contactFaxNum;
   }

   /**
    *  Sets the contactFaxNum for the groupLocator.
    *
    * @param p_groupNmae The contactFaxNum.
    */
   public void setContactFaxNum(String p_contactFaxNum) {
      m_contactFaxNum = p_contactFaxNum;
   }

   /**
    *  Gets the contactEmail for the groupLocator.
    *
    * @return The contactEmail.
    */
   public String getContactEmail() {
      return m_contactEmail;
   }

   /**
    *  Sets the contactEmail for the groupLocator.
    *
    * @param p_groupNmae The contactEmail.
    */
   public void setContactEmail(String p_contactEmail) {
      m_contactEmail = p_contactEmail;
   }


   /**
    * @return Returns the m_contactEmail2.
    */
   public String getContactEmail2() {
      return m_contactEmail2;
   }
   /**
    * @param email2 The m_contactEmail2 to set.
    */
   public void setContactEmail2(String p_contactEmail2) {
      m_contactEmail2 = p_contactEmail2;
   }
   /**
    * @return Returns the m_contactName2.
    */
   public String getContactName2() {
      return m_contactName2;
   }
   /**
    * @param name2 The m_contactName2 to set.
    */
   public void setContactName2(String p_contactName2) {
      m_contactName2 = p_contactName2;
   }
   /**
    * @return Returns the m_contactPhoneNum2.
    */
   public String getContactPhoneNum2() {
      return m_contactPhoneNum2;
   }
   /**
    * @param phoneNum2 The m_contactPhoneNum2 to set.
    */
   public void setContactPhoneNum2(String p_contactPhoneNum2) {
      m_contactPhoneNum2 = p_contactPhoneNum2;
   }
   /**
    * @return Returns the m_contactTitle2.
    */
   public String getContactTitle2() {
      return m_contactTitle2;
   }
   /**
    * @param title2 The m_contactTitle2 to set.
    */
   public void setContactTitle2(String p_contactTitle2) {
      m_contactTitle2 = p_contactTitle2;
   }
   /**
    *  Gets the url for the groupLocator.
    *
    * @return The url.
    */
   public String getUrl() {
      return m_url;
   }

   /**
    *  Sets the url for the groupLocator.
    *
    * @param p_groupNmae The url.
    */
   public void setUrl(String p_url) {
      m_url = p_url;
   }

   /**
    *  Gets the evemts for the groupLocator.
    */
    public Set getEvents() {
        return events;
    }

   /**
    *  Sets the events for the groupLocator.
    */
    public void setEvents(Set events) {
       this.events = events;
    }
   
}


Events.hbm.xml:
Code:
<?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 name="bo.groupEventLocator" table="eventlocator" lazy="false">

<id name="eventCode" column="event_code" type="java.lang.String">
      <generator class="assigned">
      </generator>
</id>

<property name="groupCode" column="group_code" type="java.lang.String" not-null="true"/>
<property name="eventDate" column="event_date" type="java.lang.String" not-null="true"/>
<property name="eventName" column="event_name" type="java.lang.String" not-null="true"/>
<property name="eventCity" column="event_city" type="java.lang.String" not-null="true"/>
<property name="estAttendance" column="est_attendance" type="java.lang.String" not-null="true"/>
<property name="contactName" column="contact_name" type="java.lang.String" not-null="true"/>
<property name="contactPhoneNum" column="contact_phone" type="java.lang.String" not-null="true"/>

<many-to-one name="group" class="bo.groupLocator" column="group_code" not-null="true" insert="false" update="false" />

</class>
</hibernate-mapping>


Events.java:
Code:
package bo;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

public class groupEventLocator {

   private static Logger log = Logger.getLogger(groupEventLocator.class);
      
   private String m_eventCode;
   private String m_groupCode;
   private String m_eventDate = "";
   private String m_eventName = "";
   private String m_eventCity = "";
   private String m_estAttendance = "";
   private String m_contactName;
   private String m_contactPhoneNum;
   private groupLocator group;

   /**
    * @return Returns the m_contactName.
    */
   public String getContactName() {
      return m_contactName;
   }
   /**
    * @param name The m_contactName to set.
    */
   public void setContactName(String p_name) {
      m_contactName = p_name;
   }
   /**
    * @return Returns the m_contactPhoneNum.
    */
   public String getContactPhoneNum() {
      return m_contactPhoneNum;
   }
   /**
    * @param phoneNum The m_contactPhoneNum to set.
    */
   public void setContactPhoneNum(String p_phoneNum) {
      m_contactPhoneNum = p_phoneNum;
   }
   /**
    * @return Returns the m_estAttendance.
    */
   public String getEstAttendance() {
      return m_estAttendance;
   }
   /**
    * @param attendance The m_estAttendance to set.
    */
   public void setEstAttendance(String p_attendance) {
      m_estAttendance = p_attendance;
   }
   /**
    * @return Returns the m_eventCity.
    */
   public String getEventCity() {
      return m_eventCity;
   }
   /**
    * @param city The m_eventCity to set.
    */
   public void setEventCity(String p_city) {
      m_eventCity = p_city;
   }
   /**
    * @return Returns the m_eventDate.
    */
   public String getEventDate() {
      return m_eventDate;
   }
   /**
    * @param date The m_eventDate to set.
    */
   public void setEventDate(String p_date) {
      m_eventDate = p_date;
   }
   /**
    * @return Returns the m_eventName.
    */
   public String getEventName() {
      return m_eventName;
   }
   /**
    * @param name The m_eventName to set.
    */
   public void setEventName(String p_name) {
      m_eventName = p_name;
   }
   /**
    * @return Returns the m_groupCode.
    */
   public String getgroupCode() {
      return m_groupCode;
   }
   /**
    * @param code The m_groupCode to set.
    */
   public void setgroupCode(String p_code) {
      m_groupCode = p_code;
   }
   /**
    * @return Returns the m_eventCode.
    */
   public String getEventCode() {
      return m_eventCode;
   }
   /**
    * @param code The m_eventCode to set.
    */
   public void setEventCode(String p_event) {
      m_eventCode = p_event;
   }
   /**
    * @return Returns the group.
    */
   public groupLocator getgroup() {
      return group;
   }
   /**
    * @param group The group to set.
    */
   public void setgroup(groupLocator group) {
      this.group = group;
   }
}


Form.java snippet:
I get the events to the form like this:
Code:
private Set events = new HashSet();
getEvents();


displayForm.jsp snippet:
The current code that is in place gets all the information about the group and displays it in text boxes on the JSP:
Code:
            <div id="contact_infoL">               
            <label for="contactName"><span class="required">*</span>
                      <logic:messagesPresent property="contactName">
                         <span class="error">Contact Name</span>
                      </logic:messagesPresent>
                      <logic:messagesNotPresent property="contactName">
                         Contact Name
                      </logic:messagesNotPresent>
            </label>
                <html:text property="contactName" styleId="contactName" tabindex="<%=(new Integer(tabIndex++)).toString()%>" styleClass="box1" title="Enter Contact Name, Required"  />
            <html:errors property="contactName" />
         </div>


Name and version of the database you are using:
Oracle 10g

I hope I have provided you with enough information to help! Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 10:44 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
an iterate tag should do enough for you. You will expect Event objects in the list so you can access whatever property you want. Just make sure iterate variable name is the same as list. If I remember correctly it won't set the values back in your list if anything has to be modified at all.



Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.