Hi,
I am relatively new to hibernate.
Trying to persist collections.
I have a class UserD.java which contains a collection of Phone.java.
But when i try to run the tester class, I get the following error:
Code:
java.lang.ExceptionInInitializerError
Caused by: org.hibernate.MappingException: Could not determine type for: com.pra.dto.collections.Phone, at table: UserD_phone, for columns: [org.hibernate.mapping.Column(phone)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
at org.hibernate.mapping.Collection.validate(Collection.java:313)
at org.hibernate.mapping.Set.validate(Set.java:42)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1336)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1835)
at base.SFCreator.<clinit>(SFCreator.java:9)
Exception in thread "main"
My Code:
The configuration file:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibtut</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Mapping files -->
<mapping class="com.pra.dto.collections.Phone"/>
<mapping class="com.pra.dto.collections.UserD"/>
</session-factory>
</hibernate-configuration>
UserD.javaCode:
package com.pra.dto.collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class UserD {
private int userID;
private String userName;
private Set<Phone> phone = new HashSet<Phone>();
@Id
@GeneratedValue
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;
}
@ElementCollection
public Set<Phone> getPhone() {
return phone;
}
public void setPhone(Set<Phone> phone) {
this.phone = phone;
}
}
Phone.javaCode:
package com.pra.dto.collections;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
public class Phone {
private int phoneID;
private String phoneNo;
@Id
@GeneratedValue
public int getPhoneID() {
return phoneID;
}
public void setPhoneID(int phoneID) {
this.phoneID = phoneID;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
Tester classCode:
package com.pra.dto.collections;
import org.hibernate.Session;
import base.SFCreator;
public class TestCollection extends SFCreator {
public static void main(String[] args) {
UserD detail = new UserD();
detail.setUserName("Praveen");
Phone p1 = new Phone();
p1.setPhoneNo("34343434");
Phone p2 = new Phone();
p2.setPhoneNo("34343434");
Phone p3 = new Phone();
p3.setPhoneNo("34343434");
Phone p4 = new Phone();
p4.setPhoneNo("34343434");
detail.getPhone().add(p1);
detail.getPhone().add(p2);
detail.getPhone().add(p3);
detail.getPhone().add(p4);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(detail);
session.getTransaction().commit();
session.close();
}
}
Thanks
Praveen