我有以下xml:
<business_partner_list>
<business_partner>
<business_partner_id>2004</business_partner_id>
<business_partner_name>S. C. Johnson & Sons Inc</business_partner_name>
<last_changed_date>2016-02-02T04:04:04</last_changed_date>
<department>
<department_id>242</department_id>
<department_name>TARGET.COM HARDLINES B</department_name>
</department>
<department>
<department_id>98</department_id>
<department_name>ADULT ATHLETIC & MEN'S CASUAL</department_name>
</department>
<business_partner/>
<business_partner>
<business_partner_id>3821</business_partner_id>
<business_partner_name>Johnson & Johnson</business_partner_name>
<last_changed_date>2016-02-02T04:04:04</last_changed_date>
<department>
<department_id>245</department_id>
<department_name>OTC-PERSONAL CARE</department_name>
</department>
<department>
<business_partner/>
</business_partner_list>
我使用JAXB对其进行了解析,并且我能够解析它。我使用Hibernate和jpa来持久保存在数据库中。
因此,它创建了两个实体类:
BusinessPartner.java和Department.java
这两个实体应该使用Hibernate和jpa持久化到DB。
BusinessPartner.java
@Entity(name="businessPartnerType")
public class BusinessPartnerType {
/*@Id
@GeneratedValue
protected long businessPartnerTypeId;*/
@Id
@XmlElement(name = "business_partner_id", required = true)
protected BigInteger businessPartnerId;
@XmlElement(name = "business_partner_name", required = true)
protected String businessPartnerName;
@OneToMany(mappedBy="businessPartnerType")/*(targetEntity=DepartmentType.class)*/
protected List<DepartmentType> department;
}
Department.java
@Entity(name="departmentType")
public class DepartmentType {
@Id
@XmlElement(name = "department_id", required = true)
protected BigInteger departmentId;
@XmlElement(name = "department_name", required = true)
protected String departmentName;
@XmlTransient
@ManyToOne(cascade=CascadeType.ALL)
protected BusinessPartnerType businessPartnerType;
}
现在我想要的是当我使用hibernate和jpa持久化BusinessPartner.java时,映射应该自动发生。但是我得到了例外:
javax.persistence.EntityNotFoundException: Unable to find target.jaxb.beans.DepartmentType with id 242
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:145)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1142)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1025)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:632)
这意味着我需要先保留Department.java,然后再保存BusinessPartner.java。
但是我有什么办法可以坚持使用BusinessPartner.java,而Hibernate会自己完成剩下的工作。
答案 0 :(得分:1)
尝试将BusinessPartnerType更改为:
@OneToMany(cascade=CascadeType.ALL, mappedBy="businessPartnerType")
protected List<DepartmentType> department;
这会导致DepartmentType对象自动保留。