我想創建新的事件對象,通過Hibernate在數據庫中保存。下面的代碼成功創建了一個事件,但是當session.save(evt)被調用時,我得到的返回值爲0(我認爲這相當於null)。任何人都可以確定下面的問題是什麼?如果您需要有關涉及類或hibernate映射文件的更多信息,請請求您的需求。休眠:持久對象
注意:假設數據庫中存在事件表。
public class ScheduleUnitOfWork {
private Map<Service, Employee> assignments;
private Date startDate;
private Date endDate;
private String customerName;
public ScheduleUnitOfWork(Map<Service, Employee> assignments, Date startDate,
Date endDate, String customerName) {
super();
this.assignments = assignments;
this.startDate = startDate;
this.endDate = endDate;
this.customerName = customerName;
}
public boolean scheduleEmployees(){
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
Transaction tx = null;
boolean retVal = true;
try {
tx = session.beginTransaction();
for(Service s : assignments.keySet()){
Employee e = assignments.get(s);
Event evt = new Event();
evt.setAssignedService(s);
evt.setCustomerName(this.customerName);
evt.setEndDate(endDate);
evt.setStartDate(startDate);
System.out.println(session.save(evt));
}
tx.commit();
}catch(RuntimeException ex){
ex.printStackTrace();
if(tx != null){
tx.rollback();
}
session.close();
retVal = false;
}
return retVal;
}
}
<class name="Event" table="Events">
<id name="ID" column="ID" type="int">
<generator class="assigned"/>
</id>
<property name="startDate" column="startDate" type="timestamp"/>
<property name="endDate" column="endDate" type="timestamp"/>
<property name="customerName" column="customerName" type="string"/>
<many-to-one name="assignedService" column="serviceID"
class="org.hibernate.service.Service"
unique="true" not-null="true"/>
</class>
查看Event的映射將有助於(.hbm.xml或帶註釋的類)。你有沒有爲它的ID指定一個生成器? – ChssPly76 2009-11-08 03:05:21
我在上面的問題中添加了休眠文件中的類定義。 – jds2501 2009-11-08 03:21:39