英文:
Facing a duplicate constraint error in Spring Boot
问题
在员工表格中,我需要从三个不同的表格(职称、国家、州)中捕获下拉列表中的数据。但是,当我第一次插入数据时,数据被捕获,即插入查询正常工作,但从第二次开始,它显示了对designationId、countryId和stateId的重复约束。
在运行以下查询后,SHOW CREATE TABLE tbl_employee,我可以看到该表使用designationId、countryId和stateId这些字段创建了唯一键,但不知道为什么。
CREATE TABLE `tbl_employee` (
`employee_id` BIGINT NOT NULL AUTO_INCREMENT,
`aadhar` VARCHAR(255) DEFAULT NULL,
`address_line1` VARCHAR(255) DEFAULT NULL,
`address_line2` VARCHAR(255) DEFAULT NULL,
`city` VARCHAR(255) DEFAULT NULL,
`dateof_birth` VARCHAR(255) DEFAULT NULL,
`dateof_joining` VARCHAR(255) DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`name` VARCHAR(255) DEFAULT NULL,
`pan` VARCHAR(255) DEFAULT NULL,
`password` VARCHAR(255) DEFAULT NULL,
`zip` VARCHAR(255) DEFAULT NULL,
`country_id` BIGINT DEFAULT NULL,
`designation_id` BIGINT DEFAULT NULL,
`state_id` BIGINT DEFAULT NULL,
PRIMARY KEY (`employee_id`),
UNIQUE KEY `UK_ku8jubtc7x8evqgglfhafudl4` (`country_id`),
UNIQUE KEY `UK_2d8ftabh5yvknmnufwqllhyi0` (`designation_id`),
UNIQUE KEY `UK_nj8cpqu4dp6qqiix9g8xd4qj2` (`state_id`),
CONSTRAINT `FKl3gotmdj4q5ns5we62h71usr7` FOREIGN KEY (`designation_id`) REFERENCES `tbl_designation` (`id`),
CONSTRAINT `FKlei86ulei19r01bm15a8qb2ls` FOREIGN KEY (`country_id`) REFERENCES `tbl_country` (`country_id`),
CONSTRAINT `FKrp2jdmscq9awnqumwkowp8hfe` FOREIGN KEY (`state_id`) REFERENCES `tbl_state` (`state_id`)
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
以下是实体类的代码:
Employee.java
@Entity
@Table(name = "tbl_employee")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long employeeId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "designationId")
private Designation designationInfo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "countryId")
private Country country;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "stateId")
private State state;
}
Country.java
@Entity(name = "tbl_country")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Country {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long countryId;
String country_name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_country_id",referencedColumnName = "countryId")
private List<State> states;
}
State.java
@Entity(name = "tbl_state")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class State {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long stateId;
String state_name;
@ManyToOne
@JoinColumn(name = "fk_country_id")
private Country country;
}
英文:
I have a employee Form where in the employee table I need to capture datas from three different table like designation, country, state from dropdown but when I am inserting the data for the first time data is captured i.e. insert query is working properly but from second time it is showing duplicate constraint for designationId,countryId,stateId.
As after running this query SHOW CREATE TABLE tbl_employee I can See that the table is created with Unique Keys for designationId, countryId, stateId these fields. Don't know how..
CREATE TABLE `tbl_employee` (
`employee_id` BIGINT NOT NULL AUTO_INCREMENT,
`aadhar` VARCHAR(255) DEFAULT NULL,
`address_line1` VARCHAR(255) DEFAULT NULL,
`address_line2` VARCHAR(255) DEFAULT NULL,
`city` VARCHAR(255) DEFAULT NULL,
`dateof_birth` VARCHAR(255) DEFAULT NULL,
`dateof_joining` VARCHAR(255) DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`name` VARCHAR(255) DEFAULT NULL,
`pan` VARCHAR(255) DEFAULT NULL,
`password` VARCHAR(255) DEFAULT NULL,
`zip` VARCHAR(255) DEFAULT NULL,
`country_id` BIGINT DEFAULT NULL,
`designation_id` BIGINT DEFAULT NULL,
`state_id` BIGINT DEFAULT NULL,
PRIMARY KEY (`employee_id`),
**UNIQUE KEY** `UK_ku8jubtc7x8evqgglfhafudl4` (`country_id`),
**UNIQUE KEY** `UK_2d8ftabh5yvknmnufwqllhyi0` (`designation_id`),
**UNIQUE KEY** `UK_nj8cpqu4dp6qqiix9g8xd4qj2` (`state_id`),
CONSTRAINT `FKl3gotmdj4q5ns5we62h71usr7` FOREIGN KEY (`designation_id`) REFERENCES `tbl_designation` (`id`),
CONSTRAINT `FKlei86ulei19r01bm15a8qb2ls` FOREIGN KEY (`country_id`) REFERENCES `tbl_country` (`country_id`),
CONSTRAINT `FKrp2jdmscq9awnqumwkowp8hfe` FOREIGN KEY (`state_id`) REFERENCES `tbl_state` (`state_id`)
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Sharing the Entity Class
Employee.java
@Entity
@Table(name = "tbl_employee")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long employeeId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "designationId")
private Designation designationInfo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "countryId")
private Country country;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "stateId")
private State state;
}
Country.java
@Entity(name = "tbl_country")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Country {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long countryId;
String country_name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_country_id",referencedColumnName = "countryId")
private List<State> states;
}
State.java
@Entity(name = "tbl_state")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class State {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long stateId;
String state_name;
@ManyToOne
@JoinColumn(name = "fk_country_id")
private Country country;
}
答案1
得分: 0
只需更改关系问题即可解决
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "designationId" , unique = false)
private Designation designationInfo;
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "countryId", unique = false)
private Country country;
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "stateId", unique = false)
private State state;
英文:
Just Changing the Relation the problem is resolved
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "designationId" , unique = false)
private Designation designationInfo;
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "countryId", unique = false)
private Country country;
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
@JoinColumn(name = "stateId", unique = false)
private State state;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论