0

I want to create a hibernate table which can store a list of values. I am doing something like:

@Column(name = "userHobbies")
@ElementCollection(targetClass = String.class)
@CollectionTable(schema = "school", name = "hobby", joinColumns = @JoinColumn(name = "id"))
private List<String> userHobbies= null;

In the table User I do not see a column "userHobbies". A table hobby is created though which is empty. Can anyone suggest something regarding how to tackle such cases, please?

EDIT:

Code to insert new records to User with hobbies:

ArrayList<String> hobbies= new ArrayList<String>();
hobbies.add(football);
hobbies.add(soccer);

User user = new User();
user.setName("ben");
user.setEmail("[email protected]");
user.setUserHobbies(hobbies);

getDAO().makePersistent(user);

Except the userHobbies all other fields are saved correctly.

1 Answer 1

1

That's the expected result. @CollectionTable is used to specify the name of the table where every element of the list is stored (name = "hobby"). joinColumns is used to specify the name of the column(s) which is used, in this hobby table, to store the reference to its owning user. And @Column is used to specify the name of the column, in the hobby table, that holds the element value (the hobby name).

So you'll end up with this, very similar to a OneToMany mapping:

Table user:
ID (primary key), name, ...

Table hobby:
ID (foreign key referencing user.ID), userHobbies

I would name the column "hobby" or "hobbyName" rather than "userHobbies", since it holds a single hobby.

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

6 Comments

Just want to make sure my question is clear, There is a table called User which has a column called userHobbies, which contains a list of hobbies for the user. and the @collectionTable creates a table hobby to map the hobbies with their ids. So my question is why do you suggest the userHobbies contains a single hobby? I want to store a list of hobbies.
Let's say user 1 has football and basketball as hobbies. You'll have one row for user 1 in the the user table, and 2 rows for its hobbies in the hobby table. The first row will contain (1, 'football') and the second one will contain (1, 'basketball'). That's classical relational database design. You don't have a table User with a column named userHobbies. You have a hibernate entity named User with a collection userHobbies. And the mapping you defined describes the generated schema.
oh ok, Thanks. But when I insert values into the User table, all the values are stored properly but the hobby table is empty. Can you suggest why it could be?
Edit your question, and show the code used to create this user and its hobbies.
Unless setUserHobbies() or makePersistent() does soething really unexpected, I don't see anything wrong with this code. Do you have a transaction around this code? Do you get any exception?
|

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.