2010-06-24

org.hibernate.MappingException: Could not determine type for: java.util.List, at table exception

In case someone got an error similar to this one:

Caused by: org.hibernate.MappingException:
Could not determine type for: java.util.List,
at table: DOGS, for columns:
[org.hibernate.mapping.Column(nicknames)]
[Line breaks added for readability]

... I found the solution.

It was caused by:
import javax.persistence.ElementCollection;
    // ...
    @ElementCollection(fetch = FetchType.EAGER)
    public List<String> getNicknames() {
        return nicknames;
    }
    // ...

If you are getting this error in a similar situation, the reason is a bug in your Hibernate implementation and you should get a newer one (I recommend 3.5.3-Final or later).

Here's my pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.m1key</groupId>
    <artifactId>springtx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.5.3-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.3-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.3-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.3.0.ga</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.3-Final</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.7</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jpa</artifactId>
            <version>2.0.8</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>JBoss Repo</id>
            <url>http://repository.jboss.com/maven2</url>
        </repository>
        <repository>
            <id>r.j.o-groups-public</id>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
</project>

Please note it may also happen for java.util.Collection, java.util.Map and java.util.Set.

18 comments:

  1. Your post helped solve my problem! I thought I was using Hibernate 3.5.2-FINAL but it turns out that the hibernate3-maven-plugin has its own dependency on Hibernate 3.3.1.GA. So whenever I was calling hbm2ddl, I would get the Exception you mentioned on Set<MyEnumType>.

    Once I added the dependencies for a newer Hibernate in my pom.xml for the plugin, I no longer get the Exception and the mapping of the Set works as expected.

    ReplyDelete
  2. Yay! That's exactly why I write those quick "problem - solution" posts.

    Thanks, man. :)

    ReplyDelete
  3. I've had this issue all along, and it was all because of that dependency missing in the hibernate3 Maven plugin.

    ReplyDelete
  4. That's not a bug. You were trying to use a JPA 2.0 feature on a JPA 1 Hibernate.

    Hibernate first implementation for JPA 2.0 came with 3.5.0

    ReplyDelete
  5. guys, i had exactly the same problem. after trying several options i found out that setting the mapping annotations on the field definitions solves the problem.

    hope it helps someone, cheers

    ReplyDelete
  6. Indeed moving my annotations:

    @BusinessField
    @ElementCollection(targetClass = ServiceName.class)
    @CollectionTable(name = "SERVICE_NAMES", joinColumns = @JoinColumn(name = "ID"))
    @Column(name = "ID")

    to fields saved the problem!!!

    byteslash thanks so much!!!! :-)

    ReplyDelete
  7. Had the same issue, moved annotations to the fields, and it worked, but why? Should the annotations not be on the get() methods and should work as well?

    ReplyDelete
  8. Thanks to your post, I was able to stop agonizing over the same problem. My solution was just to do this:


    org.codehaus.mojo
    hibernate3-maven-plugin
    2.2

    ...


    ...




    org.hibernate
    hibernate-core
    3.6.1

    ...



    Then I can keep the annotation on the getter without any problem.

    ReplyDelete
  9. Ugh, sorry, the post got messed up. The gist of it is to add the dependency to the latest version of hibernate-core to the plugin configuration of hibernate3-maven-plugin

    ReplyDelete
  10. I got this error after resolving like you told:
    "java.lang.ClassNotFoundException: org.hibernate.util.xml.Origin"

    what i have to do now?

    kindly reply me please urgently

    ReplyDelete
  11. Unknown, could it be that you have messed up the versions of different Hibernate artifacts? Does this occur when you're testing or deploying or building? The class you're missing appears to be in hibernate-entitymanager-3.6.0.Final which would suggest something's messed up with the versions.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi, have you tried any of the solutions listed by other people commenting on this post?
      Cheers

      Delete
  13. Hi Michal,

    I am still getting this error.

    "Could not determine type for: java.util.Set, at table: Role, for columns: [org.hibernate.mapping.Column(permissions)]"

    I have tried to update with the latest hibernate jars


    org.hibernate
    hibernate-core
    4.0.1.Final


    org.hibernate
    hibernate-validator
    4.2.0.Final


    org.hibernate.common
    hibernate-commons-annotations
    4.0.1.Final
    tests


    org.hibernate.javax.persistence
    hibernate-jpa-2.0-api
    1.0.1.Final


    org.hibernate
    hibernate-entitymanager
    4.0.1.Final


    javax.validation
    validation-api
    1.0.0.GA
    provided

    ReplyDelete
  14. Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: Shops, for columns: [org.hibernate.mapping.Column(productList)]

    Shop is mapped to Product as
    @OneToMany
    @JoinTable(name="Shop_Product", joinColumns=@JoinColumn(name="Shop_Id"), inverseJoinColumns=@JoinColumn(name="Product_Id"))
    private Collection productList = new HashSet();

    ReplyDelete
    Replies
    1. Hi Abhishek, thanks for visiting. You're not using the latest version of Hibernate.
      Also, have you seen other people's suggestions in the comments to this post?

      Delete


  15. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

    ReplyDelete
  16. نقدم لكم عمئلنا الكرام افضل شركة تنظيف فى بالطائف شركة تنظيف بالطائف شركة اوبى كلين ان عمليه التنظيف من اهم العوامل والوسائل التى تتطلب عملية شديد من الحرص والجهد والتعب اثناء عملية التنظيف شركة تنظيف منازل بالطائف مع شركة اوبى كلين وداعا تمام للتعب والجهد والمشقهه لان الشركة توفر لك كل انواع والعموال التى تساعدك فى عمليه التنظيف كما ايضا شركة اوبى كلين تمتلك افضل العماله المدربة على اكمل وجهه شركة تنظيف خزانات بالطائف حتى نوفر لك خدمه غير متاحه ال لدى شركة اوبى كلين وداعا للتعب بعد الان مع شركة اوبى كلين توفر لك كل سبل الراحه شركة مكافحة حشرات بالطائف لان الشركه متخصصه فى عملية التنظيف منذ زمن بعيد

    ReplyDelete