reset password
Author Message
hgadhia
Posts: 52
Posted 11:19 Feb 06, 2015 |

Hi,

I am trying to save a new department in database using merge, however nothing gets saved in database. It doesn't even give any error/exception.

Following is the snapshot of my code:

Controller Method:    

   @RequestMapping(value="/department/add.html", method = RequestMethod.POST)
    public String edit( @ModelAttribute Department department )
    {
        department = deptDao.saveDepartment(department);
        return "redirect:/department/list.html";
    }

Dao Method:

    @Override
    @Transactional
    public Department saveDepartment(Department department) {
        return entityManager.merge(department);
    }

I am no sure why this happens, can someone please help?

Note:

I have used @Transactional in the Dao method and everything else as explained by prof. Sun in the class. But still nothing gets saved in database.

 

plakhan
Posts: 37
Posted 11:22 Feb 06, 2015 |

If you trying to add new data, you should entityManager.save instead of entityManager.merge. I think merge should be used when we want to edit an entity

hgadhia
Posts: 52
Posted 11:29 Feb 06, 2015 |

Well I am not sure about that, but eclipse shows that entityManager.save doesn't exists.

Also, in the class, prof Sun had explained that entityManager.merge can handle both add/edit operations based on primary key.

So If a key exists already, then it updates it else adds it.

cysun
Posts: 2935
Posted 11:34 Feb 06, 2015 |

The code looks fine. I'd double check applicationContext.xml and make sure <tx:annotation-driven /> is in there. Also do some debugging and make sure saveDepartment() was indeed called.

hgadhia
Posts: 52
Posted 11:48 Feb 06, 2015 |

I have checked that applicationContext.xml has <tx:annotation-driven /> there.

Also, doing System.out.println(department.getName()) prints the name I entered in the form. So it means that the method saveDepartment() is getting called with correct arguments.

My model class looks like this,

@Entity
@Table(name = "departments")
public class Department implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "dept_id")
    int id;
    @Column(name = "dept_name")
    String name;

    @ManyToMany
    private List<Users> users;

}

 

And form looks like this:

                              <form:form modelAttribute="department">
                  <div class="form-group">
                    <label>Department Title</label>
                    <form:input path="name" class="form-control" />
                  </div>

                  <button type="submit" class="btn btn-primary">Save</button>

               </form:form>

 

Is everything correct in there? Really appreciate your help.

cysun
Posts: 2935
Posted 13:22 Feb 06, 2015 |

Please don't post any more code.

The error is not obvious, and "forum debugging" is not effective for these types of errors. You'll have to figure it out yourself.

lmann2
Posts: 156
Posted 22:06 Feb 11, 2015 |

That's unfortunate, I'm actually have the exact same problem (and have likewise followed similar debugging steps).  The only (and perhaps major) difference is that merge is performing a update instead of a insert operation in the database.  How do you approach debugging in Hibernate?   

 

Also, if this issue isn't resolved have you tried checking what Hibernate is auto generating as the PK for the new entry?

(This was my issue: create sequence hibernate_sequence minvalue 2000000;  Also, I think this is horrible)

Last edited by lmann2 at 22:59 Feb 11, 2015.
cysun
Posts: 2935
Posted 08:46 Feb 12, 2015 |
lmann2 wrote:

That's unfortunate, I'm actually have the exact same problem (and have likewise followed similar debugging steps).  The only (and perhaps major) difference is that merge is performing a update instead of a insert operation in the database.  How do you approach debugging in Hibernate?   

 

Also, if this issue isn't resolved have you tried checking what Hibernate is auto generating as the PK for the new entry?

(This was my issue: create sequence hibernate_sequence minvalue 2000000;  Also, I think this is horrible)

HGADHIA's problem was that the data was not saved in database, and the cause was that there was an overlap between the <component-scan> in gefp-servlet.xml and the <component-scan> in applicationContext.xml so two sets of DAO beans were created.

merge() would perform an update if the id field of the object is not null.

To debug Spring/Hibernate issues you can add logging to the project and create a logger in log4j.xml with DEBUG message level for Spring/Hibernate packages.