user468587
user468587

Reputation: 5031

could not initialize a collection

i have recs model mapping to a recs table in postgresql, some fields are declared to be Set, when i ran the code, it threw errorr as:

org.hibernate.exception.SQLGrammarException: could not initialize a collection: Recs._recsDetailName
caused by: org.postgresql.util.PSQLException: ERROR: relation "recs__recsdetailname" does not exist

my recs model:

@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;

    ..

my table:

        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[]                   

|

a sample recs_detail_name in the db is like this:

{"TeleNav GPS Navigator","Photobucket for BlackBerry","Cellfire Mobile Coupons"}

anyone know what might be wrong??? Thanks

Upvotes: 1

Views: 19316

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

An ElementCollection is not mapped to a single column where the set would be serialized. It's mapped using an additional table which contains a column for your String (recs_detail_name) and a foreign key column referencing the primary key of the owning table. This is of course described in the hibernate documentation.

If you want to map a Set to a single column, you'll have to use a custom user type.

Upvotes: 1

Related Questions