Entity Beans - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Entity Beans

Description:

No database access code in your code ... Uses '_at_Entity' annotation to declare POJO as and entity bean. 4. CMP Entity Bean Classes ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 14
Provided by: chme3
Category:
Tags: beans | entity | pojo

less

Transcript and Presenter's Notes

Title: Entity Beans


1
Entity Beans
  • Container Managed Persistence

2
Entity Beans
  • Represents a business entity in persistent
    storage
  • Persistent
  • Allow shared access
  • Multiple Clients
  • Have primary keys

3
Container Managed Persistence
  • Container automatically generates database access
    calls
  • Entity Manager
  • Handles Objects Interaction with database
  • No database access code in your code
  • Makes code more portable, container generates
    correct SQL to match database
  • Uses _at_Entity annotation to declare POJO as and
    entity bean

4
CMP Entity Bean Classes
  • Any Plain Old Java Object can be annotated as an
    Entitiy bean
  • But
  • Can be automatically generated from database
    definition

5
Container Managed Fields
  • The CMP entity bean has some container managed
    fields
  • These are specified using the _at_Column(namefieldn
    ame) annotation
  • _at_Id specifies the primary key field
  • _at_GeneratedValue annotation shows a field that is
    automatically generated by the database
    (autoincrement)

6
Entity Beans use
  • An Entity Manager is used to access an entity
    bean
  • An entity manager is included in a session bean
    using the
  • _at_PersistenceContext(unitNameejbPUname)
  • Protected EntityManager em
  • Using the Entity Manager to find an instance
  • em.find(sbAppejb.Srecord.class,StudentID)
  • Storing a new Value
  • Create new object MyRecord
  • em.persist(MyRecord)

7
  • An entity manager is included in a servlet using
    the
  • _at_PersistenceUnit(unitNameejbPUname)
  • Protected EntityManagerFactory emf
  • Protected EntityManager em
  • ememf.createEntityManager()
  • Used as in Session beans
  • Used this way as Entity Manager not thread safe

8
Using Named Queries
  • Autogenerated Entity beans will have named EJBQL
    queries already defined
  • E.g
  • _at_NamedQueries(_at_NamedQuery(nameSrecord.findByNam
    e,querySELECT s FROM Srecord s where s.name
    name......)
  • Use in session bean and servlets
  • Query query em.createNamedQuery(Srecord.findByN
    ame)
  • query.setParameter(name,name)
  • return query.getResultList()

9
Creating Queries
  • You can create your own queries in session bean
    and servlets
  • Query query em.createQuery(SELECT s FROM
    Srecord s where s.studentID gt studentID)
  • query.setParameter(studentID, studentID)
  • return query.getResultList()

10
Bean Managed Persistence
  • You write the code to access the database (not in
    Ejb3)
  • More under your control
  • Suitable for more complex interaction with
    database
  • Not as portable if migrating to different
    database back end
  • Not recommended

11
Entity Bean Life Cycle
unsetEntityContext
setEntityContext
remove ejbRemove
ejbPassivate
ejbActivate
create setSessionContext ejbCreate
12
Netbeans/mysql setup
  • Netbeans requires a password for the database
    connection used
  • So create a test user with test password on mysql
  • grant all on . to test_at_localhost identified by
    test

13
Summary
Main points to remember Container managed
persistence Entity Manager to interact with the
database
Write a Comment
User Comments (0)
About PowerShow.com