Please forgive my confusion but I have been struggling with implementing a
simple database driven dropdown list box for several days now.  I use
Hibernate and Spring along with JSF in this application.

1st, I do not understand why I must map a Center/Contractor object instead
of just the Long value of these objects that will go in the database table.

The entire list of both Centers and Contractors are initialized in an
application scoped bean as Lists of SelectItems objects.  Both are merely
used as lookup tables and never change.

It just seems to me that this is far too dificult for something that should
be very simple to implement. 

I'm hoping someone out there will have mercy on me and inform me of exactly
how to use a selectOneMenu when using Hibernate.


Here are some details:

My User business object:
public class User implements Serializable {

  private String username;
  private Long userId;
  private String phone;
  private String project;
  private boolean userContractor;
  private String contractNumber;
  private Center center;
  private Contractor contractor;

... getters/setters


My User Managed Bean (backing bean):

public class UserBean extends BaseBean {

  private String username;
  private Long userId;
  private String phone;
  private String project;
  private boolean userContractor;
  private String contractNumber;
  private Center center;
  private Contractor contractor;

... getters/setters and an action method or two

This class has an action  method used to convert a UserBean object into a
User object and save it using hibernate.  This is probably the first point
of failure.  The properties of User that are represented by drop down menus
in the UI are always null when Hibernate tries to save the User object.


Database Table for User:

CREATE TABLE SUBMITTAL_USER (
        USER_ID NUMBER NOT NULL ,
        CENTER_ID NUMBER  NOT NULL ,
        USERNAME VARCHAR2(8) NOT NULL ,
        PHONE VARCHAR2(11) NOT NULL ,
        PROJECT VARCHAR2(255),
        IS_CONTRACTOR CHAR(1) DEFAULT 0 NOT NULL  CHECK (IS_CONTRACTOR IN (0,1) 
) ,
        CONTRACTOR_ID NUMBER,
        CONTRACT_NUMBER VARCHAR2(32)) 

Database Table for Contractor:
CREATE TABLE CONTRACTOR (
        CONTRACTOR_ID NUMBER NOT NULL ,
        CONTRACTOR_NAME VARCHAR2(255) NOT NULL ) 
;

Database Table for Center:
CREATE TABLE CENTER (
        CENTER_ID NUMBER NOT NULL ,
        NAME VARCHAR2(20) UNIQUE ) 
;

Mapping Documents:

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
2.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping
package="gov.nasa.msfc.repository.submittal.model.businessobject">
  <class name="User" table="SUBMITTAL_USER" >
    <cache usage="read-write"/>
    <id name="userId" column="USER_ID" unsaved-value="null" type="long" >
      <generator class="sequence">
        <param name="sequence">submittal_user_seq</param>
      </generator>
    </id>
    <many-to-one name="center" class="Center" column="CENTER_ID"
cascade="none" not-null="true"  />
    <property name="username" column="USERNAME"/>
    <property name="phone" column="PHONE"/>
    <property name="project" column="PROJECT"/>
    <property name="userContractor" column="IS_CONTRACTOR"/>
    <many-to-one name="contractor" class="Contractor" column="CONTRACTOR_ID"
cascade="none" />
    <property name="contractNumber" column="CONTRACT_NUMBER"/>
  </class>
</hibernate-mapping>

Center.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
2.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping
package="gov.nasa.msfc.repository.submittal.model.businessobject">
  <class name="Center" table="CENTER">
    <cache usage="read-only"/>
    <id name="centerId" column="CENTER_ID" unsaved-value="null" type="long" >
      <generator class="sequence">
        <param name="sequence">submittal_cntr_seq</param>
      </generator>
    </id>
    <property name="name" column="NAME"  />
  </class>
</hibernate-mapping>

Contractor.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
2.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping
package="gov.nasa.msfc.repository.submittal.model.businessobject">
  <class name="Contractor" table="CONTRACTOR">
    <id name="contractorId" column="CONTRACTOR_ID" unsaved-value="null">
      <generator class="sequence">
        <param name="sequence">submittal_cont_seq</param>
      </generator>
    </id>
    <property name="name" column="CONTRACTOR_NAME"/>
  </class>
</hibernate-mapping>


Thanks,
-Mark




Reply via email to