休眠4.3.11
我有一个问题,节省休眠以下对象图。雇主正在使用合并()方法保存。
Employer
|_ List<EmployerProducts> employerProductsList;
|_ List<EmployerProductsPlan> employerProductsPlan;
用人单位与EmployerProducts有一个自动生成的PK。所述EmployerProductsPlan是由所述EmployerProducts ID和与该计划代码的字符串的组合键。
当有在EmployerProducts列表级联到List<EmployerProductsPlan>
临时对象时发生错误。我遇到的第一个错误,我一直在试图让过去是内部休眠NPE。这里这篇文章极好地描述,我有这将导致空指针Hibernate NullPointer on INSERTED id when persisting three levels using @Embeddable and cascade问题
该OP留下评论指定他们做了什么来解决,但改变所建议的映射时我结束了一个不同的错误。更改映射后,我现在越来越
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.webexchange.model.EmployerProductsPlan#com.webexchange.model.EmployerProductsPlanId@c733f9bd]
由于其他库的依赖,我不能上方版本4.3.x在这个时候升级。该项目是利用弹簧引导起动数据JPA 1.3.3。没有其他工作正在上比调用合并(),将通过雇主对象以外的会话中执行。
下面是每个类的映射:
雇主
@Entity
@Table(name = "employer")
@lombok.Getter
@lombok.Setter
@lombok.EqualsAndHashCode(of = {"employerNo"})
public class Employer implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "EMPLOYER_NO", unique = true, nullable = false)
private Long employerNo;
.....
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employer", orphanRemoval = true)
private List<EmployerProducts> employerProductsList = new ArrayList<>(0);
}
EmployerProducts
@Entity
@Table(name = "employer_products")
@Accessors(chain = true) // has to come before @Getter and @Setter
@lombok.Getter
@lombok.Setter
@lombok.EqualsAndHashCode(of = {"employerProductsNo"})
public class EmployerProducts implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "employer_products_no", unique = true, nullable = false)
private Long employerProductsNo;
@ManyToOne
@JoinColumn(name = "employer_no", nullable = false)
private Employer employer;
......
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employerProducts", orphanRemoval = true)
private List<EmployerProductsPlan> employerProductsPlanList = new ArrayList<>(0);
}
EmployerProductsPlan
@Accessors(chain = true) // has to come before @Getter and @Setter
@lombok.Getter
@lombok.Setter
@lombok.EqualsAndHashCode(of = {"id"})
@Entity
@Table(name="employer_products_plan")
public class EmployerProductsPlan implements Serializable {
@EmbeddedId
@AttributeOverrides({ @AttributeOverride(name = "plan", column = @Column(name = "epp_plan", nullable = false)),
@AttributeOverride(name = "employerProductsNo", column = @Column(name = "employer_products_no", nullable = false)) })
private EmployerProductsPlanId id;
@ManyToOne
@JoinColumn(name = "employer_products_no")
@MapsId("employerProductsNo")
private EmployerProducts employerProducts;
}
我上面填充与正在保存EmployerProducts对象的相同实例中employerProducts。它是短暂的,没有ID填充,因为它没有在数据库现有呢。
EmployerProductsPlanId
@Accessors(chain = true) // has to come before @Getter and @Setter
@lombok.Getter
@lombok.Setter
@lombok.EqualsAndHashCode(of = {"plan", "employerProductsNo"})
@Embeddable
public class EmployerProductsPlanId implements Serializable {
private String plan;
private Long employerProductsNo;
// This was my previous mapping that was causing the internal NPE in hibernate
/* @ManyToOne
@JoinColumn(name = "employer_products_no")
private EmployerProducts employerProducts;*/
}
UPDATE:显示Struts控制器和DAO。雇主对象永远不会从分贝保存之前加载。支柱是创建从HTTP请求的参数此整个对象图。
Struts的2.5控制器
@lombok.Getter
@lombok.Setter
public class EditEmployers extends ActionHelper implements Preparable {
@Autowired
@lombok.Getter(AccessLevel.NONE)
@lombok.Setter(AccessLevel.NONE)
private IEmployerDao employerDao;
private Employer entity;
....
public String save() {
beforeSave();
boolean newRecord = getEntity().getEmployerNo() == null || getEntity().getEmployerNo() == 0;
Employer savedEmployer = newRecord ?
employerDao.create(getEntity()) :
employerDao.update(getEntity());
setEntity(savedEmployer);
return "success";
}
private void beforeSave() {
Employer emp = getEntity();
// associate this employer record with any products attached
for (EmployerProducts employerProduct : emp.getEmployerProductsList()) {
employerProduct.setEmployer(emp);
employerProduct.getEmployerProductsPlanList().forEach(x ->
x.setEmployerProducts(employerProduct));
}
// check to see if branding needs to be NULL. It will create the object from the select parameter with no id
// if a branding record has not been selected
if (emp.getBranding() != null && emp.getBranding().getBrandingNo() == null) {
emp.setBranding(null);
}
}
}
employer DAO
@Repository
@Transactional
@Service
@Log4j
public class EmployerDao extends WebexchangeBaseDao implements IEmployerDao {
private Criteria criteria() {
return getCurrentSession().createCriteria(Employer.class);
}
@Override
@Transactional(readOnly = true)
public Employer read(Serializable id) {
return (Employer)getCurrentSession().load(Employer.class, id);
}
@Override
public Employer create(Employer employer) {
getCurrentSession().persist(employer);
return employer;
}
@Override
public Employer update(Employer employer) {
getCurrentSession().merge(employer);
return employer;
}
}
截至目前,我的解决办法是通过EmployerProducts循环并检查新记录。我叫调用合并()父用人单位面前的新的一种坚持。我也感动的逻辑我在我的Struts的行动有它的所有键关联到DAO来代替。下面是我的更新()在雇主DAO方法现在看起来像
public Employer update(Employer employer) {
// associate this employer record with any products attached
for (EmployerProducts employerProduct : employer.getEmployerProductsList()) {
employerProduct.setEmployer(employer);
if (employerProduct.getEmployerProductsNo() == null) {
// The cascade down to employerProductsPlanList has issues getting the employerProductsNo
// automatically if the employerProduct does not exists yet. Persist the new employer product
// before we try to insert the new composite key in the plan
// https://stackoverflow.com/questions/54517061/hibernate-4-3-cascade-merge-through-multiple-lists-with-embeded-id
List<EmployerProductsPlan> plansToBeSaved = employerProduct.getEmployerProductsPlanList();
employerProduct.setEmployerProductsPlanList(new ArrayList<>());
getCurrentSession().persist(employerProduct);
// add the plans back in
employerProduct.setEmployerProductsPlanList(plansToBeSaved);
}
// associate the plan with the employer product
employerProduct.getEmployerProductsPlanList().forEach(x ->
x.getId().setEmployerProductsNo(employerProduct.getEmployerProductsNo())
);
}
return (Employer)getCurrentSession().merge(employer);
}