无法从我的数据库中删除对象

时间:2017-10-01 20:05:24

标签: java hibernate spring-mvc spring-boot

我试图在我的数据库中删除一个对象,在其他表中有一个外键行,错误是IllegalArgumentException,调用getter of com.techpro.webstat.models.Role.username;嵌套异常是org.hibernate.PropertyAccessException

我有两个实体:

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", cascade = CascadeType.ALL)
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;
}



}

这是我删除用户对象的代码:

  @RequestMapping( value = "/deleteuserid/{user_id}", method = RequestMethod.GET)
public ModelAndView doDelete(@PathVariable("user_id") int user_id){
    ModelAndView mv = new ModelAndView("redirect:/utilisateurs");
    appRepo.delete(user_id);
    return mv;
}

0 个答案:

没有答案