1

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 .

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.