0
Hi I am new to Hibernate and I am getting the following exception while trying to save a collection.

Could not determine type for: java.util.List, for columns

**Here are my codes**

    @Entity
    @Table(name="USER_DETAILS")

    public class User {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;
        @ElementCollection
        private List<Address> aList=new ArrayList<Address>();


        @Embeddable

        public class Address {

        @Column(length=50)
        private String city;

        @Column(length=50)
        private String state;

        @Column(length=50)
        private String country;

        @Column(length=10)
        private int zip;
}

@Embeddable
public class Address implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(length=50)
    private String city;

    @Column(length=50)
    private String state;

    @Column(length=50)
    private String country;

    @Column(length=10)
    private int zip;

    public String getCity() {
        return city;
    }

        public class TestUser {

        public static void main(String[] args) {
            User user=new User();
            Address addr=new Address();

            addr.setCity("Asjasjs");
            addr.setCountry("gfgf");
            addr.setState("atyty");
            addr.setZip(345343);

            user.getaList().add(addr);

            Session session=SessionFactoryUtil.getSession();
            session.beginTransaction();

            session.save(user);

            session.getTransaction().commit();
            session.close();

        }

This is how it stores the data in database enter image description here

subhashis
  • 4,629
  • 8
  • 37
  • 52

1 Answers1

0

You need a table in database where Address will be stored. That table needs to have all the columns that are mapped in Address class, plus one column which is used to relate it to table USER_DETAILS

With this in place, the mapping for aList field will look like this

@ElementCollection
@CollectionTable(name = "ADDRESS", joinColumns = {@JoinColumn(name = "USER_DETAILS_ID")})
private List<Address> aList=new ArrayList<Address>();
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • I did this but it says that Student is not Serializable then I implement Serializable interface then it stored the serialized object in the database. Why this is happning? – subhashis Jan 20 '15 at 05:09
  • It is generaly good practice for all entities to implement Serializable, but [read this](http://stackoverflow.com/a/2021640/4074715) for more info. – Predrag Maric Jan 20 '15 at 07:34
  • I know the concept behind it but it stored the serialize object by making a column by the collection name eg: column name is aList and it stored the serialize object into it and in the USER_DETAILS table it did not create any new table. – subhashis Jan 20 '15 at 07:55
  • You didn't mention `Student` in your question, there is no code so I can't tell for sure, but JPA probably tries to serialize the collection (of Students) if mapped to single column. That's why you got that Serializable exception. However, in the database it will not be in a readable form, and I don't think you could query on it. I'm not even sure that deserialization will work without custom code, that should be tested. – Predrag Maric Jan 20 '15 at 09:59
  • That is no Student class it is Address class. I have added all the code. – subhashis Jan 20 '15 at 14:08
  • If you don't put `@CollectionTable` on `aList`, the whole list will be serialized into a single column (`USER_DETAILS#ALIST`), like in your screenshot. For this, `Address` needs to be serializable, hence the message you got. With `@CollectionTable` you will need a table for address data, as I explained in my answer. Is there anything else that is not working? – Predrag Maric Jan 20 '15 at 15:03
  • Even if I used the annotation @CollectionTable(name = "ADDRESS", joinColumns = {@JoinColumn(name = "USER_DETAILS_ID")}) it says Student is not serializable, So I implement Serializable. – subhashis Jan 20 '15 at 16:01
  • OK, and does everything work then? Serializable is not required for entities but it is good practice, as I said, so maybe JPA requires serializable objects whenever `@ElementCollection` is used, regardless of `@CollectionTable`. I don't know this for sure, just my guess. – Predrag Maric Jan 20 '15 at 16:12