I have a parent table i.e. audit_log(parent) which contains a single column id. For a given id in audit_log, I have a list of vendor ID's. I am storing them in separate table audit_log_vendorid(child table). I want the child table to get the id from the parent table as one of the columns(parent_id). Here's the table schema.
audit_log
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(19) | NO | PRI | NULL | |
+-------+------------+------+-----+---------+-------+
audit_log_vendorid
+-----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+----------------+
| id | bigint(19) | NO | PRI | NULL | auto_increment |
| vendor_id | bigint(19) | NO | | NULL | |
| parent_id | bigint(19) | NO | | NULL | |
+-----------+------------+------+-----+---------+----------------+
I'have defined my hibernate classes as follows
@Entity
@Table(name="audit_log")
public class AuditLog {
private List<AuditVendorPair> vendorIDs;
public AuditLog(List<AuditVendorPair> vendorIds) throws Exception {
this.vendorIDs = vendorIDs;
}
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name = "audit_log_vendorid",
joinColumns = { @JoinColumn(name = "parent_id", referencedColumnName="id") })
public List<AuditVendorPair> getVendors() {
return vendorIDs;
}
@Id @Column(name="ID")
public Long getId() {
return super.getId();
}
public void setHostServices(List<AuditVendorPair> vendorIDs){
this.vendorIDs = vendorIDs;
}
}
My hibernate mapping class for audit_log_vendorid is below. I pass in a vendor id and expect the other two fields to be populated by hibernate. The parent_id field I want from "id" field in audit_log. It's initialized as null as of now causing mysql constraint exception.
@Entity
@Table(name="audit_log_vendorid")
public class AuditVendorPair {
private Long id;
private Long parent_id;
private Long vendor_id;
public AuditVendorPair(Long vendor_id){
this.vendor_id = vendor_id;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
@Column(name="vendor_id")
public Long getVendorID() {
return vendor_id;
}
public void setVendorID(Long vendor_id){
this.vendor_id = vendor_id;
}
@Column(name="parent_id")
public Long getParentId() {
return parent_id;
}
public void setParentId(Long parentID){
this.parent_id = parentID;
}
}
I am curious to know if my annotations are correct. I basically want the id from the audit_log table to be populated in the parent_id field in audit_log_vendorid table by hibernate.