mkuff
mkuff

Reputation: 1682

jpa Hibernate @ElementCollection

I have a question which bothers me a bit.

When i use @ElementCollection in my entity hibernate (3.5) generates me a table without any constraints.

Important Edit

My Entity is a subclass of another class. That is maybe the reason why hibernate does not generate the pk and the fk.

Edit End

public MyEntity extends BaseEntity
@ElementCollection(fetch=FetchType.EAGER)
private Set<String> test;

Ends up with (postgres 9):

CREATE TABLE myentity_test
(
  entityid bigint NOT NULL,
  test character varying(255)
)

Is this my mistake that hibernate doesn't set any constraints like foreign keys or unique indexes? I could set it manually, but i would prefer to annotate the entity correctly that hibernate completely (as much as possible) creates my database.

Best regards, m

PS: My Bad at all. Mixed up two things which where going around in my mind. i meant @ElementCollection.

PS: The @Column helped to deliver the unique index. Edit: corrected the names to my example.

CREATE TABLE myentity_test
(
  entityid bigint NOT NULL,
  test character varying(255),
  CONSTRAINT itemequipable_test_test_key UNIQUE (test)
)

I would expect something like this (pseudo code):

CREATE TABLE myentity_test
(
  entityid bigint NOT NULL PRIMARY KEY,
  test character varying(255) PRIMARY KEY,
  CONSTRAINT fk_entityid references (myentity) on entityid
)

Upvotes: 1

Views: 5811

Answers (1)

Jeff
Jeff

Reputation: 3707

@Column and @JoinColum/@OneToMany annotations are the correct way to handle the foreign keys and unique constraints, even with the @ElementCollection.

In your particular example, there is not going to be a foreign key relationship because String (the type of your Set) is not mapped to another entity.

Upvotes: 1

Related Questions