21 septiembre 2009

24.2.1. Using the Hibernate API

24.2.1. Using the Hibernate API

To use the Hibernate API to manage the database objects, we inject a Hibernate Session in-
stead of an EntityManager into the ManagerPojo class. The API methods in the Hibernate
Session is roughly equivalent to methods in the EntityManager; they have only slightly dif-
ferent method names. This is the Hibernate version of the ManagerPojo class:

@Name("manager")
@Scope (APPLICATION)
public class ManagerPojo {
@In (required=false) @Out (required=false)
private Person person;
@In (create=true)
private Session helloSession;
Long pid;
@DataModel
private List fans;
@DataModelSelection
private Person selectedFan;
public String sayHello () {
helloSession.save (person);
return "fans";
}
@Factory("fans")
public void findFans () {
fans = helloSession.createQuery(
"select p from Person p")
.list();
}
public void setPid (Long pid) {
this.pid = pid;
if (pid != null) {
person = (Person)
helloSession.get(Person.class, pid);
} else {
person = new Person ();
}
}
public Long getPid () {
return pid;
}
public String delete () {
Person toDelete =
(Person) helloSession.merge (selectedFan);
helloSession.delete( toDelete );
findFans ();
return null;
}
public String update () {
return "fans";
}
}

No hay comentarios: