我有recs模型映射到postgresql中的recs表,一些字段被声明为Set,当我运行代码时,它将错误抛出:
org.hibernate.exception.SQLGrammarException: could not initialize a collection: Recs._recsDetailName
caused by: org.postgresql.util.PSQLException: ERROR: relation "recs__recsdetailname" does not exist
我的recs模型:
@Entity
@Table(name = "RECS", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class Recs implements Serializable, Cloneable {
/**
* Serialized version unique identifier.
*/
private static final long serialVersionUID = -7316874431882307750L;
@Id
@Column(name = "id")
private int _id;
@Basic
@Column(name = "recs_num")
private int _recsNum;
@Basic
@Column(name = "details")
private String _details;
@Column(name = "d_stamp")
private Date _timeStamp;
@ElementCollection(fetch = FetchType.EAGER)
@Column(name = "recs_detail_name")
private Set<String> _recsDetailName;
..
我的桌子:
Column | Type | Modifiers
-----------------------+-----------------------------+-------------------------------
id | integer | not null default
recs | xml |
recs_num | integer |
details | character varying(300) |
d_stamp | timestamp without time zone | default now()
recs_detail_name | text[]
|
db中的示例recs_detail_name如下所示:
{"TeleNav GPS Navigator","Photobucket for BlackBerry","Cellfire Mobile Coupons"}
谁知道什么可能是错的???感谢
答案 0 :(得分:1)
ElementCollection未映射到将序列化集合的单个列。它使用一个附加表进行映射,该表包含String(recs_detail_name)的列和引用拥有表的主键的外键列。这当然在hibernate documentation中描述。
如果要将Set映射到单个列,则必须使用自定义用户类型。