有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

hibernate中合并时发生java意外回滚异常(已解决)

我在使用hibernate的简单合并方法上遇到了意外的回滚异常

我正在使用JSP和控制器结构

它应该是一个简单的更新,但它返回一个异常

我有这个:

用户。爪哇

public class user{
   private Integer id;
   String name;
   String active;
}

@Id
@Column(name="ID", unique = true, nullable = false, etc...)
public Integer getId(){return this.id}

@Column(name = "NAME", nullable = true, length = 9)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
@Column(name = "ACTIVE", nullable = true, length = 9)
public String getActive() {
    return this.active;
}

public void setActive(String active) {
    this.active= active;
}

控制器:

Public class UpdateUserController{

  //Here's the main issue

  User userToUpdate = findUserById("1");

 //user properly recovered from the DB

  userToUpdate.setActive("N");

  //UserService is the class that contains the merging method 
   userService.merge(userToUpdate);

  System.out.println(userToUpdate.getActive());
}

服务类别:

@Transactional(propagation = Propagation.REQUIRED)
public user merge(User detachedInstance) {
     try {
        User result = entityManager.merge(detachedInstance);
        return result;
     } catch (RuntimeException re) {
        throw re;
     }
}

这是一个字符串的简单更新,一行中只有一个cel,这不应该返回异常,但它确实返回了异常

我在一些帖子中看到,问题可能是由于方法尝试更新当前为null的元素造成的,默认情况下,这可能会导致hibernate返回RollBackException

我已经检查过了,合并时没有空值

谢谢你的帮助

决议:

数据库中的元素名为varchar(5) 在我的代码中,我试图更新一个7个字符长的名称,这导致了回滚异常


共 (0) 个答案