-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate + EntityBean
PostPosted: Fri Oct 03, 2003 9:23 am 
Newbie

Joined: Fri Sep 26, 2003 8:31 am
Posts: 3
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();
}
}
}


Top
 Profile  
 
 Post subject: Same issue
PostPosted: Tue Jan 03, 2006 1:04 pm 
Newbie

Joined: Tue Nov 02, 2004 11:59 am
Posts: 2
I am facing exactly the same problem as thomasleung, and I've code similar to his. Does anyone have a solution for this? Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 1:16 pm 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
can you post the mappings ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.