0

I have an entity classes in JPA as below in which maId acts as a primary key and foreign key for many other tables.

@Table(name = "Table1")
public class Test implements Serializable {
    @Id
    @Column(name = "maId", updatable = false, nullable = false)
    private String id;
 
    @OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="maId")
    private MFieldData fieldData;

    @OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="maId")
    private MPS mps;

    @OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="maId")
    private MJob mJob;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="maId")
    private List<MExtension> mlExtensions;

    private Date createdDate;
    private Date lastUpdatedDate;
}

Now,this is my another entity.

@Table(name = "table 2")
public class Test implements Serializable {
    @Id
    @Column(name = "maId", updatable = false, nullable = false)
    private String maId;
    private Integer cmd;
    private String routeId;
}

By the time I receive a request this is API. I need to Insert the data across multiple tables.

  1. How to implement a custom UUID (maId) generator and use it in @Id?
  2. I need to use the same maId which is the unique id for this request across multiple entities while inserting into the DB. I am using a dirty way to do that. But is there any JPA way to solve this problem?

Any help would be highly appreciated!

vijay dhanakodi
  • 175
  • 1
  • 14

1 Answers1

1

The request has to pass the UUID (Called correlation-id) to track. https://docs.oracle.com/javase/7/docs/api/java/util/UUID.html can be used. If you generate in code it may go unknown to caller. The correlation-id is used to track the request data across systems it may travel due to event publish as well.

Satish
  • 713
  • 1
  • 5
  • 18