嗨任何人都可以帮我解决这个异常。这里我有一个名为Person的实体类,而实例变量的Person类有name和petnames,这里的petnames我把它作为数据类型为集合的简单值类型。只需查看我的以下代码和正在生成的相应异常。我这么多天都面临着这个例外。我正在努力解决这个问题我不会在哪里出错。
人员类
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
答案 0 :(得分:2)
考虑改变这个
@ElementCollection
@CollectionTable(name = "petname")
private Set<String> petname = new HashSet<String>();
我的意思是将ElementCollection和Collectiontable注释一起移到字段上方。
如果要更改列的名称,请使用此名称。
@ElementCollection
@CollectionTable(name = "petname")
@Column(name="ColumnName")
private Set<String> petname = new HashSet<String>();
当您使用String基本类型时,也不需要joinColumns = @JoinColumn(name = "PERSON_ID"
。
答案 1 :(得分:0)
首先,JPA注释必须全部在属性上设置,或者在getter上设置。不是两者兼而有之,也不是同一个实体。
其次,试试:
@ElementCollection
@CollectionTable(name = "petname", joinColumns = {@JoinColumn(name = "<column id name in Petname>", nullable = false, referencedColumnName = "<column id name in Person>")})
@Column(name="<column name for petname>")
private Set<String> petname = new HashSet<String>();