Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# This example shows an example of a container managed object relational mapping (ORM - http://bit.ly/GKnZ04) exposed as a Enterprise Java Bean (EJB). ## The dependency AuctionItemService injected into AuctionItemExternalWorker is not a pure replacement for the new operator. i.e. An EJB is injected and not a simple class. ## When the method Auct…
@Stateless
public class AuctionItemExternalWorker {
@EJB
AuctionItemService service;
@EJB
AuditServiceAudit audit;
public void setMinBid(long bidId,long minBidCents) {
audit.auditSetMinBid(bidId,minBidCents);
service.setMinBid(bidId,minBidCents);
}
}
@Stateless
public class AuctionItemService {
@PersistenceContext(unitName = "AUCTION_STORE")
private EntityManager em;
public void setMinBid(long bidId,long minBidCents) {
AuctionItem item = em.find(AuctionItem.class, bidId);
if (item == null){
throw new IllegalArgumentException("Unable to find bid with ID "+bidId);
}
item.setMinBidCents(minBidCents);
}
}
@Stateless
public class AuditServiceAudit {
@PersistenceContext(unitName = "AUCTION_STORE")
private EntityManager em;
//This forces the audit to be in a separate transaction
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void setMinBid(long bidId,long minBidCents) {
em.persist(new AuctionItemAudit(bidId,minBidCents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment