3

I have a site with multiple pages, and each page has a table that contains some data. The tables have many columns, and users can select which columns to display or hide. I am trying to persist these selections.

The question I have is choosing the best way to persist these objects.

I see many possible ways, none of which Ive had success in implementing. But before I commit to one of them, I would like to know if I am doing it horribly wrong, or am way off the mark with how this should be implemented.

I have a user class that is persisted already.

Option #1 In my user entity, add a field of Map> where the key is the page name, and the list is the columns the user has selected on that page. Using @ElementCollection.

Option #2 User entity has a @onetomany List. Which requires a UserColumns entity, with pagename as primary key, and @oneToMany relationship with another entity for each column.

Option 1:

@Entity
public class User{

private Map<String, List<String>> columns;

@ElementCollection
public void setColumns(Map<String, List<String>> colunms){

}


}

Option 2:

@Entity
public class User{

private List<UserColumns> columns;

@ElementCollection or @OneToMany
public void setColumns(List<UserColumns> colunms){

}


}

I realize this will be difficult to answer without looking at the actual code, and I've probably not described the situation adequately or precisely, but welcome all input anyways. Thank you.

4

1 に答える 1

3

個人的には、このようなことを行うときは常に、他のエンティティとの 1 対多の関係を使用することを好みます。私の理由は、今後はより柔軟な設計になるからです。列名を知るだけでなく、追加情報が必要になるシナリオを簡単に確認できます。文字列の代わりに実際のエンティティを使用することで、新しいプロパティをクラスに追加するだけです。ただし、文字列のみを使用している場合は、はるかに広範なリファクタリングが必要になります。最初は少し時間がかかりますが、ほとんどの場合、最終的には報われます。

于 2012-06-20T04:31:33.917 に答える