Unit 7 The Business Tier RMI, JNDI, Java Persistence and Session Beans Flashcards

1
Q

Exam 2008 What are entity classes in Java EE and what purpose do they serve?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exam 2008 How do Entity classes and entities map to a relational database.

A
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;...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain what session beans are in Java EE and distinguish the two main types in terms of what each one is useful for.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the role of an entity class?

A

As part of the Java Persistence API, it provides business data, capture persistent entities that are stored in a database. Package javax.persistence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the mechanisms of Javax.persistence package?

A
  1. Annotations
  2. API to manipulate entities
  3. A query language
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are annotations?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Outline the steps of creating an entity class.

A
  1. One line per table
  2. Declare an inst variable corresponding to each column. If any column is a foreign key then the type must correspond to the foreign table.
  3. All inst variables are private with getter and setter messages.
  4. Override toString() to display the primary key @Id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we map an entity class to relational schema?

A
  1. import javax.persistence.*;
  2. annotate the class @Entity (shows the instances may be stored persistently)
  3. @Table (name = string) annotates the table corresponding to the class.
  4. @Id the inst variable which is the unique primary key
  5. @JoinColumn(name=string) foreign key
  6. @Column(name=string) where the column name doesn’t match the instance variable names
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you map unidirectional relationships?

A
@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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we store changes back to the database?

A

By using a persistence context and an entity manager. The EntityManager uses EntityTransaction to methods such as begin, commit, rollback and find.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the stages in a entity Lifecycle?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the role of a session bean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the states in a session bean lifecycle?

A

Stateless is either READY or NON EXISTENT

Stateful is either READY, NON EXISTENT or PASSIVE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How might you annotate a session bean?

A

@Stateful, @Stateless, @Remote, @Local

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an entity manager?

A

It’s part of the javax.persistence package. EntityManager interface specifies begin, commit, rollback, close
to put entities in a persistence context.

17
Q

How might an entity manager be managed?

A

Application managed - explicitly created and closed by the application
or
Container managed - created by dependency injection so transactions are implicitly created and committed.
Example
@PersistenceContext(unitName = “music”)
private EntityManager manager;

18
Q

What is dependency injection?

A

It’s where an object indicates that it depends on the existence of another object and the container ‘injects’ one at run-time. Usually achieved by annotating the declaration of the required object.
Example
@PersistenceContext(unitName = “music”)
private EntityManager manager;

19
Q

What are the two kinds of persistence context?

A

Extended - contexts exist from creation to closure of entity manager.
Transaction-scoped - exist from creation to close of a transaction.

20
Q

Which kind of persistence context can be used with which kinds of entity manager?

A

Application managed - extended only.

Container managed - either extended or transaction-scoped.

21
Q

SAQ Using the Java Persistence API promotes a succinct coding style that favours using defaults. Name some of them.

A
  1. A table has the same name as an entity class and columns have the same names as instance variables.
  2. Persistence contexts are transaction scoped when using container managed entity managers.
  3. When using container managed entity managers each method executes within the current transaction or else starts a new transaction and commits it when the method terminates.
22
Q

How do we use a session bean with dependancy injection?

A

The client uses the following code to access the bean’s interface
@EJB
SessionBean bean;

23
Q

What are the characteristics of stateful Beans and Stateless Beans

A
Stateful 
Not shared - 1 bean for one client
Not persistent - state is not stored
Useful where info regarding the client or interaction history may be needed
Stateless
useful for generic one off tasks
Scalable - can be reused more easily
Better performance - do not need to be deserialised from main memory.
24
Q

What is a transfer object?

A

An object used to simplify data transfer between client and session bean.
Aggregates data to simplify business interface and reduce network load by reducing number of method calls.

25
Q

What is JNDI?

A

It is a set of interfaces that allows Java applications to access different directory service providers in a consistent way. It is vendor Neutral.
The API is provided by javax.naming package and it has a Context interface that provides bind, rebind list, and look up operations similar to RMI.