Hibernate JPA 2.0 仅更新并插入子表
Hibernate JPA 2.0 update only and insert in child tables
我是 Hibernate JPA 新手.....我有一个父 table 和多个子 tables.I 想根据父 table 在子 table 中插入记录=24=] existence.I 不想在父 table.If 中插入任何记录 table.If 父 table 中存在条目,然后我想在子 table 中插入。我尝试将父 table 主键中的 insertable 和 updatable 设置为 false。但它不工作。我正在使用 JPA 2.0 version.Thank 你提前。请让我知道任何其他信息。
================ 从评论中复制粘贴:
代码示例:
@Entity
@NamedQuery(name="Custome.findAll", query="SELECT p FROM Customer p")
public class Customer extends AbstractObject implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CustomerPK pk;
@Temporal(TemporalType.DATE)
@Column(name="end_dt")
private Date endDt;
@Column(name="desc_txt")
private String descTxt;
//bi-directional many-to-one association to Dependent
@OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
private List<Dependent> dependents = new ArrayList<>();
}
我想更新 customer table 中的 descTxt 并想插入 Dependent table 。如果 CustomerPK 存在于 table 中,那么我想更新,如果不存在,我想跳过更新 Customer table 中的记录和插入。目前我正在使用 session.saveorUpdate(object) 如果记录存在它正在更新父记录并插入子 table ,但是如果父记录不存在那么它正在插入我没有的父记录和子记录'不想插入父记录
JPA 不会解决这个问题。如果您调用 'saveOrUpdate',它将保存或更新。如果它表现得像你期望的那样,那将是非常糟糕的。
实际上,如果您不打算保存新的 Customer 对象,则永远不要创建它。您最好在 JPA 之外实现签入代码,例如
Customer customer = entityManager.find(Customer.class, customerPK);
if(customer != null){
customer.setDescription(...)
customer.addDependent(new Dependent (...))
//customer + dependents will be saved at end of transaction or at flush
}
else {
LOG.info("Ignoring unexisting id", customerPk)
}
注:
- 如果客户实例是由 Hibernate 创建的,则无需调用 session.saveOrUpdate,并且您进行了适当的 transaction/session 管理。 Hibernate 将跟踪当前会话中对持久对象的所有更改,并在会话结束时刷新它们。
- 如果您将 Dependent 添加到客户的家属列表中(最佳做法是使用 addXxx 方法),它也会在会话结束时使用正确的外键自动插入。
我是 Hibernate JPA 新手.....我有一个父 table 和多个子 tables.I 想根据父 table 在子 table 中插入记录=24=] existence.I 不想在父 table.If 中插入任何记录 table.If 父 table 中存在条目,然后我想在子 table 中插入。我尝试将父 table 主键中的 insertable 和 updatable 设置为 false。但它不工作。我正在使用 JPA 2.0 version.Thank 你提前。请让我知道任何其他信息。
================ 从评论中复制粘贴:
代码示例:
@Entity
@NamedQuery(name="Custome.findAll", query="SELECT p FROM Customer p")
public class Customer extends AbstractObject implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CustomerPK pk;
@Temporal(TemporalType.DATE)
@Column(name="end_dt")
private Date endDt;
@Column(name="desc_txt")
private String descTxt;
//bi-directional many-to-one association to Dependent
@OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
private List<Dependent> dependents = new ArrayList<>();
}
我想更新 customer table 中的 descTxt 并想插入 Dependent table 。如果 CustomerPK 存在于 table 中,那么我想更新,如果不存在,我想跳过更新 Customer table 中的记录和插入。目前我正在使用 session.saveorUpdate(object) 如果记录存在它正在更新父记录并插入子 table ,但是如果父记录不存在那么它正在插入我没有的父记录和子记录'不想插入父记录
JPA 不会解决这个问题。如果您调用 'saveOrUpdate',它将保存或更新。如果它表现得像你期望的那样,那将是非常糟糕的。
实际上,如果您不打算保存新的 Customer 对象,则永远不要创建它。您最好在 JPA 之外实现签入代码,例如
Customer customer = entityManager.find(Customer.class, customerPK);
if(customer != null){
customer.setDescription(...)
customer.addDependent(new Dependent (...))
//customer + dependents will be saved at end of transaction or at flush
}
else {
LOG.info("Ignoring unexisting id", customerPk)
}
注:
- 如果客户实例是由 Hibernate 创建的,则无需调用 session.saveOrUpdate,并且您进行了适当的 transaction/session 管理。 Hibernate 将跟踪当前会话中对持久对象的所有更改,并在会话结束时刷新它们。
- 如果您将 Dependent 添加到客户的家属列表中(最佳做法是使用 addXxx 方法),它也会在会话结束时使用正确的外键自动插入。