在Spring Boot中遇到了重复约束错误。

huangapple go评论55阅读模式
英文:

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 = &quot;tbl_employee&quot;)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
public class EmployeeEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	Long employeeId;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = &quot;designationId&quot;)
	private Designation designationInfo;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = &quot;countryId&quot;)
	private Country country;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = &quot;stateId&quot;)
	private State state;
}

Country.java
@Entity(name = &quot;tbl_country&quot;)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
public class Country {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	Long countryId;
	String country_name;
	
	 @OneToMany(cascade = CascadeType.ALL)
	 @JoinColumn(name = &quot;fk_country_id&quot;,referencedColumnName = &quot;countryId&quot;)
	private List&lt;State&gt; states;
}


State.java
@Entity(name = &quot;tbl_state&quot;)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
public class State {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	Long stateId;
	String state_name;

	 @ManyToOne
	 @JoinColumn(name = &quot;fk_country_id&quot;)
	 private Country country;
}

答案1

得分: 0

只需更改关系问题即可解决

  @ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;designationId&quot; , unique = false)
	private Designation designationInfo;

	@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;countryId&quot;, unique = false)
	private Country country;

	@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;stateId&quot;, unique = false)
	private State state;
英文:

Just Changing the Relation the problem is resolved

  @ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;designationId&quot; , unique = false)
	private Designation designationInfo;

	@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;countryId&quot;, unique = false)
	private Country country;

	@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;stateId&quot;, unique = false)
	private State state;

huangapple
  • 本文由 发表于 2023年6月5日 18:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定