Reputation: 7570
Here is my Entity class
@Entity // Model Class as a DB entity
public class UserDetails
{
@Id
private int userId;
private String name;
@ElementCollection
private Set<Address> listOfAddresses = new HashSet();
// Setters and Getters Below
}
Here is my Address Class
@Embeddable
public class Address
{
private String Street;
private String City;
private String State;
private String PinCode;
// Setters and Getters Below
}
Am using a standalone Java Class to try and insert into my MySQL database. Since, am new to Hibernate I just want to use this annotation and not the relations for now.
My standalone Java class named HibernateTest.java
public class HibernateTest
{
public static void main(String[] args)
{
UserDetails user = new UserDetails();
Address addr = new Address();
user.setUserId(1);
user.setName("Swateek");
addr.setCity("Berhampur");
addr.setPinCode("760001");
addr.setState("Odisha");
addr.setStreet("RKN");
user.getListOfAddresses().add(addr);
Address addrOff = new Address();
addrOff.setCity("Bangalore");
addrOff.setPinCode("560037");
addrOff.setState("MTH");
addrOff.setStreet("YH");
user.getListOfAddresses().add(addrOff);
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
Now when I run the class HibernateTest I find this exception coming up. I have no clue why.
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns:
Upvotes: 2
Views: 5133
Reputation: 359
Instead of having @javax.persistence.ElementCollection annotation try using @org.hibernate.annotations.CollectionOfElements.
Upvotes: 0
Reputation: 242696
@ElementCollection
is a part of JPA 2.0, which is supported by Hibernate since version 3.5.
If you version of Hibernate is older, you either need to upgrade it, or use similar Hibernate-specific annotation (@CollectionOfElements
) instead.
Upvotes: 9