Suggested Pages

Saturday, June 30, 2012

JPA @ElementCollection

Suppose you have an entity bean called User and this entity has a lot of nicknames. You can make the following mapping:
User.java
 ...

@Entity
public class User {

   @Id
   private int userId;

   @Column
   private String name;

   @ElementCollection
   private Set nicknames;

   public User() {
      super();
      this.nicknames = new TreeSet();
   }
 ...

}


Database point of view

create table User (
        parentId integer not null,
        name varchar(255),
        primary key (parentId)
)
create table User_nicknames (
        User_parentId integer not null,
        nicknames varchar(255)
)

alter table User_nicknames 
        add constraint FKA44D16712C2B0AD9 
        foreign key (User_parentId) 
        references User

No comments :

Post a Comment

Suggested Pages