hibernate + JSF 2.0 - 保存失败:未知实体

时间:2012-08-02 20:33:50

标签: hibernate jsf entity

我正在尝试使用hibernate和JSF 2.0将时间表数据发送到数据库。我已经使用DAO对我的数据库进行逆向工程以在我的bean中使用,但是当我尝试提交时遇到以下错误:save failed: Unknown entity: com.hibernate.hours.EmpHours我不确定为什么会发生这种情况。

我们有以下表格:

<h:form>
    <table>
        <thead>
                  <tr>
                      <th>Employee Details</th>
                      <th id="reqBox" colspan="2">
                          <span class="reqStar">*</span>
                          <span class="reqText">Required Field</span>
                      </th>
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      <td>
                          Employee ID <br />
                          <input name="empID" type="text" value="#{userBean.id}" readonly="readonly"/>
                      </td>
                      <td>
                          Employee Name <br />
                          <input name="empName" type="text" value="#{userBean.firstName} #{userBean.lastName}" readonly="readonly" />
                      </td>
                      <td>
                          Week Starting <span class="reqStar">*</span> <br />
                          <input name="weekOf" type="text" value="" />
                          <a href="javascript:NewCal('demo1','mmddyyyy')">&nbsp;
                              <img src="images/cal.gif" border="0" alt="Pick a date" width="16" height="16" />
                          </a>
                      </td>
                  </tr>
                  <tr class="whiteSpacerRow"></tr>
              </tbody>
          </table>
          <table class="content" border="1" rules="none" frame="box" cellpadding="0" cellspacing="0">
              <thead>
                <tr>
                    <th colspan="10">Record Timesheet</th>
                  </tr>
                  <tr class="header">
                      <td>Task</td>
                      <td>Monday</td>
                      <td>Tuesday</td>
                      <td>Wednesday</td>
                      <td>Thursday</td>
                      <td>Friday</td>
                      <td>Saturday</td>
                      <td>Sunday</td>
                      <td>Total Hours</td>
                  </tr>
              </thead>
              <tbody>
                  <tr class="dateRow">
                      <td class="blank"></td>
                      <td id="monday"></td>
                      <td id="tuesday"></td>
                      <td id="wednesday"></td>
                      <td id="thursday"></td>
                      <td id="friday"></td>
                      <td id="saturday"></td>
                      <td id="sunday"></td>
                  </tr>
            <tr id="input_row_1">
                <td>
                    <select name="select_1">
                        <option>-Select One-</option>
                        <option value="Development">Regular Hours</option>
                        <option value="Vacation">Paid Vacation</option>
                        <option value="LOSSOFPAY">Unpaid Vacation</option>
                        <option value="REMOTE">Remote Work</option>
                    </select>
                </td>
                <td id="mondayInput1">
                    <h:inputText id="monday1" size="4" value="#{timesheet.monday1}" maxlength="4" required="false" />
                    <!-- <input name="monday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="tuesdayInput1">
    <h:inputText id="tuesday1" size="4" value="#{timesheet.tuesday1}" maxlength="4" required="false" />
    <!--<input name="tuesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="wednesdayInput1">
    <h:inputText id="wednesday1" size="4" value="#{timesheet.wednesday1}" maxlength="4" required="false" />
    <!--<input name="wednesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="thursdayInput1">
    <h:inputText id="thursday1" size="4" value="#{timesheet.thursday1}" maxlength="4" required="false" />
    <!--<input name="thursday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
  </td>
  <td id="fridayInput1">
      <h:inputText id="friday1" size="4" value="#{timesheet.friday1}" maxlength="4" required="false" >
        <f:ajax event="change" render="total1" />
</h:inputText>
      <!--<input name="friday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
  </td>
  <td id="saturdayInput1">
      <h:inputText id="saturday1" size="4" value="#{timesheet.saturday1}" maxlength="4" required="false" >
        <f:ajax event="change" render="total1" listener="#{timesheet.updateTotal1}" />
</h:inputText>
      <!--<input name="saturday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="sundayInput1">
    <!--<h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" maxlength="4" required="false" />-->
      <h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" maxlength="4" required="false" >
    <f:ajax event="keyup" render="total1" />
</h:inputText>
      <!--<input name="sunday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td>
  <!--  <h:inputText id="total1updater" size="4" value="#{timesheet.total1}" maxlength="4" required="false" readonly="true" >
    <f:ajax event="change" render="total1" />
</h:inputText> --> 
<h:outputText id="total1" value="#{timesheet.total1}" />
      <!-- <input name="total1" id="total1" type="text" size="5" readonly="readonly" /> -->
    </td>
</tr>
               <tr id="input_row_2">
                   <td>
                       <select name="select_2">
                           <option>-Select One-</option>
                           <option value="Development">Regular Hours</option>
                           <option value="Vacation">Paid Vacation</option>
                           <option value="LOSSOFPAY">Unpaid Vacation</option>
                           <option value="REMOTE">Remote Work</option>
                       </select>
                   </td>
                   <td id="mondayInput2">
                    <h:inputText id="monday2" size="4" value="#{timesheet.monday2}" maxlength="4" required="false" />
                       <!-- <input name="monday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="tuesdayInput2">
         <h:inputText id="tuesday2" size="4" value="#{timesheet.tuesday2}" maxlength="4" required="false" />
         <!--<input name="tuesday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="wednesdayInput2">
        <h:inputText id="wednesday2" size="4" value="#{timesheet.wednesday2}" maxlength="4" required="false" />
         <!--<input name="wednesday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="thursdayInput2">
         <h:inputText id="thursday2" size="4" value="#{timesheet.thursday2}" maxlength="4" required="false" />
         <!--<input name="thursday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="fridayInput2">
         <h:inputText id="friday2" size="4" value="#{timesheet.friday2}" maxlength="4" required="false" onchange="setTotal2()"/>
         <!--<input name="friday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="saturdayInput2">
         <h:inputText id="saturday2" size="4" value="#{timesheet.saturday2}" maxlength="4" required="false" />
         <!--<input name="saturday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td id="sundayInput2">
         <h:inputText id="sunday2" size="4" value="#{timesheet.sunday2}" maxlength="4" required="false" />
         <!--<input name="sunday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
     </td>
     <td>
         <h:inputText id="total2" size="4" value="#{timesheet.total2}" maxlength="4" required="false" readonly="true"/>
         <!-- <input name="total2" id="total2" type="text" size="5" readonly="readonly" /> -->
     </td>
 </tr>
 <tr class="green"><th colspan="10">Expenses</th></tr>
 <tr class="header">
     <td class="blank"></td>
     <td>Air Fare</td>
     <td>Lodging</td>
     <td>Car Rental</td>
     <td>Per-Diem</td>
     <td class="blank"></td>
     <td class="blank"></td>
     <td class="blank"></td>
     <td>Total Expenses</td>
 </tr>
 <tr class="greenSpacerRow"></tr>
 <tr>
     <td class="blank"></td>
     <td>
         <h:inputText id="airFare" size="6" value="#{timesheet.airFare}" maxlength="8" required="false" />
         <!-- <input name="airFare" class="smallInput" type="number" size="4" maxlength="4" min="0"/> -->
     </td>
     <td>
         <h:inputText id="lodging" size="6" value="#{timesheet.lodging}" maxlength="8" required="false" />
         <!-- <input name="lodging" class="smallInput" type="number" size="4" maxlength="4" min="0"/> -->
     </td>
     <td>
         <h:inputText id="carRental" size="6" value="#{timesheet.carRental}" maxlength="8" required="false" />
         <!-- <input name="carRental" class="smallInput" type="number" size="4" maxlength="4" min="0"/> -->
     </td>
     <td>
         <h:inputText id="perDiem" size="6" value="#{timesheet.perDiem}" maxlength="8" required="false" />
         <!-- <input name="perDiem" class="smallInput" type="number" size="4" maxlength="4" min="0"/> -->
            </td>
            <td class="blank"></td>
            <td class="blank"></td>
            <td class="blank"></td>
            <td>
                <input name="expenseTotal" id="expenseTot" class="smallInput" type="text" size="4" maxlength="4" min="0" readonly="readonly"/>
            </td>
        </tr>
    </tbody>
</table>
<table class="content" border="0" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td>
                <h:commandButton id="submit" action="#{timesheet.submit}" value="Submit" /> 
                <h:commandButton id="cancel" action="#{timesheet.cancel}" value="Cancel" />
                <h:commandButton id="Display_Timesheets" action="#{timesheet.displayTimesheets}" value="Display Entered Timesheets" />
                <!-- <input type="button" onClick="submitTimesheet()" value="Submit" /> -->
         <!-- <input type="button" onClick="cancel()" value="Cancel" /> -->
         <!-- <input type="button" onClick="displayTimesheets()" value="Display Entered Timesheets" /> -->
                     </td>
                 </tr>
             </tbody>
</table>
     </h:form>

我的bean.submit():

public void submit() {

    setTotal1();
    setTotal2();

    User user = new User();
    user.setID( userBean.getId() );
    user.setUsername( userBean.getUsername() );

    if( total1 > 0 ){

        EmpHours hours1 = new EmpHours();
        hours1.setUser( user );
        hours1.setWeekOf( getDate() );
        hours1.setWorkType( getWorkType1() );
        hours1.setMonday( getMonday1() );
        hours1.setTuesday( getTuesday1() );
        hours1.setWednesday( getWednesday1() );
        hours1.setThursday( getThursday1() );
        hours1.setFriday( getFriday1() );
        hours1.setSaturday( getSaturday1() );
        hours1.setSunday( getSunday1() );
        hours1.setWeekTotal( total1 );
        send(hours1);
    }

    if( total2 > 0 ){

        EmpHours hours2 = new EmpHours();
        hours2.setUser( user );
        hours2.setWeekOf( getDate() );
        hours2.setWorkType( getWorkType2() );
        hours2.setMonday( getMonday2() );
        hours2.setTuesday( getTuesday2() );
        hours2.setWednesday( getWednesday2() );
        hours2.setThursday( getThursday2() );
        hours2.setFriday( getFriday2() );
        hours2.setSaturday( getSaturday2() );
        hours2.setSunday( getSunday2() );
        hours2.setWeekTotal( total2 );
        send(hours2);
    }

}

private boolean send( EmpHours hours ){

    EmpHoursDAO DAO = new EmpHoursDAO();
    try{
        Transaction tx = DAO.getSession().beginTransaction();
        DAO.save( hours ); // <-- the problem is with this line
        tx.commit();
    } catch(Exception e) {
        System.out.print("send: " + e.getMessage());
        return false;
    } finally {
        DAO.getSession().close();
    }
    return true;
}

和我的DAO文件:

package com.hibernate.hours;

import com.hibernate.user.BaseHibernateDAO;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hibernate.hours.EmpHours;

/**
 * A data access object (DAO) providing persistence and search support for
 * EmpHours entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.hibernate.hours.EmpHours
 * @author MyEclipse Persistence Tools
 */

public class EmpHoursDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory
            .getLogger(EmpHoursDAO.class);
    // property constants
    public static final String WORK_TYPE = "workType";
    public static final String MONDAY = "monday";
    public static final String TUESDAY = "tuesday";
    public static final String WEDNESDAY = "wednesday";
    public static final String THURSDAY = "thursday";
    public static final String FRIDAY = "friday";
    public static final String SATURDAY = "saturday";
    public static final String SUNDAY = "sunday";
    public static final String WEEK_TOTAL = "weekTotal";

    public void save(EmpHours transientInstance) {
        log.debug("saving EmpHours instance");
        try {
            getSession().save(transientInstance);  // <-- and the error comes from here
            log.debug("save successful");
        } catch (RuntimeException re) {
            System.err.println("save failed: " + re.getMessage());
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(EmpHours persistentInstance) {
        log.debug("deleting EmpHours instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public EmpHours findById(java.lang.Long id) {
        log.debug("getting EmpHours instance with id: " + id);
        try {
            EmpHours instance = (EmpHours) getSession().get(
                    "com.hibernate.hours.EmpHours", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(EmpHours instance) {
        log.debug("finding EmpHours instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.hibernate.hours.EmpHours")
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding EmpHours instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from EmpHours as model where model."
                    + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByWorkType(Object workType) {
        return findByProperty(WORK_TYPE, workType);
    }

    public List findByMonday(Object monday) {
        return findByProperty(MONDAY, monday);
    }

    public List findByTuesday(Object tuesday) {
        return findByProperty(TUESDAY, tuesday);
    }

    public List findByWednesday(Object wednesday) {
        return findByProperty(WEDNESDAY, wednesday);
    }

    public List findByThursday(Object thursday) {
        return findByProperty(THURSDAY, thursday);
    }

    public List findByFriday(Object friday) {
        return findByProperty(FRIDAY, friday);
    }

    public List findBySaturday(Object saturday) {
        return findByProperty(SATURDAY, saturday);
    }

    public List findBySunday(Object sunday) {
        return findByProperty(SUNDAY, sunday);
    }

    public List findByWeekTotal(Object weekTotal) {
        return findByProperty(WEEK_TOTAL, weekTotal);
    }

    public List findAll() {
        log.debug("finding all EmpHours instances");
        try {
            String queryString = "from EmpHours";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public EmpHours merge(EmpHours detachedInstance) {
        log.debug("merging EmpHours instance");
        try {
            EmpHours result = (EmpHours) getSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(EmpHours instance) {
        log.debug("attaching dirty EmpHours instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(EmpHours instance) {
        log.debug("attaching clean EmpHours instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}

package com.hibernate.hours;

import java.util.Date;

import com.hibernate.user.User;

/**
 * EmpHours entity. @author MyEclipse Persistence Tools
 */
public class EmpHours extends AbstractEmpHours implements java.io.Serializable {

    // Constructors

    /** default constructor */
    public EmpHours() {
    }

    /** full constructor */
    public EmpHours(User user, Date weekOf, String workType, Integer monday,
            Integer tuesday, Integer wednesday, Integer thursday,
            Integer friday, Integer saturday, Integer sunday, Integer weekTotal) {
        super(user, weekOf, workType, monday, tuesday, wednesday, thursday,
                friday, saturday, sunday, weekTotal);
    }

}

和EmpHours的hibernate映射:

<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->

<hibernate-mapping>
    <class name="com.hibernate.hours.EmpHours" table="emp_hours" catalog="secondsql">
        <id name="timesheetId" type="java.lang.Long">
            <column name="Timesheet_ID" />
            <generator class="native" />
        </id>
        <many-to-one name="user" class="com.hibernate.hours.User" fetch="select">
            <column name="ID" length="10" />
        </many-to-one>
        <property name="weekOf" type="java.util.Date">
            <column name="WeekOf" length="10" />
        </property>
        <property name="workType" type="java.lang.String">
            <column name="Work_Type" length="20" />
        </property>
        <property name="monday" type="java.lang.Integer">
            <column name="Monday" />
        </property>
        <property name="tuesday" type="java.lang.Integer">
            <column name="Tuesday" />
        </property>
        <property name="wednesday" type="java.lang.Integer">
            <column name="Wednesday" />
        </property>
        <property name="thursday" type="java.lang.Integer">
            <column name="Thursday" />
        </property>
        <property name="friday" type="java.lang.Integer">
            <column name="Friday" />
        </property>
        <property name="saturday" type="java.lang.Integer">
            <column name="Saturday" />
        </property>
        <property name="sunday" type="java.lang.Integer">
            <column name="Sunday" />
        </property>
        <property name="weekTotal" type="java.lang.Integer">
            <column name="Week_Total" />
        </property>
    </class>
</hibernate-mapping>

0 个答案:

没有答案