【问题标题】:JPA related entities persistJPA 相关实体持续存在
【发布时间】:2015-02-05 12:30:05
【问题描述】:

我为我的应用程序设计了一些表格,其中放置了类似的内容:

product                            productversion
---                                ---
productId                          productVersionId
productVersionId (ref)             productId (ref)
name                               length
                                       height

我们有关系一对多。我只需要一些带有可以更改的参数和版本化参数的基本产品,这样我就可以查看它的历史。现在......我有持久化这些对象的问题。我尝试了多种方法,这是最古老的一种:

Product p = new Product();
p.name = "some name";
Productversion pv = new Productversion();
pv.length = 10;
pv.height = 20;
p.productversionId = pv;

但是在尝试保留此对象时,我收到错误,即 productId 不能为 null,它引用 Productversion 对象中的字段 productId。我该怎么做?

[更新] 关于我的问题的更多信息:

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

第二个实体:

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;

以及我创建新对象的方式(只是不知道我该怎么做):

public Product getNewProduct() {
    if (newProduct == null) {
        this.newProduct = new Product();
        Productversion pv = new Productversion();
        pv.setProductId(newProduct);
        newProduct.setProductversionId(pv);
    }
    return newProduct;
}

以及我尝试持久化对象的方式:

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

【问题讨论】:

  • 能否请您显示两个实体类的代码。
  • 当然我已经用代码更新了我的问题。
  • 有些东西看起来还是很奇怪。在Product 中,您定义ManyToOne (productversionId) 和ProductProductversion 之间的OneToMany (productversionCollection) 关系。 Productiondata的定义在哪里。

标签: java hibernate jpa


【解决方案1】:

您似乎在 Product 对象中的 id 字段的集合中丢失了。

Product p = new Product(); 
p.id = 1;   <<<<<<-------------------
p.name = "some name";

【讨论】:

  • 因为 id 由 MySQL 自动提供(通过自动递增),所以很难。
  • 你也可以定义你的 id 像:@Id @GeneratedValue long id;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2016-10-29
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
相关资源
最近更新 更多