I'm very new in Hibernate. I can not fully understand the mappings of a association.
Problem:
I have 2 classes:
[*]User
[*]UserGroup
A user belongs to one group, but a group can include many users.
User class:
Code:
package ir.khorasancustoms.g2g.persistance;
public class User {
// <editor-fold defaultstate="collapsed" desc="FIELDS">
private long id;
private String username;
private String password;
private String email;
private String telNo;
private String mobileNo;
private String firstName;
private String lastName;
private UserGroup group;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="CONSTRUCTORS">
public User() {
}
public User(long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="GETTERS & SETTERS">
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public UserGroup getGroup() {
return group;
}
public void setGroup(UserGroup group) {
this.group = group;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelNo() {
return telNo;
}
public void setTelNo(String telNo) {
this.telNo = telNo;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// </editor-fold>
}
UserGroup Class:
Code:
package ir.khorasancustoms.g2g.persistance;
import java.util.HashSet;
import java.util.Set;
public class UserGroup {
// <editor-fold defaultstate="collapsed" desc="FIELDS">
private long id;
private String title;
private String description;
private Set<User> users = new HashSet<User>();
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="CONSTRUCTORS">
public UserGroup() {
}
public UserGroup(String title) {
this.title = title;
}
public UserGroup(long id, String title) {
this.id = id;
this.title = title;
}
public UserGroup(String title, String description) {
this.title = title;
this.description = description;
}
public UserGroup(long id, String title, String description) {
this.id = id;
this.title = title;
this.description = description;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="GETTERS & SETTERS">
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
// </editor-fold>
}
Mappings
User.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ir.khorasancustoms.g2g.persistance.User" table="TBL_USER">
<id column="USR_ID" type="big_integer" name="id">
<generator class="sequence"/>
</id>
<property name="username" type="string" column="USERNAME" length="50"/>
<property name="password" type="string" column="PASSWORD" length="200"/>
<property name="email" type="string" column="EMAIL" length="200"/>
<property name="telNo" type="string" column="TEL_NUM" length="50"/>
<property name="mobileNo" type="string" column="MOB_NUM" length="50"/>
<property name="firstName" type="string" column="FIRST_NAME" length="50"/>
<property name="lastName" type="string" column="LAST_NAME" length="50"/>
<many-to-one name="group" column="UGP_ID" not-null="true" class="UserGroup"/>
</class>
</hibernate-mapping>
UserGroup.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ir.khorasancustoms.g2g.persistance.UserGroup" table="TBL_USER_GROUP">
<id column="UGP_ID" name="id" type="big_integer">
<generator class="sequence"/>
</id>
<property column="GROUP_TITLE" length="50" name="title" type="string"/>
<property column="GROUP_DESC" length="250" name="description" type="string"/>
<set name="users">
<key column="UGP_ID" not-null="true"/>
<one-to-many class="User"/>
</set>
</class>
</hibernate-mapping>
And finally my test code:
Code:
import ir.khorasancustoms.g2g.persistance.UserGroup;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class Test {
public static void main(String[] args) {
SessionFactory factory = HibUtil.getSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
UserGroup userGroup = (UserGroup) session.load(UserGroup.class, 1);
session.getTransaction();
session.close();
}
}
and I end up with this error:
run:
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : ir/khorasancustoms/g2g/persistance/UserGroup.hbm.xml
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : ir/khorasancustoms/g2g/persistance/User.hbm.xml
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: ir.khorasancustoms.g2g.persistance.UserGroup -> TBL_USER_GROUP
Dec 16, 2010 11:13:44 AM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: ir.khorasancustoms.g2g.persistance.User -> TBL_USER
Initial SessionFactory creation failed.org.hibernate.MappingException: Association references unmapped class: User
Exception in thread "main" java.lang.ExceptionInInitializerError
at HibUtil.<clinit>(HibUtil.java:15)
at Test.main(Test.java:7)
Caused by: org.hibernate.MappingException: Association references unmapped class: User
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2380)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2662)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at HibUtil.<clinit>(HibUtil.java:12)
... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)