CREATE TABLE `parent_table` (
`parent_id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`parent_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `child_table` (
`child_id` int NOT NULL AUTO_INCREMENT,
`parent_id` int NOT NULL,
PRIMARY KEY (`child_id`,`parent_id`),
FOREIGN KEY `fk2` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "parent_id")
private int parentId;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
private List<Child> childs=new ArrayList<Child>();
}
@Getter
@Setter
@EqualsAndHashCode
public class ChildPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="parent_id",nullable = false)
private int parentId;
@Column(name="child_id")
private int childId;
}
@Getter
@Setter
@EqualsAndHashCode
@Entity
@IdClass(ChildPK.class)
@Table(name = "child_table")
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="child_id")
private int childId;
@Id
@TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id",insertable = false,updatable = false)
private Parent parent;
}
class MainClass{
public static void main(String[] args)
{
entityManager.persist(parent);//in child_table parent_id storing as 0
}
}
在child_table parent_id列中作为外键和组合主键的一部分。
内部嵌入式类无法使用身份生成器。所以我在这里使用ID类。为child_id列生成自动增量值。
我无法将从parent_table生成的parent_id值存储为child_table作为存储为0的外键值。
有人可以检查一下映射对我有帮助吗.....
提前谢谢...
答案 0 :(得分:0)
如果子ID已经是唯一的,为什么您需要父ID成为主键的一部分?只需使用:
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "parent_id")
private int parentId;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
private List<Child> childs=new ArrayList<Child>();
}
@Getter
@Setter
@EqualsAndHashCode
@Entity
@Table(name = "child_table")
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="child_id")
@TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
private int childId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
}
答案 1 :(得分:0)
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "parent_id")
private int parentId;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
private List<Child> childs=new ArrayList<Child>();
}
@Getter
@Setter
@EqualsAndHashCode
@Embeddable
public class ChildPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="parent_id",nullable = false)
private int parentId;
@TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
@Column(name="child_id")
private int childId;
}
@Getter
@Setter
@EqualsAndHashCode
@Entity
@Table(name = "child_table")
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddableId
private ChildPK childPk=new ChildPk();
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("parentId")
@JoinColumn(name = "parent_id",insertable = false,updatable = false)
private Parent parent;
}
class MainClass{
public static void main(String[] args)
{
entityManager.persist(parent);//in child_table parent_id storing as 0
}
}