Skip to content

Instantly share code, notes, and snippets.

@rjdkolb
Last active December 24, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjdkolb/6881143 to your computer and use it in GitHub Desktop.
Save rjdkolb/6881143 to your computer and use it in GitHub Desktop.
# 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