org.hibernate.MappingException:无法确定:org.sahil.dto.Address的类型,对于列:[org.hibernate.mapping.Column(element)]

时间:2014-09-16 18:41:50

标签: hibernate

UserDetails.java

package org.sahil.dto;

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

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionOfElements;

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

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

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    @ElementCollection
    private Set<Address> addresses = new HashSet();

    /**
     * @return the addresses
     */
    public Set<Address> getAddresses() {
        return addresses;
    }

    /**
     * @param addresses the addresses to set
     */
    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }

    @Column(name = "USER_NAME")
    private String userName;

    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;

    /**
     * @return the creationDate
     */
    public Date getCreationDate() {
        return creationDate;
    }

    /**
     * @param creationDate
     *            the creationDate to set
     */
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName
     *            the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Address.java

    package org.sahil.dto;

    import javax.persistence.Embeddable;
    import javax.persistence.Entity;


    public class Address {
        private String address;
        private String city;
        private String state;
        private String pincode;
        /**
         * @return the address
         */
        public String getAddress() {
            return address;
        }
        /**
         * @param address the address to set
         */
        public void setAddress(String address) {
            this.address = address;
        }
        /**
         * @return the city
         */
        public String getCity() {
            return city;
        }
        /**
         * @param city the city to set
         */
        public void setCity(String city) {
            this.city = city;
        }
        /**
         * @return the state
         */
        public String getState() {
            return state;
        }
        /**
         * @param state the state to set
         */
        public void setState(String state) {
            this.state = state;
        }
        /**
         * @return the pincode
         */
        public String getPincode() {
            return pincode;
        }
        /**
         * @param pincode the pincode to set
         */
        public void setPincode(String pincode) {
            this.pincode = pincode;
        }
    }

HibernateTest.java

package org.sahil.hibernate;

import java.util.Date;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.sahil.dto.Address;
import org.sahil.dto.UserDetails;

public class HibernateTest {

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

        Address address=new Address();
        address.setAddress("Address");
        address.setCity("city");
        address.setPincode("110007");
        address.setState("State");

        Address address1=new Address();
        address1.setAddress("Address");
        address1.setCity("city");
        address1.setPincode("110007");
        address1.setState("State");

        user.getAddresses().add(address);
        user.getAddresses().add(address1);
        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

}

上面是代码..正确配置了hibernate.cfg.xml。有人可以告诉我为什么我得到以下错误:

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: org.sahil.dto.Address, for columns: [org.hibernate.mapping.Column(element)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
    at org.hibernate.mapping.Collection.validate(Collection.java:276)
    at org.hibernate.mapping.Set.validate(Set.java:19)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1106)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.sahil.hibernate.HibernateTest.main(HibernateTest.java:32)

我认为@ElementCollection应该在这里工作。

1 个答案:

答案 0 :(得分:0)

Address类需要使用@Embeddable进行注释。