Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate + JSF 2.0 - save failed: Unknown entity
    primarykey
    data
    text
    <p>I'm trying to send timesheet data to a database using hibernate and JSF 2.0 . I have reverse-engineered my database with DAOs to use in my beans, but I run into the following error when I try to submit: <code>save failed: Unknown entity: com.hibernate.hours.EmpHours</code> and I'm not sure why this is happening. </p> <p>Here we have the form: </p> <pre class="lang-html prettyprint-override"><code>&lt;h:form&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Employee Details&lt;/th&gt; &lt;th id="reqBox" colspan="2"&gt; &lt;span class="reqStar"&gt;*&lt;/span&gt; &lt;span class="reqText"&gt;Required Field&lt;/span&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; Employee ID &lt;br /&gt; &lt;input name="empID" type="text" value="#{userBean.id}" readonly="readonly"/&gt; &lt;/td&gt; &lt;td&gt; Employee Name &lt;br /&gt; &lt;input name="empName" type="text" value="#{userBean.firstName} #{userBean.lastName}" readonly="readonly" /&gt; &lt;/td&gt; &lt;td&gt; Week Starting &lt;span class="reqStar"&gt;*&lt;/span&gt; &lt;br /&gt; &lt;input name="weekOf" type="text" value="" /&gt; &lt;a href="javascript:NewCal('demo1','mmddyyyy')"&gt;&amp;nbsp; &lt;img src="images/cal.gif" border="0" alt="Pick a date" width="16" height="16" /&gt; &lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="whiteSpacerRow"&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;table class="content" border="1" rules="none" frame="box" cellpadding="0" cellspacing="0"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="10"&gt;Record Timesheet&lt;/th&gt; &lt;/tr&gt; &lt;tr class="header"&gt; &lt;td&gt;Task&lt;/td&gt; &lt;td&gt;Monday&lt;/td&gt; &lt;td&gt;Tuesday&lt;/td&gt; &lt;td&gt;Wednesday&lt;/td&gt; &lt;td&gt;Thursday&lt;/td&gt; &lt;td&gt;Friday&lt;/td&gt; &lt;td&gt;Saturday&lt;/td&gt; &lt;td&gt;Sunday&lt;/td&gt; &lt;td&gt;Total Hours&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="dateRow"&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td id="monday"&gt;&lt;/td&gt; &lt;td id="tuesday"&gt;&lt;/td&gt; &lt;td id="wednesday"&gt;&lt;/td&gt; &lt;td id="thursday"&gt;&lt;/td&gt; &lt;td id="friday"&gt;&lt;/td&gt; &lt;td id="saturday"&gt;&lt;/td&gt; &lt;td id="sunday"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="input_row_1"&gt; &lt;td&gt; &lt;select name="select_1"&gt; &lt;option&gt;-Select One-&lt;/option&gt; &lt;option value="Development"&gt;Regular Hours&lt;/option&gt; &lt;option value="Vacation"&gt;Paid Vacation&lt;/option&gt; &lt;option value="LOSSOFPAY"&gt;Unpaid Vacation&lt;/option&gt; &lt;option value="REMOTE"&gt;Remote Work&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td id="mondayInput1"&gt; &lt;h:inputText id="monday1" size="4" value="#{timesheet.monday1}" maxlength="4" required="false" /&gt; &lt;!-- &lt;input name="monday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="tuesdayInput1"&gt; &lt;h:inputText id="tuesday1" size="4" value="#{timesheet.tuesday1}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="tuesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="wednesdayInput1"&gt; &lt;h:inputText id="wednesday1" size="4" value="#{timesheet.wednesday1}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="wednesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="thursdayInput1"&gt; &lt;h:inputText id="thursday1" size="4" value="#{timesheet.thursday1}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="thursday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="fridayInput1"&gt; &lt;h:inputText id="friday1" size="4" value="#{timesheet.friday1}" maxlength="4" required="false" &gt; &lt;f:ajax event="change" render="total1" /&gt; &lt;/h:inputText&gt; &lt;!--&lt;input name="friday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="saturdayInput1"&gt; &lt;h:inputText id="saturday1" size="4" value="#{timesheet.saturday1}" maxlength="4" required="false" &gt; &lt;f:ajax event="change" render="total1" listener="#{timesheet.updateTotal1}" /&gt; &lt;/h:inputText&gt; &lt;!--&lt;input name="saturday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="sundayInput1"&gt; &lt;!--&lt;h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" maxlength="4" required="false" /&gt;--&gt; &lt;h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" maxlength="4" required="false" &gt; &lt;f:ajax event="keyup" render="total1" /&gt; &lt;/h:inputText&gt; &lt;!--&lt;input name="sunday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td&gt; &lt;!-- &lt;h:inputText id="total1updater" size="4" value="#{timesheet.total1}" maxlength="4" required="false" readonly="true" &gt; &lt;f:ajax event="change" render="total1" /&gt; &lt;/h:inputText&gt; --&gt; &lt;h:outputText id="total1" value="#{timesheet.total1}" /&gt; &lt;!-- &lt;input name="total1" id="total1" type="text" size="5" readonly="readonly" /&gt; --&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr id="input_row_2"&gt; &lt;td&gt; &lt;select name="select_2"&gt; &lt;option&gt;-Select One-&lt;/option&gt; &lt;option value="Development"&gt;Regular Hours&lt;/option&gt; &lt;option value="Vacation"&gt;Paid Vacation&lt;/option&gt; &lt;option value="LOSSOFPAY"&gt;Unpaid Vacation&lt;/option&gt; &lt;option value="REMOTE"&gt;Remote Work&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td id="mondayInput2"&gt; &lt;h:inputText id="monday2" size="4" value="#{timesheet.monday2}" maxlength="4" required="false" /&gt; &lt;!-- &lt;input name="monday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="tuesdayInput2"&gt; &lt;h:inputText id="tuesday2" size="4" value="#{timesheet.tuesday2}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="tuesday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="wednesdayInput2"&gt; &lt;h:inputText id="wednesday2" size="4" value="#{timesheet.wednesday2}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="wednesday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="thursdayInput2"&gt; &lt;h:inputText id="thursday2" size="4" value="#{timesheet.thursday2}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="thursday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="fridayInput2"&gt; &lt;h:inputText id="friday2" size="4" value="#{timesheet.friday2}" maxlength="4" required="false" onchange="setTotal2()"/&gt; &lt;!--&lt;input name="friday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="saturdayInput2"&gt; &lt;h:inputText id="saturday2" size="4" value="#{timesheet.saturday2}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="saturday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td id="sundayInput2"&gt; &lt;h:inputText id="sunday2" size="4" value="#{timesheet.sunday2}" maxlength="4" required="false" /&gt; &lt;!--&lt;input name="sunday2" class="smallInput" type="number" size="2" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td&gt; &lt;h:inputText id="total2" size="4" value="#{timesheet.total2}" maxlength="4" required="false" readonly="true"/&gt; &lt;!-- &lt;input name="total2" id="total2" type="text" size="5" readonly="readonly" /&gt; --&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="green"&gt;&lt;th colspan="10"&gt;Expenses&lt;/th&gt;&lt;/tr&gt; &lt;tr class="header"&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td&gt;Air Fare&lt;/td&gt; &lt;td&gt;Lodging&lt;/td&gt; &lt;td&gt;Car Rental&lt;/td&gt; &lt;td&gt;Per-Diem&lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td&gt;Total Expenses&lt;/td&gt; &lt;/tr&gt; &lt;tr class="greenSpacerRow"&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td&gt; &lt;h:inputText id="airFare" size="6" value="#{timesheet.airFare}" maxlength="8" required="false" /&gt; &lt;!-- &lt;input name="airFare" class="smallInput" type="number" size="4" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td&gt; &lt;h:inputText id="lodging" size="6" value="#{timesheet.lodging}" maxlength="8" required="false" /&gt; &lt;!-- &lt;input name="lodging" class="smallInput" type="number" size="4" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td&gt; &lt;h:inputText id="carRental" size="6" value="#{timesheet.carRental}" maxlength="8" required="false" /&gt; &lt;!-- &lt;input name="carRental" class="smallInput" type="number" size="4" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td&gt; &lt;h:inputText id="perDiem" size="6" value="#{timesheet.perDiem}" maxlength="8" required="false" /&gt; &lt;!-- &lt;input name="perDiem" class="smallInput" type="number" size="4" maxlength="4" min="0"/&gt; --&gt; &lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td class="blank"&gt;&lt;/td&gt; &lt;td&gt; &lt;input name="expenseTotal" id="expenseTot" class="smallInput" type="text" size="4" maxlength="4" min="0" readonly="readonly"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;table class="content" border="0" cellpadding="0" cellspacing="0"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;h:commandButton id="submit" action="#{timesheet.submit}" value="Submit" /&gt; &lt;h:commandButton id="cancel" action="#{timesheet.cancel}" value="Cancel" /&gt; &lt;h:commandButton id="Display_Timesheets" action="#{timesheet.displayTimesheets}" value="Display Entered Timesheets" /&gt; &lt;!-- &lt;input type="button" onClick="submitTimesheet()" value="Submit" /&gt; --&gt; &lt;!-- &lt;input type="button" onClick="cancel()" value="Cancel" /&gt; --&gt; &lt;!-- &lt;input type="button" onClick="displayTimesheets()" value="Display Entered Timesheets" /&gt; --&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/h:form&gt; </code></pre> <p>my bean.submit():</p> <pre class="lang-java prettyprint-override"><code>public void submit() { setTotal1(); setTotal2(); User user = new User(); user.setID( userBean.getId() ); user.setUsername( userBean.getUsername() ); if( total1 &gt; 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 &gt; 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 ); // &lt;-- 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; } </code></pre> <p>and my DAO files: </p> <pre class="lang-java prettyprint-override"><code>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); // &lt;-- 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; } } } </code></pre> <p>and </p> <pre class="lang-java prettyprint-override"><code>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); } } </code></pre> <p>and hibernate mapping for EmpHours:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;!-- Mapping file autogenerated by MyEclipse Persistence Tools --&gt; &lt;hibernate-mapping&gt; &lt;class name="com.hibernate.hours.EmpHours" table="emp_hours" catalog="secondsql"&gt; &lt;id name="timesheetId" type="java.lang.Long"&gt; &lt;column name="Timesheet_ID" /&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;many-to-one name="user" class="com.hibernate.hours.User" fetch="select"&gt; &lt;column name="ID" length="10" /&gt; &lt;/many-to-one&gt; &lt;property name="weekOf" type="java.util.Date"&gt; &lt;column name="WeekOf" length="10" /&gt; &lt;/property&gt; &lt;property name="workType" type="java.lang.String"&gt; &lt;column name="Work_Type" length="20" /&gt; &lt;/property&gt; &lt;property name="monday" type="java.lang.Integer"&gt; &lt;column name="Monday" /&gt; &lt;/property&gt; &lt;property name="tuesday" type="java.lang.Integer"&gt; &lt;column name="Tuesday" /&gt; &lt;/property&gt; &lt;property name="wednesday" type="java.lang.Integer"&gt; &lt;column name="Wednesday" /&gt; &lt;/property&gt; &lt;property name="thursday" type="java.lang.Integer"&gt; &lt;column name="Thursday" /&gt; &lt;/property&gt; &lt;property name="friday" type="java.lang.Integer"&gt; &lt;column name="Friday" /&gt; &lt;/property&gt; &lt;property name="saturday" type="java.lang.Integer"&gt; &lt;column name="Saturday" /&gt; &lt;/property&gt; &lt;property name="sunday" type="java.lang.Integer"&gt; &lt;column name="Sunday" /&gt; &lt;/property&gt; &lt;property name="weekTotal" type="java.lang.Integer"&gt; &lt;column name="Week_Total" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload