How does an entity bean create a new entity in the database when a client calls the home interface’s create() method?
ejbCreate() is called by the container when a client creates a new entity bean instance. The method inserts a new record into the database and returns the primary key to the container.
- Client calls
home.create(accountID, ownerName)on home interface - Container gets a pooled bean instance
- Container calls
ejbCreate(accountID, ownerName)on the bean - Bean executes
INSERTquery to create database record - Bean returns the primary key (AccountPK) to container
- Container creates EJB Object and associates it with the bean
- Container calls
ejbPostCreate()after associating
- Each
createmethod in home interface has a correspondingejbCreatein bean class - Must return a Primary Key object to identify the new entity
- In BMP, developer writes INSERT logic in JDBC
- Container triggers after bean is associated with EJB Object
- Parameters vary based on what data the entity needs
- Built from: Entity Bean, Home Interface
- Builds into: ejbPostCreate(), Primary Key
- Related: Bean-Managed Persistence
- Must return a primary key, not the bean itself
- Only called when creating NEW database records (not for existing)
- After
ejbCreate()returns, the bean is no longer in pool—it has specific data