20
Июн
2017

как правильно обновить запись в базе данных используя spring mvc?

пытаюсь обновить запись но получаю ошибки, в попытке предотвратить их появление решил что делаю что-то не правильное. Подскажите, что не правильно делаю?
есть класс User:

    @Entity
    @Table(name = "user")
    public class User {
        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;

        @Column(name="name")
        private String name;

        @Column(name="name")
        private String login;

       //getters and setters
    }

UserDao:

@Repository
public class UserDao{
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public void updateUser(User user) {
        Session session = this.sessionFactory.getCurrentSession();
        session.update(user);
    }
    public User getForId(Integer id) {
    Session session = this.sessionFactory.getCurrentSession();
    User user = (User) session.load(User.class, new Integer(id));
    return user;
    }
}

Controller:

@RequestMapping(value = "/update", method = RequestMethod.POST)
public void update(User user) {
    service.updateUser(user);
}

@RequestMapping(value = "/updateUser/{id}")
public String updateUser(@PathVariable("id")int id, Model model) {
    User user = service.getForId(id);
    model.addAttribute("user",user);
    return "update";
}

update.jsp:

<form:form action="/update" method="post" commandName="user">
        <table>
            <tr>
                <td><form:label path="name">name
                    </form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="login">name
                    </form:label></td>
                <td><form:input path="login" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Ok" onclick="/" /></td>
            </tr>
        </table>
    </form:form>

это кнопка на созданном объекте которая вызывает /updateUser

   <table>
        <c:forEach items="${users}" var="user">
             <tr>
                <td><a href="delete/${user.id}">Удалить</a></td>
                <td><a href="updateUser/${user.id}">Изменить</a>
            </tr>
        </c:forEach>
    </table>

ошибка:

org.hibernate.TransientObjectException: The given object has a null identifier: com.model.User
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.java:270)
    at org.hibernate.event.internal.DefaultUpdateEventListener.getUpdateId(DefaultUpdateEventListener.java:70)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:238)
    at org.hibernate.event.internal.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireUpdate(SessionImpl.java:739)
    at org.hibernate.internal.SessionImpl.update(SessionImpl.java:731)
    at org.hibernate.internal.SessionImpl.update(SessionImpl.java:726)
    at com.dao.UserDAO.updateUser(UserDao.java:47)
    at com.service.UserService.updateUser(UserService.java:30)
    at com.service.UserService$$FastClassBySpringCGLIB$$12de773.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.service.UserService$$EnhancerBySpringCGLIB$$e21bb612.updateUser(<generated>)
    at com.controller.UserController.update(UserController.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

Источник: https://ru.stackoverflow.com/questions/681664/%D0%BA%D0%B0%D0%BA-%D0%BF%D1%80%D0%B0%D0%B2%D0%B8%D0%BB%D1%8C%D0%BD%D0%BE-%D0%BE%D0%B1%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D1%8C-%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C-%D0%B2-%D0%B1%D0%B0%D0%B7%D0%B5-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85-%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D1%83%D1%8F-spring-mvc

Share

Тебе может это понравится...