运行hibernate应用程序时出错

时间:2013-07-15 16:20:41

标签: hibernate

控制台错误如下,我正在尝试一个简单的应用程序来了解如何使用hibernate在数据库中保留集合。我正在使用eclipse,hibernate 3.3。据我所知,我做对了。但控制台错误说我错了。

21:33:51,354  INFO Version:15 - Hibernate Annotations 3.4.0.GA
21:33:51,363  INFO Environment:560 - Hibernate 3.3.2.GA
21:33:51,364  INFO Environment:593 - hibernate.properties not found
21:33:51,366  INFO Environment:771 - Bytecode provider name : javassist
21:33:51,369  INFO Environment:652 - using JDK 1.4 java.sql.Timestamp handling
21:33:51,416  INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
21:33:51,417  INFO Configuration:1474 - configuring from resource: /hibernate.cfg.xml
21:33:51,418  INFO Configuration:1451 - Configuration resource: /hibernate.cfg.xml
21:33:51,475  INFO Configuration:1589 - Configured SessionFactory: null
21:33:51,477  INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
21:33:51,510  INFO AnnotationBinder:419 - Binding entity from annotated class: com.hibernate.CollectionDemo.UserDetail
21:33:51,533  INFO EntityBinder:422 - Bind entity com.hibernate.CollectionDemo.UserDetail on table USER_DETAIL_COLLECTIONS
21:33:51,562  INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: USER_DETAIL_COLLECTIONS, for columns: [org.hibernate.mapping.Column(listOfAddress)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276)
    at org.hibernate.mapping.Property.isValid(Property.java:207)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:458)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1149)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1334)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.hibernate.CollectionDemo.TestUserDetail.main(TestUserDetail.java:15)

我的模特课就在这里

package com.hibernate.CollectionDemo;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="USER_DETAIL_COLLECTIONS")
public class UserDetail
{
    @Id
    private int userId;
    private String userName;

    @ElementCollection(targetClass=Address.class)
    private Set<Address> listOfAddress=new HashSet<Address>();

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Set<Address> getListOfAddress() {
        return listOfAddress;
    }

    public void setListOfAddress(Set<Address> listOfAddress) {
        this.listOfAddress = listOfAddress;
    }
}

这里包含将嵌入在userdetail类中的地址类

package com.hibernate.CollectionDemo;

import javax.persistence.Embeddable;

@Embeddable
public class Address 

{
    private String city;
    private String streetName;
    private String pincode;
    private String State;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreetName() {
        return streetName;
    }
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    public String getPincode() {
        return pincode;
    }
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }
    public String getState() {
        return State;
    }
    public void setState(String state) {
        State = state;
    }

}

这里是我的配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/testDb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!--  <property name="hibernate.default_schema">TESTSCHEMA</property> -->

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup --> 
        <property name="hbm2ddl.auto">create</property>

        <!-- name of the annotated entity class -->
        <mapping class="com.hibernate.CollectionDemo.UserDetail"/>

        <!--<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
        <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>

    -->
    </session-factory>

</hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

作为documented,在JPA 2.0中引入了@ElementCollection。 Hibernate 3.3。实现JPA 1,这就是为什么使用这个结构不会起作用的原因。

通过更新到更新的Hibernate版本可以解决问题。一种选择是至少更新为3.6.10(最新且可能持续3.x) - 可能立即转到休眠4.x