3
votes

I have 2 model/annotated classes, ProductDetails and VnfDetails. I want to join the 2 tables with @OnetoMany relation using JPA HIbernate

ProductDetails model class is below with @OnetoMany Mapping:

@Entity
@Table(name="product_details")
public class ProductDetails {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")
private int id;        

@Column(name="PRODUCT_ID")
private String productId;

@Column(name="PRODUCT_NAME")
private String productName;

@OneToMany(mappedBy="productDetails", cascade=CascadeType.ALL)
private Set<VnfDetails> vnfd;


public Set<VnfDetails> getVnfd() {
    return vnfd;
}

public void setVnfd(Set<VnfDetails> vnfd) {
    this.vnfd = vnfd;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

VnfDetails is below having @ManytoOne with JoinColumn:

@Entity
@Table(name="vnf_details")
public class VnfDetails {


@Id
@Column(name="VNF_ID")
private String vnfId;

@Column(name="VNF_NAME")
private String vnfName;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="PRODUCT_ID")
private ProductDetails productDetails;

public ProductDetails getProductDetails() {
    return productDetails;
}

public void setProductDetails(ProductDetails productDetails) {
    this.productDetails = productDetails;
}

public String getVnfId() {
    return vnfId;
}

public void setVnfId(String vnfId) {
    this.vnfId = vnfId;
}

public String getVnfName() {
    return vnfName;
}

public void setVnfName(String vnfName) {
    this.vnfName = vnfName;
}

when I am inserting data, the data is getting inserted but the PRODUCT_ID in VnfDetails table is null.

It should be the foreign key having "ID" of PRODUCT_DETAILS table.

Unable to resolve the issue.

4

4 Answers

4
votes

In Unidirectional Mapping i had similar issue that it is inserting null value in foreign key column and got run time exception datavoilation exception.

Issue got fixed by adding nullable=false on @JoinColumn like

@OnetoMany
@JoinColumn(nullable=false) 

and removed the foreign key column definition in child entity.

2
votes

Since this is a Bidirectional relation, you need to make sure you are inserting each entity with reference to the other side of the relation, since you are cascading the vndf inside product details you should do something like this

productDetails.setVndf(vndfList);//vndfList is having one item for example
vndfList.get(0).setProductDetails(productDetails);
productDetailsDao.save(productDetails);
0
votes

Define the joinColumn in onetoManyClass without any reference in ProductDetails.

@OneToMany 
@JoinColumn(name="qid") 
private Set<VnfDetails> vnfd;
-3
votes
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="PRODUCT_ID")
private ProductDetails productDetails;

PRODUCT_ID is not a PK in ProductDetails entity and that is the reason why your mapping doesn't work. Try changing it to ID column and it should work fine.