org.hibernate.MappingException: Unknown entity: java.lang.Long

26,799

Solution 1

Without looking into this further the result-class should be one of your mapped entities and because Long is not one of your Entities you're receiving the AnnotationException.

Try removing the result-class from the mapping as it is only used for building queries that return Entity Objects.


Edit:

In regard to: org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported:

After a quick dig around I found this:

https://hibernate.atlassian.net/browse/ANN-661

Where I read:

Workaround is to do such queries using Session.createSQLQuery() or straight JDBC.

There are a couple of examples for such queries here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#d0e17378

i.e

sess.createSQLQuery("SELECT * FROM CATS").list();

sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();

or further down in Example 18.4 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#querysql-namedqueries

<sql-query name="mySqlQuery">
    <return-scalar column="name" type="string"/>
    <return-scalar column="age" type="long"/>
    SELECT p.NAME AS name, 
           p.AGE AS age,
    FROM PERSON p WHERE p.NAME LIKE 'Hiber%'
</sql-query>

Solution 2

<named-native-query name="getCaseNumberByCommId">
  <query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
</named-native-query>

something like this works for me.

Share:
26,799
slashdottir
Author by

slashdottir

a time for cats a time for lulz a time for trolls a time to bait trolls a time to gather cats together

Updated on April 03, 2020

Comments

  • slashdottir
    slashdottir about 4 years

    I am trying to create a named-native-query that returns a Long.

    Here is my orm.xml file (simplified as much as possible)

    <?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
      version="2.0">
    
    <named-native-query name="getCaseNumberByCommId" result-class="java.lang.Long">
      <query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
    </named-native-query>
    
    </entity-mappings>
    

    this is the error I get:

    ERROR - org.hibernate.impl.SessionFactoryImpl - Error in named query: getCaseNumberByCommId [coral:launch] org.hibernate.MappingException: Unknown entity: java.lang.Long

    I've also tried just specifying "Long"

    <named-native-query name="getCaseNumberByCommId" result-class="Long">
      <query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
    </named-native-query>
    

    and strangely get this error:

    Caused by: org.hibernate.AnnotationException: Unable to find entity-class: Long ... Caused by: java.lang.ClassNotFoundException: Long

    Java can't find Long in java.lang?

    Thank you for any clues

    edit: I tried removing the 'result-class' annotation :

        <named-native-query name="getCaseNumberByCommId" >
            <query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
        </named-native-query>
    

    and get this error:

    nested exception is org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported

    Update*

    I never did find a way to do this, but since the database had a uniqueness constraint on comm_id, I was able to just return a mapped pojo object instead of a count.

    e.g.

        <named-native-query name="getByCommId" result-class="com.foo.model.Communication">
            <query>SELECT * FROM communications WHERE comm_id =(?1)</query>
        </named-native-query>
    

    and then pull the desired case_id out of the returned pojo.

  • slashdottir
    slashdottir over 12 years
    Thank you, I tried removing the result-class annotation and get this error: nested exception is org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
  • edwardsmatt
    edwardsmatt over 12 years
    @slashdottir Did any of the extra information help you out or did you solve it another way?
  • slashdottir
    slashdottir over 12 years
    I finally had to give up on returning a Long and wound up returning a mapped pojo object instead.... Since it turned out that in the database there is a uniqueness constraint which means only 1 row per identifier, I was able to work around this problem in this way