tags:

views:

64

answers:

1

Hi All,

i have a bean with 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 saved not the entire array

+2  A: 

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.

Arthur Ronald F D Garcia