Unit 7 The Business Tier RMI, JNDI, Java Persistence and Session Beans Flashcards
Exam 2008 What are entity classes in Java EE and what purpose do they serve?
Entity classes reside in the business tier. They contain data that is pertinent to the business. They can be used to reflect the contents of a database in Java, and ultimately they are used to made data changes persistent.
Entity classes are part of the generic Java Persistence API, which allows Java programs to interact easily with relational databases.
Entity classes provide the business data – they capture the persistent business entities that the enterprise application stores in a database. For the music shop scenario in the text, there could be entity classes to represent instruments and customers.
Exam 2008 How do Entity classes and entities map to a relational database.
An entity class represents a table in a database and an entity represents an instance of the table i.e. a line on the table. Entity classes map using the persistence frames and mapping is specified through annotations. See following example.
import javax.persistence.*;
@Entity @Table(name = "INSTRUMENTS") public class Instrument { @Id @Column(name = "INST_ID" ) private int id; @JoinColumn(name = "MODEL_ID") private InstrumentModel model;... }
Exam 2009 Explain how RMI uses names to access remote objects and why it uses this approach. Briefly outline the relation of RMI to JNDI
There is a three step process involved in RMI
The server binds an object to a symbolic name in the registry
The client uses the symbolic name to lookup the registry which returns a reference to the server object.
The client uses the reference to carry out processing.
JNDI can be used as an intermediary between the client and server (or multiple servers) to allow the client to lookup a server without knowing its location.
Explain what session beans are in Java EE and distinguish the two main types in terms of what each one is useful for.
A session bean has a business interface, through which the client can communicate with it. A session bean performs a task relative to the business tier. A session bean can be either stateful or stateless.
Stateful annotated by @Stateful this bean will hold the state ie the value of the inst variables during the session. It is useful in something like an online shopping cart, where it can gather the state ie the contents of the shopping basket.
A stateless bean @Stateless does not hold information. it is useful for generic one off tasks, where it need not gather information relating to the client.
Tutor reply
Session beans are used to respond to requests from the web and client tiers, they contain the business logic of the application. The two main types are statefull and stateless.
Stateless beans do not hold any data between requests, they are returned back to the pool. They are generally used for database queries.
Statefull beans have a state and can contain data between requests of a session. A good example of a statefull bean would be a shopping basket of an online store which will maintain state during different requests from the client
What is the role of an entity class?
As part of the Java Persistence API, it provides business data, capture persistent entities that are stored in a database. Package javax.persistence
What are the mechanisms of Javax.persistence package?
- Annotations
- API to manipulate entities
- A query language
What are annotations?
They provide info about code. Shorthand way of commenting and generating documentation. Start with @ and directly precede the code they annotate. May have elements such as name=value.
Outline the steps of creating an entity class.
- One line per table
- Declare an inst variable corresponding to each column. If any column is a foreign key then the type must correspond to the foreign table.
- All inst variables are private with getter and setter messages.
- Override toString() to display the primary key @Id
How do we map an entity class to relational schema?
- import javax.persistence.*;
- annotate the class @Entity (shows the instances may be stored persistently)
- @Table (name = string) annotates the table corresponding to the class.
- @Id the inst variable which is the unique primary key
- @JoinColumn(name=string) foreign key
- @Column(name=string) where the column name doesn’t match the instance variable names
How do you map unidirectional relationships?
@ManyToOne and @OneToMany from the perspective of that class Decide who owns the relationship. it is the class who is referred to by a foreign key. e.g. @OneToMany(mappedBy = "model", cascade=CascadeType.All)
How do we store changes back to the database?
By using a persistence context and an entity manager. The EntityManager uses EntityTransaction to methods such as begin, commit, rollback and find.
What are the stages in a entity Lifecycle?
It is New on creation.
From New it can move to Managed.
From Managed it can go to Removed.
From either Managed or Removed it can do to Detached.
Only Managed or Removed are in the persistence context.
What is the role of a session bean?
A session Bean encapsulates an interactive session of a single client with the application server. A session bean is an object that contains the application logic.
What are the states in a session bean lifecycle?
Stateless is either READY or NON EXISTENT
Stateful is either READY, NON EXISTENT or PASSIVE
How might you annotate a session bean?
@Stateful, @Stateless, @Remote, @Local