在hibernate中映射简单值类型的集合时给出异常

时间:2014-03-06 07:54:05

标签: java hibernate

我有Entity名为Person,其名称属性为String,而人petnames有收藏。映射到Hibernate时,我会遇到异常。我该如何解决这个问题?

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.io.Serializable;

import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

/**
 *
 * @author hyva
 */
@Entity
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;
        private Long id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

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

    public void setId(Long id) {
        this.id = id;
    }
    private String name;
    private Set<String> petname;

    @ElementCollection
    @CollectionTable(name = "petname", joinColumns =
            @JoinColumn(name = "PERSON_ID"))
    public Set<String> getPetname() {
        return petname;
    }
}

* 例外: *

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.HibernateUtil.<clinit>(HibernateUtil.java:28)
    at com.myapp.struts.HibernateComplexValue.main(HibernateComplexValue.java:19)
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

2 个答案:

答案 0 :(得分:0)

@CollectionTable移到getPetname上作为注释,如下所示:

private Set<String> petname;

@ElementCollection
@CollectionTable(name = "pets", joinColumns =@JoinColumn(name = "PERSON_ID"))
public Set<String> getPetname(){
      return petname;
}

答案 1 :(得分:0)

您的注释@CollectionTable位于您的属性petName之后!是复制/粘贴错误?

您尚未映射您的petname列。您的默认属性名称与表名称相同( petname )...
什么应该是这个表中包含宠物名称的列的名称?

尝试添加@Column(name="YOUR_COLUMN_NAME")以强制映射到此列。