Last active
December 24, 2015 23:29
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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