-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: java.lang.ClassCastException
PostPosted: Sun Jan 14, 2007 9:44 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
i gt java.lang.ClassCastException at the method as below:

Code:
public static MUser deleteUser(Session session, MUser user) throws InfrastructureException{
      Session ses = session;
      
      if(ses == null){
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          ses =sessionFactory.openSession();
      }
      
      try{
         ses.delete(user);
         List role = new ArrayList();
         role = user.getUserRoles();
         Iterator ite = role.iterator();
         while (ite.hasNext()) {
            MUserRoles roles = (MUserRoles) ite.next();
            ses.delete(roles);
         }
         ses.flush();
         
         return user;
      }catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }finally{
         if(session == null){
            ses.beginTransaction().commit();
             ses.close();
         }
      }
   }   


can anyone tell me what happened and how to solve it?? thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 14, 2007 11:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Where's the exception? What line? Casting from what to what?

The only obvious candidate is the line
faireni wrote:
MUserRoles roles = (MUserRoles) ite.next();

Maybe you should check that it's an MUserRoles?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 1:42 am 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
Actually i called this 'deleteUser' method as below:
Code:
Object[] args = new Object[]{part};
Class[] argsType = new Class[]{MUser.class};
EJBInvoker.invokeRemoteEJB(Global.JBOSS_JNP,SetUpBeanHome.JNDI_NAME,"deleteUser",args,argsType);



the error message is as below:
2007-01-15 13:44:00,409 ERROR [org.jboss.ejb.plugins.LogInterceptor] RuntimeException in method: public abstract my.com.shinyang.ilms.model.MUser my.com.shinyang.ilms.service.SetUpBeanRemote.deleteUser(my.com.shinyang.ilms.model.MUser) throws my.com.shinyang.ejb.ServiceException,java.rmi.RemoteException:
java.lang.ClassCastException
at my.com.shinyang.ilms.service.SetUpDAO.deleteUser(SetUpDAO.java:1942)
at my.com.shinyang.ilms.service.SetUpBean.deleteUser(SetUpBean.java:2083)
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.invocation.Invocation.performCall(Invocation.java:345)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:214)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:130)
at org.jboss.webservice.server.ServiceEndpointInterceptor.invoke(ServiceEndpointInterceptor.java:51)
at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:48)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:105)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:166)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:139)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:624)
at org.jboss.ejb.Container.invoke(Container.java:873)
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.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
at org.jboss.invocation.jrmp.server.JRMPInvoker$MBeanServerAction.invoke(JRMPInvoker.java:805)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:406)
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)

and MUser.java is as below:

Code:
public class MUser implements Serializable, TableData{
   
   public String userId;
   public String password;
   public String fullName;
   public String designation;
   public List userRoles = new ArrayList();
   
   /** default constructor */
    public MUser() {
    }
   
    public MUser(String userId, String password, String fullName, String designation) {
        this.userId = userId;
        this.password = password;
        this.fullName = fullName;
        this.designation = designation;
    }

    /** full constructor */
    public MUser(String userId, String password, String fullName, String designation, List v) {
        this.userId = userId;
        this.password = password;
        this.fullName = fullName;
        this.designation = designation;
        this.userRoles = v;
    }
   

   public String getDesignation() {
      return designation;
   }
   public void setDesignation(String designation) {
      this.designation = designation;
   }
   public String getFullName() {
      return fullName;
   }
   public void setFullName(String fullName) {
      this.fullName = fullName;
   }
   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
   public String getUserId() {
      return userId;
   }
   public void setUserId(String userId) {
      this.userId = userId;
   }
   public List getUserRoles() {
      return this.userRoles;
   }
   public void setUserRoles(List v) {
      this.userRoles = v;
   }
   
    public void addUserRoles(MUserRoles v){
           v.setUser(this);
           this.userRoles.add(v);
       }
      
}

can help?? thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 4:12 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
What is line 1942 of SetupDAO.java? Usually it's best to include this sort of information when first asking the question.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 8:20 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
sorry for unclear info: Actually line 1942 of SetupDAO.java is as shown below:

Code:
MUserRoles roles = (MUserRoles) ite.next();


i still cant figure out what's wrong. hope to hear from u. thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 9:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Obivously user.getUserRoles() isn't returning a list of MUserRoles. Is that loaded via Hibernate? If it is, post the mapping for MUser and MUserRoles.

By the way, you shouldn't delete user before calling user.getUserRoles(). It might work, if you have already populated user.getUserRoles(), but it's not guaranteed. Delete the user after you have deleted its roles.

Usually, cascade-deleting is the way to go for that.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 9:50 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
ya.. i think it's loaded via hibernate coz i have mapped them together.

MUser.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="my.com.shinyang.ilms.model.MUser" table="m_user">
        <id name="userId" type="string">
            <column name="USER_ID" length="100" />
            <generator class="assigned" />
        </id>
        <property name="password" type="string">
            <column name="PASSWORD" length="100" />
        </property>
        <property name="fullName" type="string">
            <column name="FULL_NAME" length="100" />
        </property>
        <property name="designation" type="string">
            <column name="DESIGNATION" length="100" />
        </property>
        <list
            name="userRoles"
            lazy="false"
            inverse="true"
            cascade="none"
        >
            <key
                column="USER_ID"
            >
            </key>

            <index
                column="NO"
                type="integer"
            />

            <one-to-many
                  class="my.com.shinyang.ilms.model.MUserRoles"
            />

        </list>
    </class>

MUserRoles.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="my.com.shinyang.ilms.model.MUserRoles" table="m_user_roles">
        <id name="id" type="long">
            <column name="ID" not-null="true" />
            <generator class="native" />
        </id>
        <property name="no" type="integer" update="true" insert="true">
            <column name="NO" />
        </property>
        <many-to-one name="user" class="my.com.shinyang.ilms.model.MUser" lazy="false"  update="true"
            insert="true" cascade="none">
            <column name="USER_ID" />
        </many-to-one>
        <property name="roleId" type="string">
            <column name="ROLE_ID" length="100" />
        </property>
       
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 10:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That mapping can't be correct, can it? You have the NO column in table m_user_roles mapped as insert twice: once in the <list> as its <index>, and once in the mapping for MUserRoles. Are you sure that you're using the mappings you've posted? You should change the property mapping for MUserRoles.no to be insert="false" update="false", as the <index> is the correct way to modify that column.

First things first, though: why don't you change your deleteUser code to not delete roles, and instead use cascade="all-delete-orphan" to delete the roles. That's the simple and efficient way of doing things.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 10:10 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
sorry coz i just simply added in 'insert = 'true' and update = 'true' coz i think it won't have big effect. but seems like i'm wrong. :-)

MuserRoles.hbm.xml:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="my.com.shinyang.ilms.model.MUserRoles" table="m_user_roles">
        <id name="id" type="long">
            <column name="ID" not-null="true" />
            <generator class="native" />
        </id>
        <property name="no" type="integer" update="true" insert="true">
            <column name="NO" />
        </property>
        <many-to-one name="user" class="my.com.shinyang.ilms.model.MUser" lazy="false" cascade="none">
            <column name="USER_ID" />
        </many-to-one>
        <property name="roleId" type="string">
            <column name="ROLE_ID" length="100" />
        </property>
       
    </class>
</hibernate-mapping>


is that u mean that using '' all-delete-orphan" will be on-shot-deleting??
anyway, why i still get the java.lang.ClassCastException?? :-(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 10:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
First, move the ses.delete(user) to after the MUserRoles delete: that may be causing the problem (who knows what's in the child CGLib object after the parent object has been removed from the session). Your problem may just go away.

If it doesn't, then have a look at the actual object being returned, that should help you figure out why you're getting the wrong object.
Code:
while (ite.hasNext()) {
  Object next = ite.next();
  System.out.println("Loaded an object of type " + ite.getClass().getSimpleName();
  MUserRoles roles = (MUserRoles) next;
  ses.delete(roles);
}

You could also consider upgrading to java 1.5 or 1.6 and using generics.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 10:40 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
oh no, i didn't see anything print out after i use System.out.printlnn().
i was stucked. i can't upgrade to java 1.5 or 1.6 coz most of my codings will take time to be modified.
pls help. . :-( thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 10:48 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're not seeing anything, then you're doing somehting wrong, because you should at least see "Loaded an object of type ". It may be because you're using remote invocation, but you can resolve that bit yourself (use logging or a popup window, or output to a file, whatever it takes to add some debug output).

If all you see is the debug message without a class name, then you've got a very very weird object. That might be because you've removed the user from the session prematurely. Have you moved the ses.delete(user) line to lower down in the method?

Please give more information on each step as you do it. I can't assume that you're implementing each of my suggestions, and I don't know how far you've gotten in helping yourself fix your problem.

You can always upgrade to 1.5 and 1.6. They're 100% backwards compatible to 1.4. You'll just get loads of warnings, and you can turn those off at the command line. And if you add the 1.5 generics for this problem, you should get a compiler error at the place where your code is broken.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 11:54 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
sorry. Actually the print out is as below:

2007-01-16 11:54:36,243 INFO [STDOUT] Loaded an object of type java.util.AbstractList$Itr

what does this mean??/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 12:15 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That means that you used my typo directly, instead of figuring out that I'd made a mistake. Change the line
Code:
System.out.println("Loaded an object of type " + ite.getClass().getSimpleName());
to
Code:
System.out.println("Loaded an object of type " + next.getClass().getSimpleName());

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 1:48 am 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
sorry coz i didn't notice ur typo.
and the print out is as below:

Code:
2007-01-16 13:37:24,929 INFO  [STDOUT] Loaded an object of type my.com.shinyang.ilms.mode.MRoles


anyway i didn't map MUser with MRoles. what happen actually?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.