Hibernate saveOrUpdate () creates a new record on update

I created a table called " gauge_category " and only has one field " gauge_category_id " which is the primary key, datatype varchar , I tried CRUD operation. Create, read, delete operations that work great. When I update, it creates a new entry, not an update. I learned about saveOrUpdate () from some articles. The parameter passed from the url when the edit button is clicked works correctly.

But I noticed that I tried another class that has two fields id (int) (primarykey), college (Varchar). Here the update is correct if the id adds <form:hidden path="id" />

an additional jsp to the 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 ID, throws an exception
  • if the object has no id property, save () it
  • if the object id has a value assigned to the newly created object, save () it
  • if the object is versioned by a or and the value of the version property is the same value assigned to the new value the object instance 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 to add

<spring:url value="/gaugeCategory/save" var="saveURL"></spring:url>

<form:form action="${saveURL} " modelAttribute="gaugeCategoryForm"> 

     <form:input path="category" />

</form:form>

      

+3


source to share


2 answers


The problem is that your model only has one field and a primary key. When you try to update your entity, you change the key value in the form and therefore hibernate doesn't know which object it should update and instead creates a new entity.

Also, what is the purpose of creating a model with only one field? Better to have some structure 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 an object, you can update the category name with id and specify this identifier in other database tables. If you are using a string foreign key, it would be difficult to update other tables, especially if they have many records.

0


source


you need to pass the id of the primary key if you are updating an object



0


source







All Articles