这是我的实体类
@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
}
这是我的地址类
@Embeddable
public class Address
{
private String Street;
private String City;
private String State;
private String PinCode;
// Setters and Getters Below
}
我正在使用独立的 Java 类尝试插入我的MySQL数据库。因为,我是 Hibernate 的新手,我现在只想使用这个注释而不是关系。
我的独立 Java 类名为 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();
}
}
现在,当我运行 HibernateTest 类时,我发现这个异常出现了。我不知道为什么。
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: