Thursday, June 30, 2011

JPA2 - 30 iunie

em.find(Employee.class,14)
em.remove(emp);
em.getTransaction().begin();
em.getTransaction().commit();
em.close();
emf.close();

Query - either Query or TypedQuery

1. static query - named query
2. dynamic query

TypedQuery query = em.createQuery("SELECT e from EMPLOYEE e");
List emps = em.getResultList();

<persistence-unit name="EmployeeService"
transaction-type="RESOURCE_LOCAL">
RESOURCE_LOCAL means that we are using resource-level EntityTransaction
isntead of JTA.

---------------
calculat ca imi ia 22 de zile sa citesc cartea (hai sa vedem daca reusim in 5)
---------------

Session beans - encapsulate business services
have a business interface
a client gets a session bean and starts a session with that bean

3 types - stateless, stateful and singleton
business operations on a stateful session bean can maintain state on the bean between calls. This is not true for a stateless session bean.
singleton - just one

@Stateless annotation

To emphasize that an interface is a local business interface @Local annotation
can be put on the interface.

Local business interface means that it is accessible to clients only within the same application server.

You can also define it with no interface. In this case the local business interface will be considered the public methods from that class.

For
stateless session beans, there are two lifecycle callbacks: PostConstruct and PreDestroy.

The server will invoke the PostConstruct callback as soon as it has completed initializing all the container services for the bean. In effect, this replaces the constructor as the location for initialization logic because it is only here that container services are guaranteed to be available.

@Remote
public interface HelloServiceRemote {
public String sayHello(String name);
}

@Stateful
public class ShoppingCartBean implements ShoppingCart {

@Remove
public void checkout(int paymentId) {
// store items to database
// ...
}
}

Passivation is the process by which
the server serializes the bean instance so that it can either be stored offline to free up resources or
replicated to another server in a cluster. Activation is the process of deserializing a passivated session
bean instance and making it active in the server once again.

@PrePassivate
public void passivate() { releaseConnection(); }
@PostActivate
public void activate() { acquireConnection(); }

Unlike other session beans, the singleton can be created eagerly during application initialization and exist until the application shuts down.

@Singleton
public class HitCounter {
int count;
public void increment() { ++count; }
public void getCount() { return count; }
public void reset() { count = 0; }
}

The container determines the point when the singleton instance gets created unless the bean includes the @Startup annotation to force eager initialization when the application starts.

When multiple singleton session beans depend on one another, the container needs to be informed of the order in which they should be instantiated. This is accomplished via the @DependsOn annotation on the bean class, which lists the names of other singleton session beans that must be created first.

@Singleton
public class HitCounter {
int count;
public void increment() { ++count; }
@Lock(LockType.READ)
public void getCount() { return count; }
public void reset() { count = 0; }
}

For those who wish to have fine-grained control over concurrency, the singleton session bean can be configured to use bean-managed concurrency via the
@ConcurrencyManagement(ConcurrencyManagementType.BEAN) annotation on the bean class.

@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="messageSelector",
propertyValue="RECIPIENT='ReportProcessor'")
})
public class ReportProcessorBean implements javax.jms.MessageListener {
public void onMessage(javax.jms.Message message) {
// ...

Wednesday, June 29, 2011

JPA2

Objects that leave the persistence layer are called detached objects

The set of managed entity instances within an entity manager at any
given time is called its persistence context.


There is an one-to-one correspondence between an EntityManagerFactory and a persistence unit.


----------------------------

correspondence timeunit ?

----------------------------

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EmployeeService");

1 to 1 EntityManagerFactory and persistence unit