gpt4 book ai didi

java - 内部异常 : com. mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:

转载 作者:行者123 更新时间:2023-11-30 22:41:35 25 4
gpt4 key购买 nike

我想通过在 MYSQL 数据库中使用 JPA 来持久化一个对象。这是我的创建脚本:

CREATE TABLE toolboxAccount (
idtoolboxAccount INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL ,
password_2 VARCHAR(30) NOT NULL ,
PRIMARY KEY(idtoolboxAccount));



CREATE TABLE config (
idconfig INT NOT NULL AUTO_INCREMENT,
toolboxAccount_idtoolboxAccount INTEGER UNSIGNED NOT NULL ,
defaultExportPath VARCHAR(255) NULL ,
PRIMARY KEY(idconfig) ,
INDEX config_FKIndex1(toolboxAccount_idtoolboxAccount),
FOREIGN KEY(toolboxAccount_idtoolboxAccount)
REFERENCES toolboxAccount(idtoolboxAccount)
ON DELETE CASCADE
ON UPDATE CASCADE);

CREATE TABLE connection (
idconnection INT NOT NULL AUTO_INCREMENT,
toolboxAccount_idtoolboxAccount INTEGER UNSIGNED NOT NULL ,
url VARCHAR(255) NULL ,
username VARCHAR(30) NULL ,
password_2 VARCHAR(30) NULL ,
site VARCHAR(30) NULL ,
PRIMARY KEY(idconnection) ,
INDEX connection_FKIndex1(toolboxAccount_idtoolboxAccount),
FOREIGN KEY(toolboxAccount_idtoolboxAccount)
REFERENCES toolboxAccount(idtoolboxAccount)
ON DELETE CASCADE
ON UPDATE CASCADE);

CREATE TABLE export (
idexport INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
connection_idconnection INT NOT NULL ,
nameOfExportZIP VARCHAR(255) NOT NULL ,
PRIMARY KEY(idexport) ,
INDEX export_FKIndex1(connection_idconnection),
FOREIGN KEY(connection_idconnection)
REFERENCES connection(idconnection)
ON DELETE CASCADE
ON UPDATE CASCADE);

类是


工具箱帐户实体

@Entity
@Table(name = "toolboxaccount", schema = "", catalog = "toolboxtest")
public class ToolboxaccountEntity {

@Id
@GeneratedValue(generator="my_seq")
@SequenceGenerator(name="my_seq",sequenceName="MY_SEQ", allocationSize=1)
private long idtoolboxAccount;
private String username;
private String password_2;



@OneToOne(cascade = CascadeType.PERSIST)
private ConfigEntity configEntity;

@OneToMany(cascade = CascadeType.PERSIST)
private List<ConnectionEntity> connections;

public List<ConnectionEntity> getConnections() {
return connections;
}

public void setConnections(List<ConnectionEntity> connections) {
this.connections = connections;
}
...

连接实体

@Entity
@Table(name = "connection", schema = "", catalog = "toolboxtest")
public class ConnectionEntity {
@Id
@GeneratedValue(generator="my_seq")
@SequenceGenerator(name="my_seq",sequenceName="MY_SEQ", allocationSize=1)
private long idconnection;
private String url;
private String username;
private String password_2;
private String site;


@Column(name = "idconnection")
public long getIdconnection() {
return idconnection;
}

public void setIdconnection(int idconnection) {
this.idconnection = idconnection;
}
...

ToolboxAccountEntity 的持久化有效,但是当我添加 ConnectionEntity 时,出现以下错误:

Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`toolboxtest`.`connection`, CONSTRAINT `connection_ibfk_1` FOREIGN KEY (`toolboxAccount_idtoolboxAccount`) REFERENCES `toolboxaccount` (`idtoolboxAccount`) ON DELETE CASCADE ON UPDATE CASCADE)
Error Code: 1452
Call: INSERT INTO toolboxtest.connection (PASSWORD_2, SITE, URL, USERNAME) VALUES (?, ?, ?, ?)
bind => [4 parameters bound]

最佳答案

2 件事:

1) 如果要进行插入操作,请确保数据库中不存在该记录。

2) 如果要进行更新操作,请确保该记录存在于数据库中。

3) 记住记录必须是良好的关系,主键,外键。

当记录已经存在时,这种错误很常见。

问候!如果您更正了问题,请告诉我们

关于java - 内部异常 : com. mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com