作为ORM的新手,我想找到一种方法来定义一个简单(意味着没有额外的实体)映射实体中的一个列表(或一组)字符串.我找到了这个样本:
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
@ElementCollection
@CollectionTable(name = "tags")
private Set tags;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set getTags() {
return tags;
}
public void setTags(Set tags) {
this.tags = tags;
}
}
这似乎符合我的需要.但是,使用Eclipse的hibernate3-maven-plugin处理这个类:2.2:hbm2ddl,我最终得到以下错误:
[ERROR] Failed to execute goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl (default) on
project test-database: Execution default of goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl failed: Could
not determine type for: java.util.Set, at table: Book, for columns:
[org.hibernate.mapping.Column(tags)] -> [Help 1]
指定@ElementCollection(targetClass = String.class)没有帮助.将列定义添加到tags字段(@Column(name =“tags”,columnDefinition =“character varying(255)”,nullable = false))会导致成功构建但生成此SQL:
create table Book (
id int8 not null,
tags character varying (255) not null,
primary key (id)
);
这不是我想要的,因为我期望最终得到一个链接到books表的标签表.有人能指出我正确的方向吗?谢谢.