MappingException:无法确定java.util.Set的类型

时间:2015-09-07 10:14:36

标签: java hibernate hibernate-mapping

在运行以下程序时,我收到错误

  

org.hibernate.MappingException:无法确定类型:java.util.Set,对于列:[org.hibernate.mapping.Column(ListOfAddress)]

我添加了import javax.persistence.ElementCollection; 但我得到了同样的错误

任何人都可以建议我任何选择吗?

HibernateTest.java

package src.com.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.mapping.*;
import src.com.hibernate.Address;
import src.com.hibernate.UserDetails;

public class HibernateTest {

    public static void main(String[] args) 
    {
        UserDetails user = new UserDetails();
        user.setUserName("Surendar");

        Address addr = new Address();
        addr.setStreet("Abith colony");
        addr.setCity("Chennai");
        addr.setState("TamilNAdu");
        addr.setPincode("600015");  

        Address addr1= new Address();
        addr1.setStreet("Anna Salai");
        addr1.setCity("Chennaimain");
        addr1.setState("TN");
        addr1.setPincode("600033");

        user.getListOfAddress().add(addr);
        user.getListOfAddress().add(addr1);

        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();

    }

}

UserDetails.java

      package src.com.hibernate;


        import java.util.HashSet;
        import java.util.Set;

        import javax.persistence.ElementCollection;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.GenerationType;
        import javax.persistence.Id;
        import javax.persistence.Table;

        @Entity
        @Table(name="UserTABLEaddress")
        public class UserDetails 
        {

            @Id
            @GeneratedValue(strategy=GenerationType.AUTO)
            private int userId;     
            private String userName;

            @SuppressWarnings("rawtypes")
            @ElementCollection
            private Set<Address> ListOfAddress = new HashSet();

            public Set<Address> getListOfAddress() {
                return ListOfAddress;
            }
            public void setListOfAddress(Set<Address> listOfAddress) {
                ListOfAddress = listOfAddress;
            }
            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;
            }

Address.java

      package src.com.hibernate;
      import javax.persistence.Embeddable;

      @Embeddable
      public class Address 
         {

private String street;

private String city;

private String state;

private String pincode;
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getPincode() {
    return pincode;
}
public void setPincode(String pincode) {
    this.pincode = pincode;
}

}

hibernate.cfg.xml中

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="src.com.hibernate.UserDetails"/>
    </session-factory>
    </hibernate-configuration>

2 个答案:

答案 0 :(得分:1)

我认为你们正在混合概念。您尝试在@Embeddable关系中使用的类中使用many-to-one注释。这是导致错误的原因。

如果要将ListOfAddress用作元素集合,则必须将Address定义为实体并正确配置关系,例如:

...
@ElementCollection
@CollectionTable(name="address", joinColumns=@JoinColumn(name="userId"))
@Column(name="addresses")
private Set<Address> ListOfAddress = new HashSet<Address>();
...

在地址类

@Entity
@Table(name="address")
public class Address 
...

网上有很多例子。

答案 1 :(得分:0)

我遇到了类似的问题,当我将我的java编译器版本从1.6改为1.5时,它对我有用。

所以可能会检查你是否属于这种情况