@CollectionTable和@ElementCollection用法

时间:2014-03-11 09:32:46

标签: java hibernate hibernate-mapping

嗨,任何人都可以帮我解决这个异常。这里有一个名为Person的实体类,而且这个实例变量的类有名字和小名,这里的小名字我把它作为数据类型集合的简单值类型而且有通过使用jpa注释进行映射,请查看我的以下代码和正在生成的相应异常。我这么多天都面临这个例外。我正在努力解决这个问题,我不明白我哪里出错了。

人员类

 private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @ElementCollection
    @CollectionTable(name = "petname")
//    @Column(name = "ColumnName")
    private Set<String> petname = new HashSet<String>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPetname(Set<String> petname) {
        this.petname = petname;
    }

    public Set<String> getPetname() {
        return petname;
    }

    public boolean addPets(String p) {
        return petname.add(p);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.myapp.struts.Person[ id=" + id + " ]";
    }

Excaption

Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(petname)]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.myapp.struts.NewHibernateUtil.<clinit>(NewHibernateUtil.java:28)
    at com.myapp.struts.Test.main(Test.java:16)
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(petname)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
    aJava Result: 1

1 个答案:

答案 0 :(得分:0)

@CollectionOfElements
@JoinTable(name = "PET", joinColumns = { @JoinColumn(name = "person_id") })
@Column(name = "petname")
private Set<String> petname = new HashSet<String>();