在运行以下程序时出现错误


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;
            }


地址.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>

最佳答案

我认为您在混搭概念。您尝试在@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
...


互联网上有很多例子。

09-15 16:00