-->
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.  [ 2 posts ] 
Author Message
 Post subject: NullPointerException on one-to-many/many-to-one relations
PostPosted: Thu May 27, 2004 3:22 pm 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
Hi, i`ve posted scattered pieces of the problem i`m having.

As i didn`t get solving answers and i didn`t post all the code, I`m trying this time (with all the code). Also i`m a little desperate (i`ve been trying to make this work for days...)

Here's is part of my session bean wich access the classes.

Code:
System.out.println("All employess of org 10...");   
List emps = session.find("from Employee as e where e.org = 10");Iterator iter = emps.iterator();   
while (iter.hasNext()){   
    Employee empOrg10 = (Employee)iter.next();   
    System.out.println("Employee is: " + empOrg10.getLastName());
}

// !!!! My code works fine till here
Organization organization1 = (Organization)session.load(Organization.class, new Integer(11));   
// !!!! At this point, the attributes orgName, orgAddress and orgId have been loaded,   
// !!!! but employees have not (even when i'm not using lazy load )!!!!
Set org10Employees = organization1.getEmployees();   
Iterator it = org10Employees.iterator();   
// !!!! Here i get a NullPointer Exception, as i try to iterate on something null !!!!   
while (it.hasNext()){      
   Employee empOrg10 = (Employee)it.next();   
   System.out.println("Employee is: " + empOrg10.getLastName());
}




Also, here are my classes...
Organization

Code:
/**
* @author Leandro

* @hibernate.class
*  table="Organization"
*
*/
public class Organization {      
private Integer orgId;   
private String orgName;   
private String orgAddress;   
private Set employees;    

public Organization(){   }
   
public Organization(Integer id, String name, String add){      this.orgId = id;      
                this.orgName = name;      
                this.orgAddress = add;   
}                   

// i also tried inserting table="Employee" under
//@hibenate.set tag    
/**   
*    
* @hibernate.set   
*  lazy="false"   
*  inverse="true"   
*     
* @hibernate.collection-key   
*  column="fk_orgId"   
* @hibernate.collection-one-to-many   
*  class="hibernate.Employee"   
*    
* @return all the employees   
*    
*/   
public Set getEmployees() {      
   return employees;   
}
   
/**   
*    
* @hibernate.property   
*  column="orgAddress"   
*    
* @return   
*    
*/   
public String getOrgAddress() {      
  return orgAddress;   
}    

/**   
*    
* @hibernate.id   
*  generator-class="native"   
*  column="orgId"   
*    
* @return   
*/   
public Integer getOrgId() {      
  return orgId;   
}    

/**   
*    
* @hibernate.property   
*  column="orgName"   
*    
* @return   
*/   
public String getOrgName() {      
  return orgName;   
}    

/**   
* @param set   
*/   
public void setEmployees(Set emps) {      
  employees = emps;   
}
   
/**   
* @param string   
*/   
public void setOrgAddress(String string) {      
  orgAddress = string;   
}    

/**   
* @param integer   
*/   
public void setOrgId(Integer integer) {      
orgId = integer;   
}    

/**   
* @param string   
*/   
public void setOrgName(String string) {      
  orgName = string;   
}
}




Employee

Code:
public class Employee {      
  private Integer empId;   
  private String firstName;   
  private String lastName;   
  private Organization org;    

  public Employee(){   }         

  public Employee(Integer id, String fn, String ln, Organization fk){   
         this.empId = id;      
         this.firstName = fn;      
         this.lastName = ln;      
         this.org = fk;   
}     

/**   
*    
* @hibernate.id   
*  generator-class="native"   
*  column="empId"   
*    
* @return the id   
*   
*/   
public Integer getEmpId() {      
  return empId;   
}    

/**   
*    
* @hibernate.property   
*  column="firstName"   
*    
* @return   
*    
*/   
public String getFirstName() {      
  return firstName;   
}    

/**   
*    
* @hibernate.many-to-one   
*  column="fk_orgId"   
*    
* @return the organization of the employee   
*    
*/   
public Organization getOrg() {      
  return org;   
}    

/**   
*    
* @hibernate.property   
*  column="lastName"   
*    
* @return   
*    
*/   
public String getLastName() {      
   return lastName;   
}    

/**   
* @param integer   
*/   
public void setEmpId(Integer integer) {      
  empId = integer;   
}    

/**   
* @param string   
*/   
public void setFirstName(String string) {      
  firstName = string;   
}    

/**   
* @param organization   
*/   
public void setOrg(Organization fk_orgId) {      
  this.org = fk_orgId;   
}    

/**   
* @param string   
*/   
public void setLastName(String string) {      
lastName = string;   
}
}




My mapping files have already been posted, but as i have made some minor modifications, I'll post them again, ok.

Organization.hbm.xml

Code:

<hibernate-mapping>   
  <class       
  name="hibernate.Organization"       
  table="Organization"       
  dynamic-update="false"       
  dynamic-insert="false"    >         

    <id           
      name="orgId"           
      column="orgId"           
      type="java.lang.Integer"        >           

      <generator class="native">           
      </generator>       

    </id>         


    <set           
      name="employees"           
      table="Employee"           
      lazy="false"     
      inverse="true"       
      cascade="none"       
      sort="unsorted"        >

              <key   
                column="fk_orgId"              />

              <one-to-many                 
                class="hibernate.Employee"              />   
   
   </set>         

  <property            name="orgAddress"             
     type="java.lang.String"            update="true"           
     insert="true"            column="orgAddress"        />   

  <property            name="orgName"           
    type="java.lang.String"            update="true"           
    insert="true"            column="orgName"        />         


</class>
</hibernate-mapping>




Employee.hbm.xml

Code:
<hibernate-mapping>   
<class       
  name="hibernate.Employee"       
  table="Employee"       
  dynamic-update="false"     
  dynamic-insert="false"    >   

     <id     
      name="empId"     
      column="empId"   
      type="java.lang.Integer"        > 

          <generator class="native"> 
          </generator>   

     </id>       

    <property       
     name="firstName"   
      type="java.lang.String"   
      update="true"     
      insert="true"     
      column="firstName"        /> 

    <many-to-one     
       name="org"     
       class="hibernate.Organization"   
       cascade="none"       
       outer-join="auto"       
      update="true"   
       insert="true"     
      column="fk_orgId"        />     

    <property     
      name="lastName"       
      type="java.lang.String"     
      update="true"     
      insert="true"     
      column="lastName"        />     

</class>
</hibernate-mapping>




Here's the exception.

11:30:03,468 ERROR [LogInterceptor] RuntimeException:
java.lang.NullPointerException
at session.TestBean.executaTudo1(TestBean.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:683)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:331)
at org.jboss.ejb.Container.invoke(Container.java:700)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:367)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:534)


Thank you so much,
ltcmelo


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 8:27 am 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
Hi,
sorry, for my my insenstence, but hasn`t anyone seen a problem with my code?

It just doesn`t work, i`ve read the Hibernate Reference and other documents, and it seems i`m doing everthing right, but


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.