gpt4 book ai didi

java - @CollectionTable 和 @ElementCollection 用法

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:18 30 4
gpt4 key购买 nike

嗨,有人可以帮我解决这个异常吗?这里我有一个名为 Person 的实体类,并且具有实例变量的 thisclass 有名称和宠物名称,这里的宠物名称我采用了简单的值类型,数据类型是集合,并且具有使用 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 + " ]";
}

说明

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

最佳答案

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

关于java - @CollectionTable 和 @ElementCollection 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22321582/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com