2

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>
varman
  • 8,704
  • 5
  • 19
  • 53
  • In case of a table with just one field no way to get old id to `update`. If old id was e.g. 1 and you changed it to 2 if you have just one field there is no way to detect old value 1 to perform update. So a new instance is created. If there are more than one field id (PK) and some data fields the id is used to perform update. – StanislavL Jun 26 '17 at 07:47
  • Should we need to have id (INT) always in all table? – varman Jun 26 '17 at 08:16

1 Answers1

2

Problem is that your model has only one field and it's primary key. When you're trying to update your entity you change key's value in the form and thus hibernate doesn't know which entity it should update and creates a new entity instead.

Also, what is the purpose of having a model with only one field? It's better to have some entity like:

@Entity
@Table(name = "gauge_category")
public class GaugeCategory {

    @Id
    @GeneratedValue
    private Integer id;

    @Id
    @Column(name = "name", unique = true, nullable = false)
    private String name;

    // ...

}

Having such entity you'll be able to update category's name using id and reference that id in your other DB tables. If you use string foreign key it would be difficult to update other tables, especially if they have lots of records.

ledniov
  • 2,302
  • 3
  • 21
  • 27
  • 1
    So u suggest me to have more than one field, no? I got your explanation. My doubt is, If I have tow fields with the data type of varchar, one is primary key, what happens? This question also same like that. Or Do we need at least id with the datatype of INT? – varman Jun 26 '17 at 08:14
  • 1
    @varman yes, I suggest you to use one `INT` and one `VARCHAR` field, see [this answer](https://stackoverflow.com/a/3162274/1126831) for more details why it's better to use surrogate integer keys. – ledniov Jun 26 '17 at 08:18
  • 1
    Thank you so much for advice you given. I have done with Integer id. – varman Jun 27 '17 at 04:12