1 / 26

Entity Beans

Entity Beans. By James Kinkel Chris Bagley. Introduction. JavaBeans A Java Bean is a software component that is reusable Serializable Used to temporarily store data Entity Bean Similar to a JavaBean Has additional components to allow for persistence of data. Purpose.

honora
Télécharger la présentation

Entity Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Entity Beans By James Kinkel Chris Bagley

  2. Introduction • JavaBeans • A Java Bean is a software component that is reusable • Serializable • Used to temporarily store data • Entity Bean • Similar to a JavaBean • Has additional components to allow for persistence of data

  3. Purpose • To easily save data from business classes to the database • To allows for the easy manipulation of data and reduce coding time

  4. Entity Bean Defined • An Entity Bean is a class that contains a mechanism for persistence • Each Entity Bean should have an associated table in a database • They allow shared access, contain primary keys and can have relationships with other Entity Beans (ex. One-to-one, one-to-many, many-to-many) • Each instance of an entity bean represents a record in the database

  5. Entity Bean Defined • Entity beans allow for shared access • Each bean, much like a record in a table, has a unique identifier • The beans can have relationships between them

  6. Persistence • Persistence within an Entity Bean allows for the data contained within the beans properties to be stored • This can be done by using bean-managed or container-managed persistence

  7. Persistence • Bean-managed persistence: • Refers to calling methods that the developer will code within the bean • These methods contain calls to the database

  8. Persistence • Container-managed persistence: • Contains no SQL calls to the database • Not tied to a specific database • The abstract schema is used to define persistent fields and relationships • This schema is then used to generate virtual fields that are used for persistence

  9. Advantages • Only one of each entity bean is created (based on records) • This means that all users are looking at the most current data for each record • Makes CRUD simple for developers • Uses an object then method call instead of a SQL call to the database • Simple to make changes • Change the schema then change the appropriate associated xml file

  10. Disadvantages • Poor performance • When a get or set method is called, the bean’s load method must then be called, then the call to the database will be made • Direct call would connect to database and retrieve data • Entity beans are instantiated for each record • If you are returning 100 records, 100 instances of the bean will be created

  11. Class Involvement • Who wants to volunteer??? • Andrew? • George? • Illian?

  12. Competing Technologies • To get the same results as an entity bean in the Microsoft world, this would be handled by designing a 3 or 4 tiered system. • The system would utilize a Data Access Layer (DAL) and a Business Logic Layer (BLL). • The data is encapsulated in a Business object and by utilizing business processing logic each particular business entity is processed.

  13. Competing Technologies • The BLL isn’t a persistent storage mechanism and business objects cannot store data indefinitely. • The BLL uses the DAL for long term data storage and retrieval.

  14. Supporting Technologies • Entity beans are used within the JAVA environment • Entity beans can be used with Oracle databases as well as MS SQL • IBM WebSphere IDE supports entity bean development

  15. Vendors • Sun Microsystems • IBM • NetBeans • Eclipse • JBoss

  16. Code: Entity Bean Class import javax.ejb.*; import java.rmi.RemoteException; public class EmployeeBean implements EntityBean { public int id; public String firstname; public String lastname; public String email; public EntityContext context; public EmployeeBean() { } public Integer ejbCreate( String firstname, String lastname, String email ) throws CreateException { this.firstname = firstname; this.lastname = lastname; this.email = email; return null; } public void ejbPostCreate( String firstname, String lastname, String email ) throws CreateException { }

  17. Code: Entity Bean Class Cont. public void says( String message ) { System.out.println( "[" + firstname + " " + lastname + "(" + id + ") _ “ + email + "] " + message ); } public void setEntityContext( EntityContext context ) throws EJBException, _ RemoteException { this.context = context; } public void unsetEntityContext() throws EJBException, RemoteException { } public void ejbActivate() throws EJBException, RemoteException { } public void ejbLoad() throws EJBException, RemoteException { } public void ejbPassivate() throws EJBException, RemoteException { } public void ejbRemove() throws RemoveException, EJBException, RemoteException { } public void ejbStore() throws EJBException, RemoteException { } }

  18. Code: Home Interface import javax.ejb.CreateException; import javax.ejb.EJBHome; import javax.ejb.FinderException; import java.rmi.RemoteException; public interface EmployeeHome extends EJBHome { public Employee create( String firstname, String lastname, String email ) throws _ RemoteException, CreateException; public Employee findByPrimaryKey( Integer id ) throws RemoteException, _ FinderException; }

  19. Code: Remote Interface import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Employee extends EJBObject { public void says( String message ) throws RemoteException; }

  20. Code: ejb-jar.xml <ejb-jar> <enterprise-beans> <entity> <ejb-name>EmployeeBean</ejb-name> <home>org.acme.employee.EmployeeHome</home> <remote>org.acme.employee.Employee</remote> <ejb-class>org.acme.employee.EmployeeBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.Integer</prim-key-class> <reentrant>False</reentrant> <cmp-field> <field-name>id</field-name> </cmp-field> <cmp-field> <field-name>firstname</field-name> </cmp-field> <cmp-field> <field-name>lastname</field-name> </cmp-field> <cmp-field> <field-name>email</field-name> </cmp-field>

  21. Code: ejb-jar.xml Cont. <primkey-field>id</primkey-field> <resource-ref> <res-ref-name>jdbc/postgresql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </entity> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>EmployeeBean</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Supports</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>

  22. Code: Client App import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.util.Properties; public class EmployeeClient { public static void main( String[] args ) throws Exception { Properties env = new Properties(); String jndiProvider = "org.openejb.client.RemoteInitialContextFactory"; env.put( "java.naming.factory.initial", jndiProvider ); env.put( "java.naming.provider.url", "localhost:4201" ); env.put( "java.naming.security.principal", "fakeuser" ); env.put( "java.naming.security.credentials", "fakepass" ); Context ctx = new InitialContext( env );

  23. Code: Client App Cont. Object obj = ctx.lookup( "EmployeeBean" ); obj = PortableRemoteObject.narrow( obj, EmployeeHome.class ); EmployeeHome home = ( EmployeeHome ) obj; Employee empl_create = home.create( "Jacek", "Laskowski", "OpenEJB-" + System.currentTimeMillis() + "@SF.net" ); Integer primaryKey = ( Integer ) empl_create.getPrimaryKey(); Employee empl_find = home.findByPrimaryKey( primaryKey ); System.out.println( "Are the \"create\" and \"find\" users identical ? "+ empl_create.isIdentical( empl_find ) ); empl_find.says( "Hello OpenEJB World!" ); empl_find.remove(); } }

  24. Code: Supporting SQL create table employee ( id integer primary key, first_name varchar(15), last_name varchar(15), email varchar(30) ); create sequence employee_seq; (Code courtesy of http://openejb.codehaus.org/cmp_entity_postgresql.html)

  25. Conclusion • Java Entity Beans are similar to a Java Bean but provide an easy way to save data from business classes to the database and allows easy manipulation of data to reduce coding time. • Java Entity Beans have additional components to allow for the persistence of data and simplifies CRUD for developers. • Since Entity beans are instantiated for each record there can be performance issues when returning a large number of records.

  26. Question???

More Related