I created a table called "gauge_category" and it has only one field "gauge_category_id" which is primary key, data type is varchar. I tried CRUD operation. Create,read,delete operations working perfectly. When I update, it's creating a new entry, not updating. I got to know about saveOrUpdate() from some articles. Parameter passing from URL when click on edit button is working properly.
But I noticed that I tried a another class which has two fields id(int)(primarykey) , college(Varchar). Here update is orking correctly when id add <form:hidden path="id" />
additionally in jsp form.
saveOrUpdate() does the following:
- if the object is already persistent in this session, do nothing
- if another object associated with the session has the same identifier, throw an exception
- if the object has no identifier property, save() it
- if the object's identifier has the value assigned to a newly instantiated object, save() it
- if the object is versioned by a or , and the version property value is the same value assigned to a newly
instantiated object, save() it- otherwise update() the object
Model class
@Entity
@Table(name = "gauge_category")
public class GaugeCategory {
@Id
@Column(name = "gauge_category_id", unique = true, nullable = false)
private String category;
public GaugeCategory() {
super();
}
public GaugeCategory(String category) {
super();
this.category = category;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Dao implementation class
//other methods, @Autowired
@Override
public void saveOrUpdate(GaugeCategory GaugeCategory) {
sessionFactory.getCurrentSession().saveOrUpdate(GaugeCategory);
}
Controller class
//other methods, @Autowired
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveCategory(@ModelAttribute("gaugeCategoryForm") GaugeCategory gaugeCategory) {
gaugeCategoryService.saveOrUpdate(gaugeCategory);
return new ModelAndView("redirect:/gaugeCategory/list");
}
jsp page for add
<spring:url value="/gaugeCategory/save" var="saveURL"></spring:url>
<form:form action="${saveURL} " modelAttribute="gaugeCategoryForm">
<form:input path="category" />
</form:form>