I'm not sure if I've missed something really small and stupid but the example in the documentation is pretty straightforward on how to update an object that has been loaded into the current session. Try as I may I am unable to get the following code to generate the update SQL, it seems the object isn't detected as being dirty despite having called many of it's setters. The insert code path works fine. The list of eligibility objects it's processing was generated from a text file and all of them have 0 for their ID. They are being used as is on insert but their state is copied into a loaded object if it exists (the load succeeds) which according to the documentation should make it dirty and be automatically updated when session.flush() is called or the transaction is commited (I'm only commiting the transaction). So either I've missed something in the manual that covers this particular case or I've found an annoying bug.
Hibernate version:
Version 3.0.3
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pristx.Eligibility" table="eligibility">
<id name="eligibilityId" column="eligibility_id" type="integer">
<generator class="native"/>
</id>
<version name="version" column="version" type="integer"/>
<property name="processDate" column="process_date" type="timestamp"/>
<property name="employeeId" column="employee_id" type="integer"/>
<property name="employeeName" column="employee_name" type="string"/>
<property name="nameSuffix" column="name_suffix" type="string"/>
<property name="address1" column="address_1" type="string"/>
<property name="address2" column="address_2" type="string"/>
<property name="city" column="city" type="string"/>
<property name="state" column="state" type="string"/>
<property name="zipCode" column="zip_code" type="string"/>
<property name="country" column="country" type="string"/>
<property name="phone" column="phone" type="string"/>
<property name="gender" column="gender" type="string"/>
<property name="birthDate" column="birth_date" type="timestamp"/>
<property name="maritalStatus" column="marital_status" type="string"/>
<property name="educationLevel" column="education_level" type="string"/>
<property name="employeeType" column="employee_type" type="string"/>
<property name="departmentName" column="department_name" type="string"/>
<property name="jobCode" column="job_code" type="string"/>
<property name="jobDescription" column="job_description" type="string"/>
<property name="fullPartTime" column="full_part_time" type="string"/>
<property name="hireDate" column="hire_date" type="timestamp"/>
<property name="rehireDate" column="rehire_date" type="timestamp"/>
<property name="employeeStatus" column="employee_status" type="string"/>
<property name="effectiveDate" column="effective_date" type="timestamp"/>
<property name="terminationDate" column="termination_date" type="timestamp"/>
<property name="lastDateWorked" column="last_date_worked" type="timestamp"/>
<property name="standardHours" column="standard_hours" type="java.math.BigDecimal"/>
<property name="averageWeek" column="average_week" type="java.math.BigDecimal"/>
<property name="averageDay" column="average_day" type="java.math.BigDecimal"/>
<property name="daysPerWeek" column="days_per_week" type="integer"/>
<property name="supervisorName" column="supervisor_name" type="string"/>
<property name="locationCode" column="location_code" type="string"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public void save() {
Transaction transaction = new Transaction();
try {
transaction.execute(new InsertOrUpdateEligibilityListCommand(list));
} catch (Exception e) {
e.printStackTrace();
}
}
public class Transaction {
public void execute(Command command) throws Exception {
Session session = SessionManager.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
command.execute();
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw e;
} finally {
SessionManager.closeSession();
}
command.success();
}
}
public class InsertOrUpdateEligibilityListCommand implements Command {
private List<Eligibility> eligibilityList;
public InsertOrUpdateEligibilityListCommand(List<Eligibility> eligibilityList) {
this.eligibilityList = eligibilityList;
}
public void execute() throws Exception {
for (Eligibility eligibility : eligibilityList) {
Eligibility existing = EligibilityDAO.findEligibility(eligibility.getEmployeeId());
if (existing == null) {
eligibility = EligibilityDAO.insertEligibility(eligibility);
} else {
existing.setAddress1(eligibility.getAddress1());
existing.setAddress2(eligibility.getAddress2());
existing.setAverageDay(eligibility.getAverageDay());
existing.setAverageWeek(eligibility.getAverageWeek());
existing.setBirthDate(eligibility.getBirthDate());
existing.setCity(eligibility.getCity());
existing.setCountry(eligibility.getCountry());
existing.setDaysPerWeek(eligibility.getDaysPerWeek());
existing.setDepartmentName(eligibility.getDepartmentName());
existing.setEducationLevel(eligibility.getEducationLevel());
existing.setEffectiveDate(eligibility.getEffectiveDate());
existing.setEmployeeId(eligibility.getEmployeeId());
existing.setEmployeeName(eligibility.getEmployeeName());
existing.setEmployeeStatus(eligibility.getEmployeeStatus());
existing.setEmployeeType(eligibility.getEmployeeType());
existing.setFullPartTime(eligibility.getFullPartTime());
existing.setGender(eligibility.getGender());
existing.setHireDate(eligibility.getHireDate());
existing.setJobCode(eligibility.getJobCode());
existing.setJobDescription(eligibility.getJobDescription());
existing.setLastDateWorked(eligibility.getLastDateWorked());
existing.setLocationCode(eligibility.getLocationCode());
existing.setMaritalStatus(eligibility.getMaritalStatus());
existing.setNameSuffix(eligibility.getNameSuffix());
existing.setPhone(eligibility.getPhone());
existing.setProcessDate(eligibility.getProcessDate());
existing.setRehireDate(eligibility.getRehireDate());
existing.setStandardHours(eligibility.getStandardHours());
existing.setState(eligibility.getState());
existing.setSupervisorName(eligibility.getSupervisorName());
existing.setTerminationDate(eligibility.getTerminationDate());
existing.setZipCode(eligibility.getZipCode());
}
}
}
public void success() throws Exception {
// Do nothing
}
}
public class EligibilityDAO {
private static EligibilityDAO instance = new EligibilityDAO();
public static Eligibility findEligibility(int employeeId) {
return instance.find(employeeId);
}
public static Eligibility insertEligibility(Eligibility eligibility) {
return instance.insert(eligibility);
}
public EligibilityDAO() {
super();
}
public Eligibility find(int employeeId) {
Session session = SessionManager.openSession();
Query query = session.createQuery("FROM Eligibility AS e WHERE e.employeeId = :employeeId");
query.setInteger("employeeId", employeeId);
return (Eligibility)query.uniqueResult();
}
public Eligibility insert(Eligibility eligibility) {
Session session = SessionManager.openSession();
eligibility.setEligibilityId((Integer)session.save(eligibility));
return eligibility;
}
}
public class SessionManager {
private SessionFactory sessionFactory;
private Session session;
public static void initialize() {
instance = new SessionManager();
}
public SessionManager() {
sessionFactory = createConfiguration().buildSessionFactory();
session = null;
}
public static SessionManager getInstance() {
return instance;
}
public Session openCurrentSession() {
if (session == null) {
session = sessionFactory.openSession();
}
return session;
}
public void closeCurrentSession() {
if (session != null) {
session.close();
session = null;
}
}
private static final String MAPPING_FILE_LOCATION = "com/pristx/dao/";
private static SessionManager instance;
public static Session openSession() {
return instance.openCurrentSession();
}
public static void closeSession() {
instance.closeCurrentSession();
}
private static Configuration createConfiguration() {
Configuration configuration = new Configuration();
InputStream input = SessionManager.class.getResourceAsStream("hibernate.properties");
Properties properties = new Properties();
try {
properties.load(input);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
configuration.setProperties(properties);
configuration.addResource(MAPPING_FILE_LOCATION + "Eligibility.hbm.xml");
configuration.addResource(MAPPING_FILE_LOCATION + "File.hbm.xml");
configuration.addResource(MAPPING_FILE_LOCATION + "Payroll.hbm.xml");
configuration.addResource(MAPPING_FILE_LOCATION + "User.hbm.xml");
return configuration;
}
}
Full stack trace of any exception that occurs:No exceptions
Name and version of the database you are using:Microsoft SQL Server 2000
The generated SQL (show_sql=true):N/A
Debug level Hibernate log excerpt:N/A
Code: