I've designed some tables for my application where I've placed something like that:
product productversion
--- ---
productId productVersionId
productVersionId (ref) productId (ref)
name length
height
where we have relation one to many . I just need some base product with parameters which can change and versioned parameters so I can watch it's history. Now... I have problem with Persisting those objects. I've tried multiple ways, this is oldest one:
Product p = new Product();
p.name = "some name";
Productversion pv = new Productversion();
pv.length = 10;
pv.height = 20;
p.productversionId = pv;
But while trying to persist this object I've getting error that productId cannot be null and it refers to field productId inside Productversion object. How can I do this?
[update] Some more information about my problem:
public class Product implements Serializable {
@JoinColumn(name = "productversionId", referencedColumnName = "productversionId")
@ManyToOne(optional = true, cascade = CascadeType.PERSIST)
private Productversion productversionId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productId")
private Collection<Productversion> productversionCollection;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ProductId")
private Integer productId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "Name")
private String name = null;
// well I believe construct and getter/setter not important now
And second Entity:
public class Productversion implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productversionId")
private Collection<Productiondata> productiondataCollection;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "productversionId")
private Integer productversionId;
@Basic(optional = false)
@NotNull
@Column(name = "parameter")
private float weight;
And the way I create new object (just have no idea how can I do this):
public Product getNewProduct() {
if (newProduct == null) {
this.newProduct = new Product();
Productversion pv = new Productversion();
pv.setProductId(newProduct);
newProduct.setProductversionId(pv);
}
return newProduct;
}
And the way I'm trying to persist object:
public Boolean addEntry(Product productData) {
Boolean status = false;
DbConnector dc = new DbConnector();
EntityManager em = dc.getEntityManager();
try {
EntityTransaction et = em.getTransaction();
et.begin();
em.persist(productData);
et.commit();
status = true;
} finally {
em.close();
}
return status;
}
Seems like you missing on the set for the id field in your Product object.
Product p = new Product();
p.id = 1; <<<<<<-------------------
p.name = "some name";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.