我正在尝试实现继承,但是方便我检索数据。所以我有2个POJO,一个是父实体,另一个扩展Parent是子,基本上,我使用子作为映射器以key:value对方式从Parent表中检索值。所以这两个键都可以复制。有没有办法可以执行此操作。我想在child_Id:parent_Id中创建一个结构,其中一个child_Id可以有许多不同的parent_Id链接到它,例如。 1:1,1:2,2:1,2:3,3:1等。但由于继承,我无法复制parent_Id列。有工作吗?我知道它可能违反了规范化规则但是有可能吗?如果是这样的话?
Computer.java(父实体)
import java.sql.Timestamp;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name="Computer", uniqueConstraints={@UniqueConstraint(columnNames = {"tac_Code" , "config_Code"})})
@Embeddable
@Inheritance(strategy=InheritanceType.JOINED)
@NoArgsConstructor
@AllArgsConstructor
@ToString
public abstract class Computer {
@Id
@Column(name = "ID", nullable = false)
@JoinColumn(name="laptop_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
protected Long computerId;
......
}
这是Child类 ReplacementComputer.java(子实体)
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
* The Class ReplacementComputer
*/
@Entity
@NoArgsConstructor
@AllArgsConstructor
@ToString
@PrimaryKeyJoinColumn(name="computer_id")
public class ReplacementComputer extends Computer implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
@OneToMany(cascade = CascadeType.ALL, targetEntity = Product.class)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JoinColumn(name="REPLACEMENTComputer_ID")
protected Collection<Computer> computers = new ArrayList<Computer>();
// ... getters and setters
}
在为ReplacementComputer_computers中的List创建3个表之后,将计算机作为父表,以及ReplacementComputer。因此,每台计算机都会有几个与之相关的替换计算机,replacementComputer将映射回计算机表以检索详细信息。这是我试图完成的流程。但是在MySQLWorkbench中,如果已经存在这些组合,
1,2 1,3 1,4 2,1,
这是我试图坚持时得到的错误(2,3)。
操作失败:将SQL脚本应用于数据库时出错。
执行:
INSERT INTO testingdb1
。replacement_computer_computers
(replacement_computer_computer_id
,computers_computer_id
)VALUES(&#39; 2&#39;,&#39; 3&#39;);
错误1062:1062:重复输入&#39; 3&#39;关键词&#39; UK_lbghmganelv3o7oupon6f798c&#39;
SQL语句:
INSERT INTO testingdb1
。replacement_computer_computers
(replacement_computer_computer_id
,computers_computer_id
)价值观(&#39; 2&#39;,&#39; 3&#39;)
任何帮助都很好,谢谢你。