He estado buscando una solución por Internet pero sólo las he encontrado en inglés y me ha costado entenderlas. Tengo cuatro clases relacionadas tal que así:
- Clase Issue.java
Código:
- Clase MaintenaceProcedureCódigo Java:(...)Ver original
@Table(name = "table_issue") public class Issue implements IIssue { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "issueNumber", unique = true) private int issueNumber; @Column(name = "name") @Column(name = "email", unique = true) @Column(name = "description") @ManyToOne(cascade = CascadeType.PERSIST, targetEntity = Product.class) @JoinColumn(name = "code_p") private IProduct product; @ManyToOne(cascade = CascadeType.PERSIST, targetEntity = MaintenanceProcedure.class) @JoinColumn(name = "code_mp") private IMaintenanceProcedure maintenanceProcedure; @ManyToOne(cascade = CascadeType.PERSIST, targetEntity = Technician.class) @JoinColumn(name = "technicianNumber") private ITechnician technician; @Column(name = "response") @Column(name = "punctuation") private int punctuation; @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Issue.class) @JoinColumn(name = "issueNumber") private Set<IIssue> recordList = new HashSet<IIssue>();
Código:
- Clase TechnicianCódigo Java:(...)Ver original
@Table(name = "table_maintenanceProcedure") public class MaintenanceProcedure implements IMaintenanceProcedure { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "code_mp", unique = true) private int code_mp; @Column(name = "name", unique = true) @Column(name = "description") @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Product.class) @JoinColumn(name = "code_p") private Set<IProduct> productList = new HashSet<IProduct>(); @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Technician.class) @JoinColumn(name = "technicianNumber") private Set<ITechnician> technicianList = new HashSet<ITechnician>(); @OneToMany(mappedBy = "issueNumber", cascade = CascadeType.PERSIST, targetEntity = Issue.class) private Set<IIssue> issueList = new HashSet<IIssue>();
Código:
- Clase ProductCódigo Java:(...)Ver original
@Table(name = "table_technician") public class Technician implements ITechnician { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "technicianNumber", unique = true) private int technicianNumber; @Column(name = "email", unique = true) @Column(name = "password") @Column(name = "name") @Column(name = "lastname") @Column(name = "city") @Column(name = "country") @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = MaintenanceProcedure.class) @JoinColumn(name = "code_mp") private Set<IMaintenanceProcedure> maintenanceProcedureList; @OneToMany(mappedBy = "issueNumber", cascade = CascadeType.PERSIST, targetEntity = Issue.class) private Set<IIssue> issueList;
Código:
Estas clases se relacionan tal que así:Código Java:Ver original
@Table(name = "table_product") public class Product implements IProduct { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "code_p", unique = true) private int code_p; @Column(name = "name", unique = true) @Column(name = "description") @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = MaintenanceProcedure.class) @JoinColumn(name = "code_mp") private Set<IMaintenanceProcedure> maintenanceProcedureList; @OneToMany(mappedBy = "issueNumber", cascade = CascadeType.PERSIST, targetEntity = Issue.class) private Set<IIssue> issueList; (...)
(todas bidireccionales)
Issue 0..* ----- 0..* Issue
Issue 0..* ----- 1 Product
Issue 0..* ----- 1 MaintenanceProcedure
Issue 0..* ----- 1 Technician
Product 1..* ----- 1..* MaintenanceProcedure
Technician 0..* ----- 1..10 MaintenanceProcedure
El problema es el siguiente. Al insertar un objeto de tipo Issue en la base de datos, me sale este error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Cannot add or update a child row: a foreign key constraint fails (`acmedb`.`table_issue`, CONSTRAINT `FK2395B528A0E34810` FOREIGN KEY (`issueNumber`) REFERENCES `table_technician` (`technicianNumber`))
¿Qué es lo que falla entre las relaciones? Muchas gracias y un saludo