hibernate

Reply

Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

hibernate

 
0
  #1
Aug 24th, 2008
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.

  1. /*
  2.  * SessionBean1.java
  3.  *
  4.  * Created on Aug 21, 2008, 7:41:35 AM
  5.  */
  6. package hibernatetravelapp;
  7.  
  8. import com.sun.rave.web.ui.appbase.AbstractSessionBean;
  9. import com.sun.webui.jsf.model.Option;
  10. import java.util.List;
  11. import java.util.Set;
  12. import javax.faces.FacesException;
  13. import org.hibernate.Query;
  14. import org.hibernate.Session;
  15. import org.hibernate.Transaction;
  16. import org.hibernate.collection.PersistentSet;
  17. import travel.HibernateUtil;
  18. import travel.Person;
  19. import travel.Trip;
  20.  
  21. /**
  22.  * <p>Session scope data bean for your application. Create properties
  23.  * here to represent cached data that should be made available across
  24.  * multiple HTTP requests for an individual user.</p>
  25.  *
  26.  * <p>An instance of this class will be created for you automatically,
  27.  * the first time your application evaluates a value binding expression
  28.  * or method binding expression that references a managed bean using
  29.  * this class.</p>
  30.  *
  31.  * @author depot
  32.  */
  33. public class SessionBean1 extends AbstractSessionBean {
  34. // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
  35. /**
  36.   * <p>Automatically managed component initialization. <strong>WARNING:</strong>
  37.   * This method is automatically generated, so any user-specified code inserted
  38.   * here is subject to being replaced.</p>
  39.   */
  40. private void _init() throws Exception {
  41. }
  42. // </editor-fold>
  43. /**
  44.   * <p>Construct a new session data bean instance.</p>
  45.   */
  46. public SessionBean1() {
  47. }
  48. private Option[] personOptions;
  49. private Integer selectedPersonId;
  50. private Trip[] trips4Person;
  51.  
  52. /**
  53.   * Get the value of trips4Person
  54.   *
  55.   * @return the value of trips4Person
  56.   */
  57. public Trip[] getTrips4Person() {
  58. return trips4Person;
  59. }
  60.  
  61. /**
  62.   * Set the value of trips4Person
  63.   *
  64.   * @param trips4Person new value of trips4Person
  65.   */
  66. public void setTrips4Person(Trip[] trips4Person) {
  67. this.trips4Person = trips4Person;
  68. }
  69.  
  70. /**
  71.   * Get the value of selectedPersonId
  72.   *
  73.   * @return the value of selectedPersonId
  74.   */
  75. public Integer getSelectedPersonId() {
  76. return selectedPersonId;
  77. }
  78.  
  79. /**
  80.   * Set the value of selectedPersonId
  81.   *
  82.   * @param selectedPersonId new value of selectedPersonId
  83.   */
  84. public void setSelectedPersonId(Integer selectedPersonId) {
  85. this.selectedPersonId = selectedPersonId;
  86. updateTrips4Person();
  87. }
  88.  
  89. /**
  90.   * Get the value of personOptions
  91.   *
  92.   * @return the value of personOptions
  93.   */
  94. public Option[] getPersonOptions() {
  95. return personOptions;
  96. }
  97.  
  98. /**
  99.   * Set the value of personOptions
  100.   *
  101.   * @param personOptions new value of personOptions
  102.   */
  103. public void setPersonOptions(Option[] personOptions) {
  104. this.personOptions = personOptions;
  105. }
  106.  
  107. /**
  108.   * <p>This method is called when this bean is initially added to
  109.   * session scope. Typically, this occurs as a result of evaluating
  110.   * a value binding or method binding expression, which utilizes the
  111.   * managed bean facility to instantiate this bean and store it into
  112.   * session scope.</p>
  113.   *
  114.   * <p>You may customize this method to initialize and cache data values
  115.   * or resources that are required for the lifetime of a particular
  116.   * user session.</p>
  117.   */
  118. @Override
  119. public void init() {
  120. super.init();
  121.  
  122. try {
  123. _init();
  124. } catch (Exception e) {
  125. log("SessionBean1 Initialization Failure", e);
  126. throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
  127. }
  128. // Fill in the personOptions[]
  129. buildPersonOptions();
  130.  
  131. }
  132.  
  133. /**
  134.   * <p>This method is called when the session containing it is about to be
  135.   * passivated. Typically, this occurs in a distributed servlet container
  136.   * when the session is about to be transferred to a different
  137.   * container instance, after which the <code>activate()</code> method
  138.   * will be called to indicate that the transfer is complete.</p>
  139.   *
  140.   * <p>You may customize this method to release references to session data
  141.   * or resources that can not be serialized with the session itself.</p>
  142.   */
  143. @Override
  144. public void passivate() {
  145. }
  146.  
  147. /**
  148.   * <p>This method is called when the session containing it was
  149.   * reactivated.</p>
  150.   *
  151.   * <p>You may customize this method to reacquire references to session
  152.   * data or resources that could not be serialized with the
  153.   * session itself.</p>
  154.   */
  155. @Override
  156. public void activate() {
  157. }
  158.  
  159. /**
  160.   * <p>This method is called when this bean is removed from
  161.   * session scope. Typically, this occurs as a result of
  162.   * the session timing out or being terminated by the application.</p>
  163.   *
  164.   * <p>You may customize this method to clean up resources allocated
  165.   * during the execution of the <code>init()</code> method, or
  166.   * at any later time during the lifetime of the application.</p>
  167.   */
  168. @Override
  169. public void destroy() {
  170. }
  171.  
  172. /**
  173.   * <p>Return a reference to the scoped data bean.</p>
  174.   *
  175.   * @return reference to the scoped data bean
  176.   */
  177. protected ApplicationBean1 getApplicationBean1() {
  178. return (ApplicationBean1) getBean("ApplicationBean1");
  179. }
  180.  
  181. private void buildPersonOptions() {
  182. List<Person> personList = null;
  183. try {
  184. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  185. Transaction tx = session.beginTransaction();
  186. Query q = session.createQuery("from Person");
  187. personList = (List<Person>) q.list();
  188.  
  189. } catch (Exception e) {
  190. e.printStackTrace();
  191. }
  192.  
  193. personOptions = new Option[personList.size()];
  194. int i = 0;
  195. for (Person person : personList) {
  196. Option opt = new Option(person.getPersonId(), person.getName());
  197. personOptions[i++] = opt;
  198. }
  199. }
  200.  
  201. private void updateTrips4Person() {
  202. if (selectedPersonId == null) {
  203. trips4Person = new Trip[1];
  204. trips4Person[0] = new Trip();
  205. return;
  206. }
  207.  
  208. Set personTrips = null;
  209. try {
  210. Session session =
  211. HibernateUtil.getSessionFactory().getCurrentSession();
  212. Transaction tx = session.beginTransaction();
  213. Person person = (Person) session.load(Person.class, selectedPersonId);
  214. personTrips = (PersistentSet) person.getTrips();
  215.  
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. }
  219.  
  220. trips4Person = (Trip[]) personTrips.toArray(new Trip[0]);
  221. }
  222. }

I see in the JSF it controls the table rows

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Document : Page1
  4. Created on : Aug 21, 2008, 7:41:36 AM
  5. Author : depot
  6. -->
  7. <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">
  8. <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
  9. <f:view>
  10. <webuijsf:page id="page1">
  11. <webuijsf:html id="html1">
  12. <webuijsf:head id="head1">
  13. <webuijsf:link id="link1" url="/resources/stylesheet.css"/>
  14. </webuijsf:head>
  15. <webuijsf:body id="body1" style="-rave-layout: grid">
  16. <webuijsf:form id="form1">
  17. <webuijsf:messageGroup id="messageGroup1" style="position: absolute; left: 384px; top: 432px"/>
  18. <webuijsf:table augmentTitle="false" id="table1" style="left: 144px; top: 168px; position: absolute; width: 0px" title="Table" width="0">
  19. <webuijsf:tableRowGroup id="tableRowGroup1" rows="10" sourceData="#{SessionBean1.trips4Person}" sourceVar="currentRow">
  20. <webuijsf:tableColumn headerText="tripId" id="tableColumn1" sort="tripId">
  21. <webuijsf:staticText id="staticText1" text="#{currentRow.value['tripId']}"/>
  22. </webuijsf:tableColumn>
  23. <webuijsf:tableColumn headerText="depCity" id="tableColumn2" sort="depCity">
  24. <webuijsf:staticText id="staticText2" text="#{currentRow.value['depCity']}"/>
  25. </webuijsf:tableColumn>
  26. <webuijsf:tableColumn headerText="depDate" id="tableColumn3" sort="depDate">
  27. <webuijsf:staticText id="staticText3" text="#{currentRow.value['depDate']}"/>
  28. </webuijsf:tableColumn>
  29. <webuijsf:tableColumn headerText="destCity" id="tableColumn4" sort="destCity">
  30. <webuijsf:staticText id="staticText4" text="#{currentRow.value['destCity']}"/>
  31. </webuijsf:tableColumn>
  32. <webuijsf:tableColumn headerText="tripTypeId" id="tableColumn5" sort="tripTypeId">
  33. <webuijsf:staticText id="staticText5" text="#{currentRow.value['tripTypeId']}"/>
  34. </webuijsf:tableColumn>
  35. </webuijsf:tableRowGroup>
  36. </webuijsf:table>
  37. <webuijsf:dropDown binding="#{Page1.dropDown1}" id="dropDown1" items="#{SessionBean1.personOptions}"
  38. onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'dropDown1');" selected="#{SessionBean1.selectedPersonId}" style="position: absolute; left: 120px; top: 72px"/>
  39. </webuijsf:form>
  40. </webuijsf:body>
  41. </webuijsf:html>
  42. </webuijsf:page>
  43. </f:view>
  44. </jsp:root>
maybe I should start by understanding elements in the xml Person.hbm





  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  3. <hibernate-mapping>
  4. <class dynamic-insert="false" dynamic-update="false"
  5. mutable="true" name="travel.Person" optimistic-lock="version"
  6. polymorphism="implicit" select-before-update="false" table="PERSON">
  7. <id column="PERSONID" name="personId">
  8. <generator class="increment"/>
  9. </id>
  10. <property column="NAME" name="name"/>
  11. <property column="JOBTITLE" name="jobTitle"/>
  12. <property column="FREQUENTFLYER" name="frequentFlyer"/>
  13. <set cascade="all-delete-orphan" inverse="true" lazy="true" name="trips" table="TRIP">
  14. <key column="PERSONID"/>
  15. <one-to-many class="travel.Trip"/>
  16. </set>
  17. </class>
  18. </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
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package schedule;
  7.  
  8. /**
  9.  *
  10.  * @author depot
  11.  */
  12. public class Teacher {
  13. private int instrNum;
  14. private String instructor;
  15. private String state;
  16.  
  17. public int getInstrNum() {
  18. return instrNum;
  19. }
  20.  
  21. public void setInstrNum(int instrNum) {
  22. this.instrNum = instrNum;
  23. }
  24.  
  25. public String getInstructor() {
  26. return instructor;
  27.  
  28. }
  29.  
  30. public void setInstructor(String instructor) {
  31. this.instructor = instructor;
  32. }
  33.  
  34. public String getState() {
  35. return state;
  36. }
  37.  
  38. public void setState(String state) {
  39. this.state = state;
  40. }
  41.  
  42.  
  43. }
this is the .hbm.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  3. <hibernate-mapping>
  4. <class dynamic-insert="false" dynamic-update="false" mutable="true" name="schedule.Teacher" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
  5. <id column="instr_num" name="instructorId">
  6. <generator class="increment"/>
  7. </id>
  8. <!-- <property column="instr_num" name="instructorId"> -->
  9. <property column="instr_lname" name="instructor"/>
  10. <property column="instr_state" name="state"/>
  11. <set cascade="all-delete-orphan" inverse="true" lazy="true" name="instructorId" table="booking">
  12. <key column="instr_num"/>
  13. <one-to-many class="schedule.Booking"/>
  14. </set>
  15.  
  16. </class>
  17. </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
  1. <id column="instr_num" name="instructorId">
  2. <generator class="increment"/>
  3. </id>
I am not sure why this would be different from the other properties

and concerning the code
  1. <set cascade="all-delete-orphan" inverse="true" lazy="true" name="instructorId" table="booking">
  2. <key column="instr_num"/>
  3. <one-to-many class="schedule.Booking"/>
  4. </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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

HibernateTravelApp tutorial

 
-1
  #2
Aug 24th, 2008
Has any one worked with the HibernateTravelApp tutorial
-Steve
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 410
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: HibernateTravelApp tutorial

 
0
  #3
Aug 24th, 2008
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.
Last edited by sciwizeh; Aug 24th, 2008 at 8:52 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC