I'm trying to integrate the Hibernate with the EntityBean. I'm success to insert the new record into the PostgreSQL database. But the EntityBean reports the NullPointerException when I call the findByPrimaryKey(Integer) method. Does any expert can help me to take a look?
The error is occurs in "System.out.println(result.getId());" located in AddressTest.java. In the JBoss console, it reports the NullPointerException at line "address = (pojo.Address)session.load(pojo.Address.class, primaryKey);", located in selectByPrimaryKey(Integer) method.
Thanks a lot.
Thomas
AddressBean.java
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.naming.Context;
import javax.naming.InitialContext;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
/**
* @author thomas
*/
public class AddressBean implements EntityBean {
private pojo.Address address = null;
private EntityContext context = null;
private SessionFactory factory = null;
// EJB methods
public void ejbActivate() {
}
public Integer ejbCreate(String flat, String floor, String block, String building,
String estate, String street) throws CreateException {
try {
Session session = factory.openSession();
address = new pojo.Address();
address.setFlat(flat);
address.setFloor(floor);
address.setBlock(block);
address.setBuilding(building);
address.setEstate(estate);
address.setStreet(street);
session.save(address);
session.flush();
session.close();
} catch (HibernateException exception) {
throw new CreateException(exception.getMessage());
}
return new Integer(address.getId());
}
public Integer ejbFindByPrimaryKey(Integer id) throws FinderException {
boolean result;
try {
result = this.selectByPrimaryKey(id);
} catch (HibernateException exception) {
throw new EJBException(exception.getMessage());
}
if (result)
return id;
else
throw new ObjectNotFoundException("NOT FOUND");
}
public void ejbLoad() {
}
public void ejbPassivate() {
}
public void ejbPostCreate(String flat, String floor, String block, String building,
String estate, String street, String district, String country) {
}
public void ejbRemove() {
try {
Session session = factory.openSession();
session.delete(address);
session.flush();
session.close();
} catch (HibernateException exception) {
}
}
public void ejbStore() {
try {
Session session = factory.openSession();
session.update(address);
session.close();
} catch (HibernateException exception) {
}
}
public void setEntityContext(EntityContext context) {
this.context = context;
try {
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp/env");
factory = (SessionFactory)ic.lookup("java:/hibernate/HibernateFactory");
} catch (Exception exception) {
throw new EJBException(exception);
}
}
public void unsetEntityContext() {
try {
factory.close();
} catch (Exception exception) {
throw new EJBException(exception);
}
}
// Database methods
private boolean selectByPrimaryKey(Integer primaryKey) throws HibernateException {
Session session = factory.openSession();
address = (pojo.Address)session.load(pojo.Address.class, primaryKey);
session.close();
if (address != null) {
return true;
} else {
return false;
}
}
// Business methods
/**
* @return
*/
public String getBlock() {
return address.getBlock();
}
/**
* @return
*/
public String getBuilding() {
return address.getBuilding();
}
/**
* @return
*/
public String getEstate() {
return address.getEstate();
}
/**
* @return
*/
public String getFlat() {
return address.getFlat();
}
/**
* @return
*/
public String getFloor() {
return address.getFloor();
}
/**
* @return
*/
public int getId() {
return address.getId();
}
/**
* @return
*/
public String getStreet() {
return address.getStreet();
}
/**
* @param block
*/
public void setBlock(String block) {
address.setBlock(block);
}
/**
* @param building
*/
public void setBuilding(String building) {
address.setBuilding(building);
}
/**
* @param estate
*/
public void setEstate(String estate) {
address.setEstate(estate);
}
/**
* @param flat
*/
public void setFlat(String flat) {
address.setFlat(flat);
}
/**
* @param floor
*/
public void setFloor(String floor) {
address.setFloor(floor);
}
/**
* @param id
*/
public void setId(int id) {
address.setId(id);
}
/**
* @param street
*/
public void setStreet(String street) {
address.setStreet(street);
}
}
AddressTest.java
import com.quamdata.address.ejb.Address;
import com.quamdata.address.ejb.AddressHome;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class AddressTest {
public static void main(String[] args) {
try {
InitialContext context = new InitialContext();
Object object = context.lookup("ejb/Address");
AddressHome home = (AddressHome)PortableRemoteObject.narrow(object, AddressHome.class);
Address address = home.create("1","2","C","MOON BUILDING","SUN ESTATE","MILKWAY STREET");
Address result = home.findByPrimaryKey(new Integer(1));
System.out.println(result.getId());
System.exit(0);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
|