Hibernate外键更改父列

时间:2016-08-29 07:05:02

标签: mysql spring hibernate foreign-keys

我有2个实体。当我想更改子列中的值时,它会在父列中更改。

第一个实体(User.class)

@Entity
@Table(name = "users")
public class User {

    private String username;
    private String password;
    private String name;
    private String surname;
    private Department department;
    private boolean enabled;
    private Set<UserRole> userRole = new HashSet<UserRole>(0);
    private Set<Equipment> equipment = new HashSet<Equipment>();

    public User() {
    }

    public User(String username, String password, String name, String surname, Department department, boolean enabled,
            Set<UserRole> userRole, Set<Equipment> equipment) {
        super();
        this.username = username;
        this.password = password;
        this.name = name;
        this.surname = surname;
        this.department = department;
        this.enabled = enabled;
        this.userRole = userRole;
        this.equipment = equipment;
    }

    @Id
    @Column(name = "username", unique = true, length = 45)
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password", length = 60)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "enabled")
    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    public Set<UserRole> getUserRole() {
        return this.userRole;
    }

    public void setUserRole(Set<UserRole> userRole) {
        this.userRole = userRole;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "surname")
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "depId", updatable = false, insertable = false, nullable = false)
    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

第二实体:

@Entity
@Table(name = "department")
@NamedQuery(name = "allDepartments", query = "SELECT d FROM Department d")
public class Department {

    private int depId;
    private String name;
    private String adress;
    private Set<User> user = new HashSet<User>();

    public Department() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Department(int depId, String name, String adress, Set<User> user) {
        super();
        this.depId = depId;
        this.name = name;
        this.adress = adress;
        this.user = user;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "department_id", unique = true)
    public int getDepId() {
        return depId;
    }

    public void setDepId(int depId) {
        this.depId = depId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "department")
    public Set<User> getUser() {
        return user;
    }

    public void setUser(Set<User> user) {
        this.user = user;
    }

}

我的UserDAOImpl:

@Repository
public class UserDAOImpl extends AbstractSessionFactory implements UserDAO {

    @Override
    public User findByUserName(String username) {
        return (User) getSession().get(User.class, username);
    }

    @Override
    public void insert(User user) {
        getSession().save(user);
    }

    @Override
    public void update(User user) {
        getSession().update(user);
    }

    @Override
    public void delete(String username) {
        getSession().delete(findByUserName(username));
    }

    @Override
    public User findByDepartment(int departmentId) {
        return (User) getSession().get(User.class, departmentId);
    }

    @Override
    public Department getDepartment (int departmentId) {
        return (Department) getSession().get(Department.class, departmentId);
    }

    @Override
    public User findByName(String name) {
        return (User) getSession().get(User.class, name);
    }

    @Override
    public List<User> getAllUsers() {
        return getSession().getNamedQuery("allUsers").list();
    }
}

我的控制器:

@Controller
public class UserController {

    @Autowired
    private UserSevice userService;

    @Autowired
    private DepartmentService departmentService;

    private User user;
    private Department department;

    @RequestMapping(value = "/updateUser/{username}", method = RequestMethod.GET)
    private ModelAndView updateUserPage(@PathVariable("username") String username) {
        ModelAndView model = new ModelAndView();
        model.addObject("getUser", userService.findByUserName(username));
        model.setViewName("updateUser");
        return model;
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    private ModelAndView updateUser(@ModelAttribute("users") User user, BindingResult result) {
        ModelAndView model = new ModelAndView();
        userService.update(user);
        model.addObject("allUsers", userService.getAllUsers());
        model.setViewName("superAdmin");
        return model;
    }

我的jsp页面:

<form:form modelAttribute="getUser"
                    action="${pageContext.request.contextPath}/updateUser"
                    method="POST">
                    <!--Simple table example -->
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <i class="fa fa-bar-chart-o fa-fw"></i>Employee
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-hover table-striped">
                                            <tr>
                                                <td>Username:</td>
                                                <td>Password:</td>
                                                <td>Name:</td>
                                                <td>Surname:</td>
                                                <td>Enabled:</td>
                                                <td>Department:</td>
                                            </tr>
                                            <tr>
                                                <td><form:input path="username" readonly="true" /></td>
                                                <td><form:input path="password" readonly="true" /></td>
                                                <td><form:input path="name" /></td>
                                                <td><form:input path="surname" /></td>
                                                <td><form:input path="enabled" /></td>
                                                <td><form:input path="department.name" name="depName" /></td>
                                            </tr>
                                        </table>
                                        <input type="submit" value="Save" name="btnSave" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form:form>

我改变了我的控制器:

@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    private ModelAndView updateUser(@RequestParam("depName") String departmentName, @ModelAttribute("users") User user,
            BindingResult result) {
        ModelAndView model = new ModelAndView();
        for (Department department : departmentService.getAllDepartments()) {
            department = departmentService.findByDepartmentName(departmentName);
            user.setDepartment(department);
            userService.update(user);
        }
        model.addObject("allUsers", userService.getAllUsers());
        model.setViewName("superAdmin");
        return model;
    }

UserServiceImpl:

@Service
public class UserServiceImpl implements UserSevice {

    @Autowired
    private UserDAO userDAO;

    @Override
    @Transactional
    public void insert(User user) {
        userDAO.insert(user);
    }

    @Override
    @Transactional
    public void update(User user) {
        userDAO.update(user);
    }

    @Override
    @Transactional
    public void delete(String username) {
        userDAO.delete(username);
    }

    @Override
    @Transactional
    public User findByUserName(String username) {
        return userDAO.findByUserName(username);
    }

    @Override
    @Transactional
    public User findByDepartment(int departmentId) {
        return userDAO.findByDepartment(departmentId);
    }

    @Override
    @Transactional
    public Department getDepartment(int departmentId) {
        return userDAO.getDepartment(departmentId);
    }

    @Override
    @Transactional
    public User findByName(String name) {
        return userDAO.findByName(name);
    }

    @Override
    @Transactional
    public List<User> getAllUsers() {
        return userDAO.getAllUsers();
    }
}

root case:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Head Office' for key 'UK_1t68827l97cwyxo9r1u6t4p7d'

我必须展示什么?

我按用户名获取用户。当我想要更改他的部门时,它会更改Department表中的所有部门,而不是在用户的department_id列中。如何更改user.department而非部门?

2 个答案:

答案 0 :(得分:0)

您可以为用户设置部门并更新用户。它将更新用户表中的部门ID值。

user.setDepartment(department);
userService.update(user);

答案 1 :(得分:0)

如果您需要更新部门,请从@JoinColumn(name = "depId", updatable = false, insertable = false, nullable = false)中删除 updatable = false,并使用以下代码设置部门

user.setDepartment(department);    
userService.update(user); 

或在UserDAOImpl中尝试:

@Override
public void update(User user) {
String hql = "UPDATE User set department = :department "  + 
         "WHERE username = :username";
Query query = getSession().createQuery(hql);    
query.setParameter("department", user.getDepartment());    
query.setParameter("username", user.getUsername());    
int result = query.executeUpdate();
}  
来自your Github

将UserController.updateUser更改为

@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
private ModelAndView updateUser(@ModelAttribute("users") User user,
        BindingResult result) {
    ModelAndView model = new ModelAndView();
    department = departmentService.findByDepartmentName(user.getDepartment().getName());
    user.setDepartment(department);
    userService.update(user);
    model.addObject("allUsers", userService.getAllUsers());
    model.setViewName("superAdmin");
    return model;
}

和DepatmentDAOImpl.findByDepartmentName到

@Override
public Department findByDepartmentName(String departmentName) {
    List<Department> departments = getSession()
            .createQuery("select d from Department d where d.name =:departmentName")
            .setParameter("departmentName", departmentName).list();
    return departments.size() == 0 ? null : departments.get(0);
}