0
votes

Tables:

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;

Entity Classes:

@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
    }
    }

In child_table parent_id column as foreign key and part of the composite primary key.

Inside Embedded class unable to use identity generator. so i am using here ID classes. To generate auto-increment value for child_id column.

I am unable to store parent_id value generated from the parent_table into the child_table as foreign key value it storing as 0.

can someone please check the mappings help me.....

advance thanks...

2

2 Answers

0
votes

If the child id is unique already, why do you need the parent id to be part of the primary key? Just use:

@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;
    
    }
0
votes
@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
    }
    }