Results 1 to 5 of 5
  1. #1
    johnwcv is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Problems casting with the same class

    Hi.

    First of all, I am using JPA.
    My code is:
    List<Object> list1 = query.getResultList();
    System.out.println("list1.get(0).getClass(): "+list1.get(0).getClass());
    System.out.println("TbInfoPersonaJuridica.class: "+TbInfoPersonaJuridica.class);
    TbInfoPersonaJuridica obj = (TbInfoPersonaJuridica)list1.get(0);


    But, I get the next exception:

    INFO: list1.get(0).getClass(): class co.com.colpatria.crm.dao.TbInfoPersonaJuridica
    INFO: TbInfoPersonaJuridica.class: class co.com.colpatria.crm.dao.TbInfoPersonaJuridica
    GRAVE: find all failed
    java.lang.ClassCastException: co.com.colpatria.crm.dao.TbInfoPersonaJuridica cannot be cast to co.com.colpatria.crm.dao.TbInfoPersonaJuridica


    I am totally sure that I have only a TbInfoPersonaJuridica class.

    Can anyone help me?

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    It might be that the object you are retrieving from the server is a different version of
    co.com.colpatria.crm.dao.TbInfoPersonaJuridica
    than your client side.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Maybe the classes were loaded by different class loaders (ClassLoader). Given the Class object you can check that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    johnwcv is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Maybe the classes were loaded by different class loaders (ClassLoader). Given the Class object you can check that.

    kind regards,

    Jos
    Apparently, this is the reason.

    With this:
    System.out.println(list1.get(0).getClass().getClas sLoader().equals(TbInfoPersonaJuridica.class.getCl assLoader()))

    I got false.

    How could I guarantee or force that both use the same ClassLoader?

  5. #5
    johnwcv is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default More information

    The error is:
    Java Code:
    java.lang.IllegalArgumentException: Object: co.com.colpatria.crm.dao.TbInfoPersonaNatural@1a72585 is not a known entity type.
    	at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3212)
    	at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:205)
    	at co.com.colpatria.crm.dao.TbInfoPersonaNaturalDAO.save(TbInfoPersonaNaturalDAO.java:99)
    .....
    The persistence.xml file is:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence 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" version="1.0">
        
    	<persistence-unit name="ClientesErradosCRMPU"
    		transaction-type="RESOURCE_LOCAL">
    		<provider>
    			oracle.toplink.essentials.PersistenceProvider
    		</provider>
    		<class>co.com.colpatria.crm.dao.TbInfoPersonaNatural</class>
    		<class>co.com.colpatria.crm.dao.TbInfoPersonaJuridica</class>
    		<properties>
    ...
    		</properties>
    	</persistence-unit>
    
    </persistence>
    Finally, with both clases (TbInfoPersonaNatural and TbInfoPersonaJuridica) I got the same error.

    TbInfoPersonaNatural:
    Java Code:
    @Entity
    @Table(name = "TB_INFO_PERSONA_NATURAL", schema = "METRICAS_ORIGINACION")
    public class TbInfoPersonaNatural implements java.io.Serializable {
    ...
    TbInfoPersonaJuridica:
    Java Code:
    @Entity
    @Table(name = "TB_INFO_PERSONA_JURIDICA", schema = "METRICAS_ORIGINACION")
    public class TbInfoPersonaJuridica implements java.io.Serializable {
    ...

Similar Threads

  1. class loader problems!
    By alacn in forum New To Java
    Replies: 6
    Last Post: 08-11-2010, 10:51 PM
  2. Class.forName and casting
    By martinmarinov in forum Advanced Java
    Replies: 16
    Last Post: 06-09-2010, 02:42 PM
  3. Casting a child class into a parent class.
    By Unsub in forum New To Java
    Replies: 7
    Last Post: 01-30-2010, 01:39 AM
  4. Inner class problems
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-13-2008, 08:56 AM
  5. class casting
    By vital parsley in forum New To Java
    Replies: 1
    Last Post: 07-24-2008, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •