1

Following is my Entity class

@Entity 
@Table(name = "USER_DETAILS")
public class UserDetails {

@Id 
private int userId;
@Column (name="USER_NAME")
private String userName;
@Temporal(TemporalType.DATE)
private Date date;
@ElementCollection
private Set<Address> streetAddress = new HashSet<Address>();


public Set<Address> getStreetAddress() {
    return streetAddress;
}
public void setStreetAddress(Set<Address> streetAddress) {
    this.streetAddress = streetAddress;
}
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}
public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

}

Following is Address class

public class Address {

private String city;
private String pin;
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getPin() {
    return pin;
}
public void setPin(String pin) {
    this.pin = pin;
}

}

And following is main class

public class HibernateMain {

public static void main(String[] args) {

    Address address = new Address();
    address.setCity("Pune");
    address.setPin("1232");

    Address homeAddress = new Address();
    homeAddress.setCity("home_Pune");
    homeAddress.setPin("home_1232");

    UserDetails user = new UserDetails();
    user.setUserId(3);
    user.setUserName("Second user");
    user.setDate(new Date());

    user.getStreetAddress().add(address);
    user.getStreetAddress().add(homeAddress);

    Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

    SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder()
                           .applySettings(cfg.getProperties()).build());

    Session session = sf.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
}
}

Exception is thrown at run time

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.example.model.Address, at table: UserDetails_streetAddress, for columns: [org.hibernate.mapping.Column(streetAddress)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
at org.hibernate.mapping.Collection.validate(Collection.java:315)
at org.hibernate.mapping.Set.validate(Set.java:40)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at com.example.hibernate.HibernateMain.main(HibernateMain.java:36)

I am using hibernate 4.3 and MySQL as database. If i dont use collection then it works for rest.What is wrong with code.

EDIT:

One other question.Am i using right code to get SessionFactory for my hibernate version as my IDE showing ServiceRegistryBuilder class as depricated. Thanks for help.

Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89

1 Answers1

2

The address class must be annotated with mapping annotations. Particularly, @Embeddable and if necessary @Column to map to the appropriate columns in the database.

@Embeddable
public class Address {

    private String city;
    private String pin;

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getPin() {
        return pin;
    }
    public void setPin(String pin) {
        this.pin = pin;
    }

}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189