Reputation: 2397
I have a bean of type string[] which has two or more values. I want to save the array:
user.setItem(item[i]);
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
But I am getting only one data item saved not the entire array .
Upvotes: 1
Views: 2838
Reputation: 33785
If you are using Annotation, do as follows (Use List instead of Array)
@Entity
public class User {
private List<String> itemList = new ArrayList<String>();
@CollectionOfElements
@JoinTable(name="TABLE_ITEM")
private List<String> getItemList() {
return this.itemList;
}
}
And do as follows
User user = (User) sessionFactory.openSession().get(User.class, userId);
user.getItemList().add(item);
Because you have a managed Entity instance (User) and the lifecycle of a value-type instance (your String list) is bound to that of its owning entity instance (User). Hibernate will save your new item.
Upvotes: 3