【问题标题】:Hibernate One-to-Many constraint violation exceptionHibernate 一对多约束违反异常
【发布时间】:2014-10-08 21:53:00
【问题描述】:

您好,我正在尝试进行一对多插入,但遇到了问题。我有两张桌子:

CREATE TABLE users_app (
user_id int UNSIGNED NOT NULL AUTO_INCREMENT,
user_number varchar(45) NOT NULL default '0',
user_password varchar(45) NOT NULL default '0',
os int(1) unsigned NOT NULL,
token varchar(500) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

CREATE TABLE user_app_devices(  
 id int AUTO_INCREMENT PRIMARY KEY,  
 user_id int UNSIGNED  NULL,  // Here it can accept null values
 device_name varchar(45) NOT NULL,
 FOREIGN KEY (user_id) REFERENCES users_app (user_id)  
)ENGINE=InnoDB CHARSET=utf8;

我的课:

@Entity
@Table(name="user_app_devices")
public class UserAppDevice implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="id")
@GeneratedValue(strategy = IDENTITY)
private int id;


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

//bi-directional many-to-one association to UsersApp
@ManyToOne
@JoinColumn(name="user_id",insertable=false,updatable=false)  // Ignoring this column    using JSON IGNORE
@JsonIgnore     
private UsersApp usersApp;


@Column(name="user_id")
private int userId;

public UserAppDevice() {
}

public int getId() {
 return this.id;
}

public void setId(int id) {
  this.id = id;
}

public int getUserId() {
 return this.id;
}

public void setUserId(int id) {
  this.id = id;
}

public String getDeviceName() {
 return this.deviceName;
}

public void setDeviceName(String deviceName) {
 this.deviceName = deviceName;
}

public UsersApp getUsersApp() {
 return this.usersApp;
}

public void setUsersApp(UsersApp usersApp) {
 this.usersApp = usersApp;
}

}

@Entity
@Table(name="users_app")
public class UsersApp implements Serializable {
 private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="user_id")
private int userId;

private int os;

private String token;

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

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

 //bi-directional many-to-one association to UserAppDevice
 @OneToMany(mappedBy="usersApp")
 private List<UserAppDevice> userAppDevices;

 public UsersApp() {
 }

 public int getUserId() {
   return this.userId;
 }

 public void setUserId(int userId) {
  this.userId = userId;
 }

 public int getOs() {
  return this.os;
 }

 public void setOs(int os) {
  this.os = os;
 }

 public String getToken() {
  return this.token;
 }

 public void setToken(String token) {
  this.token = token;
 }

 public String getUserNumber() {
   return this.userNumber;
 }

 public void setUserNumber(String userNumber) {
   this.userNumber = userNumber;
 }

 public String getUserPassword() {
   return this.userPassword;
  }

 public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
 }

 public List<UserAppDevice> getUserAppDevices() {
    return this.userAppDevices;
 }

 public void setUserAppDevices(List<UserAppDevice> userAppDevices) {
   this.userAppDevices = userAppDevices;
 }

 public UsersApp(int os, String token, String userNumber, String userPassword) {
   this.os = os;
   this.token = token;
   this.userNumber = userNumber;
   this.userPassword = userPassword;
 }

在 'user_app_devices' 表 'user_id' 列中接受 MySql 中的 Null 值。但是使用 Hibernate 它不接受值,给出以下错误消息是:

[WARN] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1452, SQLState: 23000
[ERROR] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`user_app_devices`, CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user_app` (`user_id`))
[ERROR] com.jaguar.nbfcapp.aop.logging.LoggingAspect - Exception in com.example.web.rest.userResource.create() with cause = org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute

【问题讨论】:

  • 对不起,我在 'user_id' 处犯了一个小错误,我采用了 int 原始值,所以它不接受空值...

标签: java mysql sql hibernate jpa


【解决方案1】:

试试看

@JoinColumn(name="user_id", nullable = true)

insertableupdatable属性解释见this SO

【讨论】:

    【解决方案2】:

    这是因为您插入的设备的 user_id 不存在于 users_app 表中。顺便说一句,你不应该在 Device 实体中有这个 user_id 字段,因为你有一个 ManyToOne 关联。只需使用

    @ManyToOne
    @JoinColumn(name="user_id")
    @JsonIgnore     
    private UsersApp usersApp;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2019-02-04
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多