[jboss-user] [EJB 3.0 Users] - How to find Entity by Id

Fuchs do-not-reply at jboss.com
Fri Nov 13 10:08:22 EST 2009


Hello, 
i am using jboss portal 2.7.2 bundle with jboss 4.2.3 and i like to use ejb3 persistence for database query.
But i get this exception:
15:45:31,530 ERROR [CustomerHome] get failed
  | java.lang.NullPointerException
  | 	at swk.hibernate.dao.CustomerHome.findById(Unknown Source)
  | 	at de.counter.CounterBean.<init>(CounterBean.java:69)

My datasource:
<datasources>
  |   <local-tx-datasource>
  | 	  <jndi-name>HelloWorldDS</jndi-name>
  | 	  <connection-url>jdbc:mysql://xxxx/xxxx</connection-url>
  | 	  <driver-class>com.mysql.jdbc.Driver</driver-class>
  | 	  <user-name>xxxx</user-name>
  | 	  <password>xxxx</password>
  | 	  <min-pool-size>5</min-pool-size>
  | 	  <max-pool-size>20</max-pool-size>
  |   </local-tx-datasource>
  | </datasources>
persistence.xml:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  | 	<persistence-unit name="customerdb">
  | 		<provider>org.hibernate.ejb.HibernatePersistence</provider>
  | 		<jta-data-source>java:/HelloWorldDS</jta-data-source>
  | 		<properties>
  | 			<property name="hibernate.archive.autodetection" value="class, hbm"></property>
  | 			<property name="hibernate.show_sql" value="true"/>
  | 			<property name="hibernate.format_sql" value="true"/>
  | 			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  | 		</properties>
  | 	</persistence-unit>
  | </persistence>
  | 
and the entity with Stateless Home Class:
CustomerEntity
  | @Entity
  | @Table(name = "Customer", catalog = "swkcustomerportal")
  | public class Customer implements java.io.Serializable {
  | 
  | 	private Integer idCustomer;
  | 	private String jbossUser;
  |         ...
  | 
  | 	public Customer() {
  | 	}
  | 
  | 	public Customer(String jbossUser, ...) {
  | 		this.jbossUser = jbossUser;
  |                 ...
  | 	}
  | 
  | 	public Customer(String jbossUser, ....)
  |         ...
  | 	}
  | 
  | 	@Id
  | 	@GeneratedValue(strategy = IDENTITY)
  | 	@Column(name = "idCustomer", unique = true, nullable = false)
  | 	public Integer getIdCustomer() {
  | 		return this.idCustomer;
  | 	}
  | 
  | 	protected void setIdCustomer(Integer idCustomer) {
  | 		this.idCustomer = idCustomer;
  | 	}
  |         ...
  | 
  | CustomerHomeStateless
  | @Stateless
  | public class CustomerHome {
  | 
  | 	private static final Log log = LogFactory.getLog(CustomerHome.class);
  | 
  | 	@PersistenceContext(unitName="customerdb") 
  | 	private EntityManager entityManager;
  | 
  | 	public void persist(Customer transientInstance) {
  |         ...
  | 	}
  | 
  | 	public void remove(Customer persistentInstance) {
  |         ...
  | 	}
  | 
  | 	public Customer merge(Customer detachedInstance) {
  |         ...
  | 	}
  | 
  | 	public Customer findById(Integer id) {
  | 		log.debug("getting Customer instance with id: " + id);
  | 		try {
  | 			
  | 			Customer instance = (Customer)entityManager.find(Customer.class, id);
  | 			log.debug("get successful");
  | 			return instance;
  | 		} catch (RuntimeException re) {
  | 			log.error("get failed", re);
  | 			throw re;
  | 		}
  | 	}
  | }
I shorted the Classes to give an easier overview.
The Customer and the CustomerHome are in different packages of the same jar File. I thought that it is no problem.

Jboss Console gives me the following information:

  | 15:28:33,054 INFO  [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.entity.PersistenceUnitDeployment
  | 15:28:33,070 INFO  [JmxKernelAbstraction] installing MBean: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb with dependencies:
  | 15:28:33,070 INFO  [JmxKernelAbstraction] 	jboss.jca:name=HelloWorldDS,service=DataSourceBinding
  | 15:28:33,086 INFO  [PersistenceUnitDeployment] Starting persistence unit persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
  | 15:28:33,164 INFO  [Version] Hibernate EntityManager 3.2.1.GA
  | 15:28:33,164 INFO  [Version] Hibernate Annotations 3.2.1.GA
  | 15:28:33,351 INFO  [Ejb3Configuration] found EJB3 Entity bean: swk.hibernate.model.Customer
  | ...
  | 15:28:34,773 INFO  [HbmBinder] Mapping class: swk.hibernate.model.Customer -> Customer
  | ...
  | 15:28:36,273 INFO  [SessionFactoryObjectFactory] Factory name: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
  | 15:28:36,273 INFO  [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
  | 15:28:36,289 INFO  [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
  | 15:28:36,289 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext
  | 15:28:36,289 INFO  [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
  | 15:28:36,742 INFO  [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.StatelessContainer
  | 15:28:37,039 INFO  [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=HibernateCustomer.jar,name=CustomerHome,service=EJB3 with dependencies:
  | 15:28:37,039 INFO  [JmxKernelAbstraction] 	persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
  | 15:28:37,055 INFO  [EJBContainer] STARTED EJB: swk.hibernate.dao.CustomerHome ejbName: CustomerHome
  | 15:28:37,055 INFO  [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.StatelessContainer
  | ...
  | 15:28:37,289 INFO  [EJB3Deployer] Deployed: file:/D:/server/jboss-portal-2.7.2/server/default/deploy/HibernateCustomer.jar

My portlet does the following:
	Customer customers;
  |         ...
  | 	
  | 	public CounterBean() {
  |         ...
  | 		try {
  | 			customers = (Customer) new CustomerHome().findById(1);
  |                 ...

I must omit something, or I did not understand it correctly.
Can someone help me?


View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4265547#4265547

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4265547



More information about the jboss-user mailing list