我正在尝试编写一个Spring批处理作业来解析嵌套的XML,并使用Hibernate将数据保存到多个表中。我能够使用StaxEventItemReader解组xml。我尝试使用HibernateItemWriter来保存数据,但这只保存顶级实体'Shipment',而不是映射实体'Party'。
这是我的xml
<Shipment>
<TransactionId>000551070008555</TransactionId>
<TransactionSetPurpose>Replace</TransactionSetPurpose>
<TransactionType>Customer</TransactionType>
<Parties>
<Party>
<PartyType>Account</PartyType>
<PartyCode>933334</PartyCode>
<Name>AT HOME</Name>
</Party>
<Party>
<PartyType>Shipper</PartyType>
<PartyCode>CVASIH</PartyCode>
<Name>CV.WIDHI ASIH BALI EXPORT</Name>
</Party>
<Parties>
</Shipment>
我的处理器类
public class ShipmentItemProcessor implements ItemProcessor<Shipment,
Shipment> {
@Override
public Shipment process(Shipment result) throws Exception {
System.out.println("Processing result :"+result);
Set<Party> partySet = new HashSet<Party>();
partySet.addAll(result.getParties().getParty());
result.setPartySet(partySet);
return result;
}
}
如何从处理器返回我的Party Set以及Shipment到HibernateItemWriter?
装运
@XmlRootElement(name = "Shipment")
@Entity
public class Shipment{
protected String transactionId;
protected String transactionSetPurpose;
protected String transactionType;
@Id
protected String fileNumber;
@Transient
protected Parties parties;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "shipment")
private Set<Party> partySet = new HashSet<Party>(0);
--getters and setters
}
方
@Entity
@XmlRootElement(name = "Party")
public class Party {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
protected String partyType;
protected String partyCode;
protected String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fileNumber", nullable = false)
protected Shipment shipment;
--getters and setters
}