Alver
Alver

Reputation: 182

Update function work like my Add Function ( Spring , Hibernate , mvc)

i am newbie in SH mvc , i learn spring hibernate mvc, i make CRUD implementation but when i try the update function the database does not change, but look like when i create new data to my database. .what should i do

This data will be changed

- i rename the name with : hello world and i click update..

This is the result, "sasazaki mami" Data is still here and is not renewable with "Hello World" data

This my Controller

     package com.list.satu;

     import java.util.List;

     import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

      import javax.servlet.http.HttpServletRequest;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.Validator;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;

    import com.list.satu.entity.Anggota;
    import com.list.satu.service.AnggotaService;

    @Controller
    @RequestMapping("test")
    public class AnggotaController {
private static final Logger logger = LoggerFactory
        .getLogger(AnggotaController.class);

private AnggotaService anggotaService;
private Validator validator;

@Autowired
public void setAnggotaService(AnggotaService anggotaService) {
    this.anggotaService = anggotaService;
}

@Autowired
public void setValidator(Validator validator) {
    this.validator = validator;
}

@RequestMapping(value = "/anggota", method = RequestMethod.GET)
public String search(Model model) {
    List<Anggota> anggota = anggotaService.getAllAnggota();
    model.addAttribute("anggota", anggota);

    return "test/anggota";
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addNew(Model model) {
    model.addAttribute("anggota", new Anggota());
    return "test/add";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String save(Anggota anggota, Model model, BindingResult errors,
        HttpServletRequest request) {
    if (validator != null) {
        validator.validate(anggota, errors);
    }
    if (errors.hasErrors()) {

        return "test/add";
    }
    this.anggotaService.save(anggota);
    return "redirect:/test/anggota";
}

@RequestMapping(value = "/edit/{npm}", method = RequestMethod.GET)
public String editRecord(@PathVariable("npm") Integer kode, Model model,
        HttpServletRequest request) {
    logger.info("Received npm" + kode.toString());
    Anggota anggota = this.anggotaService.getById(kode);
    model.addAttribute("anggota", anggota);
    return "test/edit";
}

@RequestMapping(value = "/edit/{npm}", method = RequestMethod.POST)
public String edit(@ModelAttribute Anggota anggota, Model model,
        BindingResult errors, HttpServletRequest request,
        @PathVariable Integer npm) {

    if (validator != null) {
        validator.validate(anggota, errors);
    }
    if (errors.hasErrors()) {

        return "test/anggota";
    }
    anggota.setNpm(npm);
    this.anggotaService.update(anggota);

    System.out.println(anggota.getNpm());
    System.out.println(anggota.getNama());
    System.out.println(anggota.getUmur());

    return "redirect:/test/anggota";
}
    }

my Dao Implement

package com.list.satu.dao.im;

    import java.util.List;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.stereotype.Repository;

    import com.list.satu.dao.AnggotaDao;
    import com.list.satu.entity.Anggota;


        @Repository
     public class AnggotaDaoim implements AnggotaDao{
private HibernateTemplate hibernateTemplate;
@Autowired
private SessionFactory sessionFactory;

public List<Anggota> getAllAnggota(){
    Session session = sessionFactory.getCurrentSession();
    return session.createQuery("FROM Anggota").list();
}

public List<Anggota> getAllAnggotaDetails() {
    Session session = sessionFactory.getCurrentSession();
    return session.createQuery("FROM Anggota a LEFT JOIN FETCH a.flight").list();
}

public Anggota getAnggota(Anggota anggota) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void save(Anggota anggota) {
    // TODO Auto-generated method stub
    Session session = sessionFactory.getCurrentSession();
    session.merge(anggota);
}

@Override
public void update(Anggota anggota) {
    // TODO Auto-generated method stub
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(anggota);
}

@Override
public Anggota getById(Integer kode) {
    // TODO Auto-generated method stub
    return (Anggota) this.sessionFactory.getCurrentSession().get(Anggota.class, kode);
}

@Override
public void remove(Anggota anggota) {
    // TODO Auto-generated method stub
    Session session = sessionFactory.getCurrentSession();
    session.delete(anggota);

      }}

My Service Implement

    package com.list.satu.service.im;

     import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import com.list.satu.dao.AnggotaDao;
    import com.list.satu.entity.Anggota;
    import com.list.satu.service.AnggotaService;

    @Service
    public class AnggotaServiceIm implements AnggotaService{

@Autowired
private AnggotaDao anggotaDao;

public AnggotaDao getAnggotaDao() {
    return anggotaDao;
}

public void setAnggotaDao(AnggotaDao anggotaDao) {
    this.anggotaDao = anggotaDao;
}

@Transactional
public List<Anggota> getAnggotaDetails() {
    return anggotaDao.getAllAnggotaDetails();
}

@Transactional
public List<Anggota> getAllAnggota() {
    // TODO Auto-generated method stub
    return anggotaDao.getAllAnggota();
}

@Transactional
public void save(Anggota anggota) {
    // TODO Auto-generated method stub
    anggotaDao.save(anggota);
}

@Override
@Transactional
public void update(Anggota anggota) {
    // TODO Auto-generated method stub
    anggotaDao.update(anggota);
}

@Override
@Transactional
public Anggota getById(Integer kode) {
    // TODO Auto-generated method stub
    return anggotaDao.getById(kode);
}

@Override
@Transactional
public void remove(Anggota anggota) {
    // TODO Auto-generated method stub
    anggotaDao.remove(anggota);
}


    }

My jsp action

    <form:form role="form" action="/satu/test/edit/${npm}"
commandName="anggota" method="post">

Thanksss before ^^

Upvotes: 0

Views: 1315

Answers (1)

Balaji Reddy
Balaji Reddy

Reputation: 5700

try like this

@Override
public void update(Anggota externalAnggota) {
    Session session = sessionFactory.getCurrentSession();

    Anggota dbAnggota = (Anggota)session.load(Anggota.class, externalAnggota.getId());

 //now set all your values from exteralAnggota instance  to dbAnggota instance.  
  dbAnggota.setName(exteralAnggota.getName());

  //Set for all the fields 

   session.saveOrUpdate(dbAnggota);
}

Reason the object which you are passing to the session is not part of the session so hibernate engine considers it as new instance and crating new row. saveOrUpdate() method works based on the below condition

1)If the object is already persistent in the current session, it do nothing

2)If another object associated with the session has the same identifier, throw an exception to the caller

3)If the object has no identifier property, save() the object

4) If the object’s identifier has the value assigned to a newly instantiated object, save()the object

Hope this is helpful!

Upvotes: 2

Related Questions