I have read and reread everything here on SO and many other sites but can't seem to figure out why my updated objects are not updating.
The basic overview of what I'm doing:
Service layer asks DAO for some People
Return an ArrayList of People from the DB (DAO / @Repository)
Service Layer manipulates object and adds them to a new arraylist
Service Layers passes new list back to DAO to update
NOTHING GETS Updated
If I throw a log message in my object has the new values, the children are correctly hydrated. I get no errors in the code is just doesn't commit my changes.
Here is some code:
PersonDAO annotated as @Repository
public void updatePeople(List<Person> people) { log.info("Updating " + people.size() + " people"); try { Transaction tx = getCurrentSession().beginTransaction(); for (Person person : people){ getCurrentSession().saveOrUpdate(person); } getCurrentSession().flush(); tx.commit(); getCurrentSession().close(); } catch (Exception e){ log.error("Exception Updating all people " + e.toString()); e.printStackTrace(); } } public List<Person> getAssociatesByStoreId(String storeId) { try { List<Person> usersInStore = (List<Person>) getCurrentSession() .createCriteria(Person.class).createCriteria("store") .add(Restrictions.eq("storeId", storeId)).list(); return usersInStore; } catch (Exception e) { log.error("exception in getAssociatesByStoreId ", e); } return null; } PersonService annotated as @Service - relevant method
/* I put the people into a map to do some other logic on them */ for (Person person : personDAO.getAllPeople()){ personMap.put(person.getEmployeeId() + "-" + person.getStore().getStoreId(), person); } /* I iterate a list creating new Person objects (based on some random stuff), including saving any children entities (managementcodes and stores) that need to be created After I have created a new Person I attempt to find it in the map from above. If I find it pull it out of the map and put it into an array list that is eventually passed back into the DAO */ Person annotated as @Entity
private int personid; private String firstName; private String lastName; private Store store; private ManagementCode managementCode; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "personid", unique = true, nullable = false) public int getPersonid() { return this.personid; } /*a bunch of getters and setters*/ @ManyToOne(fetch = FetchType.LAZY, optional = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) @org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST}) @LazyCollection(LazyCollectionOption.EXTRA) @JoinColumn(name = "managementlevel", nullable = true) public ManagementCode getManagementCode() { return this.managementCode; } @ManyToOne(fetch = FetchType.LAZY, optional = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) // @org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST}) @JoinColumn(name = "storeid", nullable = false) public Store getStore() { return this.store; } Store annotated as entity (Managementcode is the same)
/*fields + getters and setter */ @OneToMany(fetch = FetchType.LAZY, mappedBy = "store", cascade = {CascadeType.MERGE, CascadeType.PERSIST}) @LazyCollection(LazyCollectionOption.EXTRA) @JsonIgnore public Set<Person> getPersons() { return this.persons; } EDIT I added both sets of Cascade type annotations above and still no luck
EDIT 2 Updated the to show the primary key definition Someone please help me before I kick a puppy. Thanks