You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by infinity2heaven <in...@gmail.com> on 2011/06/15 00:18:09 UTC
Update statements not generated for a @OneToMany mapping
OpenJPA 1.2.2
Spring 3
HSQLDB 2.0
JUnit Testing with SpringJUnit4ClassRunner & Spring @Transactional on
Testclass
--
class Settlement {
@OneToMany(mappedBy = "settlement", Cascade=CascadeType.REFRESH)
private List<VoucherProcessed> vouchersProcessed = new
ArrayList<VoucherProcessed>(0);
...
}
public class VoucherProcessed {
@OneToOne
@JoinColumn
private Voucher voucher;
@ManyToOne
@JoinColumn
private Settlement settlement;
...
}
The service class passes a new Settlement object along with a list of
vocherIds (since VoucherProcessed
are already persisted)
// dao
@Transactional
public void save(Settlement settlement, List<Long> voucherIds) {
// Load VoucherProcessed
List<VoucherProcessed> vouchersProcessed = (List<VoucherProcessed>) em
.createQuery("SELECT vp FROM VoucherProcessed vp WHERE
vp.voucher.voucherId IN (:voucherIds)")
.setParameter("voucherIds", voucherIds).getResultList();
if (vouchersProcessed != null) {
settlement.setVouchersProcessed(vouchersProcessed);
em.persist(settlement);
}
}
The above code generates INSERT for settlement but there's no update for
Voucher like:
UPDATE Voucher SET settlement.id=?
However, if I pass Settlement with a persistent VoucherList to the dao,
Update is generated.
What am I missing?
--
View this message in context: http://openjpa.208410.n2.nabble.com/Update-statements-not-generated-for-a-OneToMany-mapping-tp6476296p6476296.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Update statements not generated for a @OneToMany mapping (with
persistent entity)
Posted by Pinaki Poddar <pp...@apache.org>.
Also you should call voucher.setSettlement(s). JPA, by default, will not
manage bi-directional referential integrity. The application has to do it.
OpenJPA has a inverse manager -- you can activate that and most likely with
the current code you will see an exception.
But asking OpenJPA to check referential integrity of bi-directional relation
is costly.
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Update-statements-not-generated-for-a-OneToMany-mapping-with-persistent-entity-tp6476296p6476807.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Update statements not generated for a @OneToMany mapping (with
persistent entity)
Posted by Pinaki Poddar <pp...@apache.org>.
add cascade type MERGE, and call em.merge(settlement). Remember that merge
returns a Settlement different than its input argument.
Looks like a non-owning, owning side issue. Settlement is the non-owning
side of the relation. Owning side is the one that has the Foreign Key for
the relation in the database.
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Update-statements-not-generated-for-a-OneToMany-mapping-with-persistent-entity-tp6476296p6476786.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Update statements not generated for a @OneToMany mapping (with
persistent entity)
Posted by infinity2heaven <in...@gmail.com>.
It's a typo in this post. code compiles just fine. Cascade type shouldn't
matter because I'm not persisting Voucher here (it's already a persistent
entity as it's reloaded in dao). As a test, CascadeType.All doesn't make any
difference.
--
View this message in context: http://openjpa.208410.n2.nabble.com/Update-statements-not-generated-for-a-OneToMany-mapping-with-persistent-entity-tp6476296p6476707.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Update statements not generated for a @OneToMany mapping (with
persistent entity)
Posted by Pinaki Poddar <pp...@apache.org>.
> What am I missing?
> @OneToMany(mappedBy = "settlement", Cascade=CascadeType.REFRESH)
Correct cascade type. (also the capital case on Cascade looks a suspect)
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Update-statements-not-generated-for-a-OneToMany-mapping-with-persistent-entity-tp6476296p6476693.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.