tags:

views:

372

answers:

1

I have a class User that can have many loginNames:

@Entity
public class User {

  @ElementCollection
  private List<String> logins = new ArrayList<String>();

}

I want to ensure that each login is unique in the system when a user registers. When someone logs in the user object should be found by the login name. So the elements in the collection are some kind of (database) key.

How can I make them keys? How can I query efficiently? Would it be better to use a separate entity class for the login name?

+1  A: 

What do you mean key? You can add "unique" constraints on CollectionTable to make them unique. If they are supposed to key across into some other table then the only sensible answer is to have a User Entity.

DataNucleus
With "key" I mean that I can get the related object without quering all object whether they contain a certain value in the login list. Would be `@CollectionTable(uniqueConstraints=@UniqueConstraint(columnNames={"USER_LOGIN"}))` correct?Is it possibly to query the database for a user with a certain login (UserRepository#findByLogin(String)) efficiently?
deamon
unique constraints looks ok, but then you can easily try it.Whether a query is "efficient" is down to the JPA implementation. The user provides a JPQL query only and that's all they should have to do. If you want to search on some column then you could index it also
DataNucleus