@collectiontable和@elementcollection用法

rur96b6h  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(553)

嗨,任何人都可以帮我解决这个异常。这里我有一个名为person的实体类,拥有示例变量的thisclass有name和petnames,这里petnames作为简单值类型,数据类型是collection,并使用jpa注解进行Map,请看我下面的代码和生成的相应异常。很多天以来,我都面临着这个例外,我正在努力解决这个问题,我不知道我错在哪里。
人员等级

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 + " ]";
    }

突厥

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
b09cbbtk

b09cbbtk1#

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

相关问题