Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Object not mapped problem

  1. #1
    Join Date
    May 2005
    Posts
    26

    Default Object not mapped problem

    Hi,
    Firstly appolgoise for the long post!
    I am having trouble searching and writing a object to the database. I have mapped the object via the hbm.xml but when hibernate performs its query it sayes the object is not mapped.

    to explain further:

    I have a parent class cp which has 2 subclasses cplive and cptemp

    In the parebt object i have all the setters and getters and in the cplive i have couple of extra fields as in the temp. all mapped.

    My hbm.xml for the live is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- change reference to DTD -->
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- <!DOCTYPE hibernate-mapping SYSTEM
    "file:///C:/apps/hibernate-3.1/src/org/hibernate/hibernate-mapping-3.0.dtd"> -->
    <hibernate-mapping>
    <class name="com.my.valueobjs.CPPersistent" table="CP">
    <id type="java.lang.String">

    </id>
    <property name="cpId" column="CPID"/>
    <property name="clientName" column="CLIENTNAME" not-null="true"/>
    <property name="london" column="LONDON" not-null="true"/>
    <property name="newyork" column="NEWYORK" not-null="true"/>

    </class>
    </hibernate-mapping>

    The Id field in the mapping is generated by the user when he fills in the form from the web fornt end. Before i tried the above i had

    <id name="cpId" column="CPID"

    </id>


    In my code I do the following query when performing a search:

    public List findById(String Id, String table) {
    log.debug("entered find by Id: " + Id);
    HibernateTemplate hibTemp = getHibernateTemplate();
    List results = new ArrayList();
    if(table.equalsIgnoreCase("live"))
    {
    results = hibTemp.find("from CPPersistent as cp where cp.cpId = '" + Id + "'");

    }


    When I try to execute the following code I get the following exception:
    11:56:00,838 DEBUG TransactionSynchronizationManager:136 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@f 800db] for key [org.hibernate.impl.SessionFactoryImpl@b95f72] bound to thread [http-8080-Processor3]
    11:56:00,849 DEBUG HibernateTemplate:350 - Found thread-bound Session for HibernateTemplate
    11:56:00,869 DEBUG TransactionSynchronizationManager:136 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@f 800db] for key [org.hibernate.impl.SessionFactoryImpl@b95f72] bound to thread [http-8080-Processor3]
    11:56:00,879 DEBUG SessionImpl:1005 - find: from CPPersistent as cp where cp.cpId = 'dsfrwef'
    11:56:00,899 DEBUG QueryParameters:261 - named parameters: {}
    11:56:01,049 DEBUG QueryTranslatorImpl:234 - parse() - HQL: from CPPersistent as cp where cp.cpId = 'dsfrwef'

    11:56:01,079 DEBUG AST:250 - --- HQL AST ---
    \-[QUERY] 'query'
    +-[SELECT_FROM] 'SELECT_FROM'
    | \-[FROM] 'from'
    | \-[RANGE] 'RANGE'
    | +-[IDENT] 'CPPersistent'
    | \-[ALIAS] 'cp'
    \-[WHERE] 'where'
    \-[EQ] '='
    +-[DOT] '.'
    | +-[IDENT] 'cp'
    | \-[IDENT] 'cpd'
    \-[QUOTED_STRING] ''dsfrwef''

    11:56:01,089 DEBUG ErrorCounter:68 - throwQueryException() : no errors
    11:56:01,089 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]

    org.springframework.orm.hibernate3.HibernateQueryE xception: CPPersistent is not mapped. [from CPPersistent as cp where cp.cpId = 'dsfrwef']; nested exception is org.hibernate.hql.ast.QuerySyntaxException: CPPersistent is not mapped. [from CPPersistent as cp where cp.cpId = 'dsfrwef']

    I cant undersatnd why I get this object not mapped exception as earlier in my log it prints out the following

    11:53:55,628 DEBUG DTDEntityResolver:42 - trying to locate http://hibernate.sourceforge.net/hib...apping-3.0.dtd in classpath under org/hibernate/
    11:53:55,638 DEBUG DTDEntityResolver:56 - found http://hibernate.sourceforge.net/hib...apping-3.0.dtd in classpath
    11:53:55,859 INFO HbmBinder:266 - Mapping class: com.my.CPPersistent -> CP
    11:53:55,949 DEBUG HbmBinder:1179 - Mapped property: cpId -> CPID
    11:53:55,949 DEBUG HbmBinder:1179 - Mapped property: clientName -> CLIENTNAME
    11:53:55,949 DEBUG HbmBinder:1179 - Mapped property: london -> LONDON
    11:53:55,949 DEBUG HbmBinder:1179 - Mapped property: newyork -> NEWYORK
    11:53:55,949 DEBUG DTDEntityResolver:42 - trying to locate http://hibernate.sourceforge.net/hib...apping-3.0.dtd in classpath under org/hibernate/

    All help is greatly appreciated as I have tried a few things namely messing with the ID in the mapping and none have worked. I think it may be something to do with this but am not sure.

    thanks

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by Fez
    <hibernate-mapping>
    <class name="com.my.valueobjs.CPPersistent" table="CP">

    ...

    11:53:55,638 DEBUG DTDEntityResolver:56 - found http://hibernate.sourceforge.net/hib...apping-3.0.dtd in classpath
    11:53:55,859 INFO HbmBinder:266 - Mapping class: com.my.CPPersistent -> CP
    Maybe you have different classes named "CPPersistent" in different packages?

    Regards,
    Andreas

  3. #3
    Join Date
    May 2005
    Posts
    26

    Default

    Further to the above I amended my temp class so I could check if I could write into the database and suprisingly I was able to write a record into the DB. However then when I tried to perform a fin i.e from cpTemp where cpTemp.cpid = xxxxxx I got the same exception as before stating that the object was not mapped.

    i am even more confused now because if the object was not mapped how could i wirite into the DB.

  4. #4
    Join Date
    May 2005
    Posts
    26

    Default

    I wish the case was that I had different classes but no that is just a typo error...

  5. #5
    Join Date
    May 2005
    Posts
    26

    Default

    I fixed this problem by using the get method on the hibernate template but still dont understand why the find would not have worked!!

    Anyone?

  6. #6
    Join Date
    Mar 2006
    Posts
    1

    Default

    Hey, don't know if you came across a solution to this issue, however I ran into the same problem and was able to fix it by changing the name of my persistent class to match the name of the table. (In your case, changing CPPersistent to CP). Why this would be needed is beyond me, as the mapping file appears to allow tables and their persistent classes to have different names by using the class 'name' and 'table' attributes, as you did, and which I also had tried.

    Anyways, I don't particularly like the solution, but it did seem to work for me. If you were able to come up with a more satisfactory solution and/or explanation, it would be great if you could post it, I would be curious.

    Alright, since writing the above I found a better solution. Are you by any chance using Spring along with Hibernate? If so, are you using the hibernate3.jar provided with the Spring distribution? If so, download the actual Hibernate distribution, and use the hibernate3.jar that comes with that. See if that allows you to have tables/persistent classes with different names, along with potentially fixing a bunch of other issues you may have noticed.

    regards
    Last edited by jstern81; Mar 2nd, 2006 at 07:40 AM.

  7. #7
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    I fill basically reiterate what jstern81 - in the past I had problems with different hibernate.jars floating around and I'm always using their official site (sourceforge) to get the jars I'm interested in.
    See also their FAQ since I think you might have some class problems - either an old xerces/xalan jar version or multiple/incomplete hibernate jars.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  8. #8
    Join Date
    Apr 2006
    Posts
    1

    Default object not mapped problem

    Hi all,

    I m facing object not mapped problem:
    spring version: 1.2.6
    hibernate version: 3.1
    database: oracle 9i
    connection using datasource and jdbc pool weblogic 8.3

    scenario:
    I got 2 tables at 2 schema and i create 2 models in different package.

    1. TBL_A at SCHEMA: M,

    <hibernate-mapping package="org.models.m" >
    <class name="TA" schema="M" table="TBL_A">
    <id name="code" column="CD" />
    <property name="name" column="NAME" />
    </class>
    </hibernate-mapping>

    2. TBL_P at SCHEMA: S

    <hibernate-mapping package="org.models.s" >
    <class name="TP" schema="S" table="TBL_P">
    <id name="id" column="ID"/>
    <property name="programCode" column="PROGRAM_CD"/>
    <property name="targetedCode" column="TARGETED_CD"/>
    </class>
    </hibernate-mapping>

    my code at DAO is something like this:
    Query query = session.createQuery("the_query");
    query.setParameter("code_in", ta.getCode(), Hibernate.STRING);
    query.setParameter("name_in", ta.getName(), Hibernate.STRING);
    query.setParameter("programCode_in", ta.getProgramCode(), Hibernate.STRING);

    and the_query:
    "from TA as ta " +
    "where UPPER(ta.code) like UPPER(:code_in) " +
    "and UPPER(ta.name) like UPPER(:name_in) " +
    "and unitarea.code not in " +
    "( select tp.targetedCode from TP as tp" +
    " tp.programCode= rogramCode_in" + " ) " +
    "order by ta.code ";

    I got error :
    Exception encountered, e = org.hibernate.hql.ast.QuerySyntaxException: TP is not mapped.

    any clues, please help

    Regards,
    Johan.
    Last edited by Johan.jones; Apr 24th, 2006 at 10:15 AM.

  9. #9
    Join Date
    Jul 2006
    Posts
    5

    Default

    I also had the "not mapped" problem. After tracing through the Hibernate Annotation code, I eventually figured out that I was using the wrong import in the class I was trying to map. The correct import is:

    import javax.persistence.Entity;

    When I was writing the file in Eclipse, I must have chosen "import org.hibernate.annotations.Entity;" instead, which is wrong.

  10. #10
    madhav Guest

    Default

    I solved the problem I encountered using exact 'case' of the persistent class.

    I had a Message class that is getting persisted to MESSAGES table.
    My query read like:
    Code:
    session.createQuery("from message m order by m.text asc").list()
    That did not work.

    Changing to
    Code:
    session.createQuery("from Message m order by m.text asc").list()
    worked !!!

    Environment: Hibernate 3.2, Spring 2, Hibernate Annotations, Ejb3 persistence jar

    -Madhav

Posting Permissions

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