[EJB 3.0 Users] - How to find Entity by Id
by Fuchs
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
16 years, 7 months
[EJB 3.0 Users] - Re: JBoss 4.2.1 RemoteEJB Lookup ClassCastException
by jaikiran
anonymous wrote : com.thesearchagency.mms.service.user.UserException: Non matching type for inject of field: com.these
| archagency.mms.service.customization.ICustomizationRemote com.thesearchagency.mms.service.user.impl.
| UserEJB.theCustomizationService for type: $Proxy220 of jndiName env/com.thesearchagency.mms.service.
| user.impl.UserEJB/theCustomizationService
| intfs: , com.thesearchagency.mms.service.customization.ICustomizationRemote, org.jboss.ejb3.JBossPro
| xy, javax.ejb.EJBObject; nested exception is:
| java.lang.RuntimeException: Non matching type for inject of field: com.thesearchagency.mms.service.
| customization.ICustomizationRemote com.thesearchagency.mms.service.user.impl.UserEJB.theCustomizatio
| nService for type: $Proxy220 of jndiName env/com.thesearchagency.mms.service.user.impl.UserEJB/theCu
| stomizationService
| intfs: , com.thesearchagency.mms.service.customization.ICustomizationRemote, org.jboss.ejb3.JBossPro
| xy, javax.ejb.EJBObject
|
As suspected, it's the similar to what we fixed as part of EJBTHREE-1889 for AS-5.
anonymous wrote :
| Also if this is a bug is there any way to patch it in a JBoss 4.2.1.GA environment?
The 4.x community branch is way too old and is no longer being developed. The code too is vastly different from the current state. The possible options are:
1) Upgrade to latest 5.1.0 AS and you start using the EJB3 plugins
2) Checkout AS 4.x branch from source and then you will have to manually create the patch and build the AS.
3) If you have a support contract with the enterprise edition of AS (the EAP version), you can always file a ticket and the issue will be fixed in the enterprise edition branch and will be made available to you.
anonymous wrote :
| So I am assuming this is a bug in JBoss 4.2.1 and Remote EJB calls from a client are not possible... ?
Remote calls are possible. The problem you are running into is that you have isolated your deployments and are packaging the EJB interfaces in each deployment (a valid case, btw). The proxy being bound is JNDI uses a different classloader (corresponding to deployment A) and the injection/lookup happens using a different classloader (corresponding to deployment B) and hence the classcast exception.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4265533#4265533
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4265533
16 years, 7 months