1

I am working on a Spring-MVC project where I am using Hibernate for persistence. In one of the model classes I have a List which I want to persist. I am facing the problem as I don't know which dataType to use in PostgreSQL and do I need instruct hibernate in some way or other that I am trying to persist a List. Performance requirements are not that much of a problem on this list, as it does not get that much action. I am posting some code for reference, kindly let me know. Thanks a lot :

GroupAccount model class :

@Entity
@Table(name="groupaccount")
public class GroupAccount {
 @Column(name = "blacklist")
 private List<String> blacklist;
 public List<String> getBlacklist() {
 return blacklist;
 }
 public void setBlacklist(List<String> blacklist) {
 this.blacklist = blacklist;
 }
}

I would sometimes require to update the values of the blacklist, so I have a method in DAO which updates the groupAccount, I am pasting it below.

GroupAccountDAOImpl edit function :

 @Override
 public void editGroupAccount(GroupAccount groupAccount) {
 session = this.sessionFactory.getCurrentSession();
 GroupAccount groupAccount1 = (GroupAccount)session.get(GroupAccount.class,groupAccount.getGroupId());
 if(!(groupAccount1==null)){
 groupAccount.setOwnedcanvas(groupAccount1.getOwnedcanvas());
 groupAccount.setGroupMembersSet(groupAccount1.getGroupMembersSet());
 session.merge(groupAccount);
 session.flush();
 }
 }

One use-case for adding users in blacklist :

List<String> blackListUsers;
blackListUsers = groupAccount.getBlacklist();
blackListUsers.add(memberForBlackListing.getMemberUsername());
groupAccount.setBlacklist(blackListUsers);
this.groupAccountService.editGroupAccount(groupAccount);
removeAllMemberships(memberId);
return true;

Any help would be nice. Thanks a lot. :-)

fap
7031 gold badge7 silver badges15 bronze badges
asked Feb 11, 2015 at 10:07

1 Answer 1

3

You can't map List<String> to a single column. For these cases, @ElementCollection is used

@ElementCollection
@CollectionTable(name="blacklist", joinColumns=@JoinColumn(name="group_account_id")
@Column(name = "name")
private List<String> blacklist;

This requires a database table named blacklist with columns name and group_account_id (which will be used as a foreign key to group_account table). Of course, table and column names are customizable.

answered Feb 11, 2015 at 10:27
Sign up to request clarification or add additional context in comments.

3 Comments

Ok. Thanks a lot, but is there some other type I can use, like an array which I can use for the above mentioned use-case. I just need to check if an email-address exists in the blacklist and add an Email, but in same database table. Thanks a lot.
You can use simple String field, and keep the list as comma separated values (CSV). Requires a little pack-unpack code, but it's the simplest solution.
I agree. I will try with that. Thanks a lot.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.