更新子行

时间:2017-09-30 19:40:44

标签: java mysql spring

我的数据库中存在外键属性的问题无法添加或更新子行我认为问题出现在下面的代码中:

    @RequestMapping( value = "/saveeeee", method = RequestMethod.POST)
public ModelAndView doSave(@RequestParam("user_id") String user_id, @RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("rol") String rol){
    ModelAndView mv = new ModelAndView("redirect:/utilisateurs");
    User op;
    op = new User();

    Role ro;
    ro = new Role();

    ro.setRole(rol);

    if(!user_id.isEmpty()){
        op =(User)appRepo.findOne(Integer.parseInt(user_id));
    } else {
        op = new User();
    }

    op.setUsername(username);
    op.setPassword(passwordEncoder.encode(password));
    ro.setUser(op);
    ro.setUsername(op.getUsername());

    op.setRo(ro);



    appRepo.save(op);
    return mv;
}

实体:

import javax.persistence.*;

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

public Role ro;
public int user_id;
public String username;
public String password;
public int enabled = 1;

public User() {
}



public User(String username, String password, int enabled) {
    this.username = username;
    this.password = password;
    this.enabled = enabled;

}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
public Role getRo() {
    return ro;
}

public void setRo(Role ro) {
    this.ro = ro;
}









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

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

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

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

@Column(name = "enabled")
public int getEnabled() {
    return enabled;
}

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





}

_

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "user_roles")
public class Role implements Serializable {

public int user_role_id;
public String username;
public String role;
public User user;

public void setUser_role_id(int user_role_id) {
    this.user_role_id = user_role_id;
}

@OneToOne(mappedBy = "ro")
public User getUser() {
    return user;
}

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



public Role(String username, String role) {
    this.username = username;
    this.role = role;
}

public Role() {
}


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getUser_role_id() {
    return user_role_id;
}


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

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

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

public void setRole(String role) {
    this.role = role;
}

}

我的sql shema:

CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(100) NOT NULL,
`enabled` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`username`),
UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1


CREATE TABLE `user_roles` (
`user_role_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`role` varchar(20) NOT NULL,
PRIMARY KEY (`user_role_id`),
UNIQUE KEY `uni_username_role` (`role`,`username`),
KEY `fk_username_idx` (`username`),
CONSTRAINT `fk_username` FOREIGN KEY (`username`) REFERENCES `users` 
(`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

我的错误是:

om.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(databaseuser_roles,CONSTRAINT fk_username FOREIGN KEY( username)参考usersusername))

0 个答案:

没有答案