Hello,
I am new to Hibernate. I am developing a very simple Java Hibernate Application. Below are my configurations of laptop:
1. Windows 7 64 bit
2. JDK 6
3. Eclipse Indigo
4. Hibernate 3.6.10
5. Oracle 11g Database
I have developed 3 tables in Oracle 11g Database named: USERDETAILS, VEHICLE AND ADDRESS.
Userdetails: userid, username
Vehicle: vehicleid, vehiclename, userid
Address: userid, street, city, state, country, pincode
i have 2 sequences named USERDETAILS_seq and VEHICLE_sed for primary key generation userid(userdetails) and vehicleid(vehicle)
ER Diagram
My logic is Userdetails can have many different vehicles and also per user one address is there. Below are the details
I have developed 3 entity classes as below:
UserDetails.javaCode:
package org.dto;
import java.util.Set;
public class UserDetails {
private int userID;
private String userName;
//One User can have multiple vehicles. therefore set
private Set<Vehicle> vehciles;
//one to one mapping between UserDetails and Address
private Address address;
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;
}
public Set<Vehicle> getVehciles() {
return vehciles;
}
public void setVehciles(Set<Vehicle> vehciles) {
this.vehciles = vehciles;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Vehicle.javaCode:
package org.dto;
public class Vehicle {
private int vehicleID;
private String vehicleName;
//Many vehicles can have one User
private UserDetails user;
public int getVehicleID() {
return vehicleID;
}
public void setVehicleID(int vehicleID) {
this.vehicleID = vehicleID;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
}
Address.javaCode:
package org.dto;
public class Address {
private int userID;
private String street;
private String city;
private String state;
private String country;
private String pincode;
// One to one between UserDetails and Address
private UserDetails user;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
}
hibernate.cfg.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">gaurav</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">vaildate</property>
<mapping resource="org/dto/mappings.hbm.xml" />
</session-factory>
</hibernate-configuration>
mappings.hbm.xmlCode:
<?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>
<!-- UserDetails -->
<class name="org.dto.UserDetails" table="USERDETAILS">
<id name="userID" column="USERID">
<generator class="sequence">
<param name="sequence">USERDETAILS_SEQ</param>
</generator>
</id>
<property name="userName" column="USERNAME" />
<set name="vehicles" table="VEHICLE" inverse="true" lazy="true"
fetch="select">
<key>
<column name="VEHICLEID" not-null="true" />
</key>
<one-to-many class="
org.dto.Vehicle " />
</set>
<one-to-one name="address" class="org.dto.Address" cascade="save-update" />
</class>
<!-- Address -->
<class name="org.dto.Address" table="ADDRESS">
<id name="userID" column="USERID">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="street" column="STREET" />
<property name="city" column="CITY" />
<property name="state" column="STATE" />
<property name="country" column="COUNTRY" />
<property name="pincode" column="PINCODE" />
<one-to-one name="user" class="org.dto.UserDetails"
constrained="true" />
</class>
<!-- Vehicle -->
<class name="org.dto.Vehicle" table="VEHICLE">
<id name="vehicleID" column="VEHICLEID">
<generator class="sequence">
<param name="sequence">VEHICLE_SEQ</param>
</generator>
</id>
<property name="vehicleName" column="VEHICLENAME" />
<many-to-one name="user" class="org.dto.UserDetails"
fetch="select">
<column name="VEHICLEID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
HibernateInsert.java----------to insert recordsCode:
package org.dto.hibernate;
import org.dto.Address;
import org.dto.UserDetails;
import org.dto.Vehicle;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateInsert {
public static void main(String[] args) {
UserDetails user1 = new UserDetails();
user1.setUserName("Gaurav");
Address address1 = new Address();
address1.setStreet("3/602, Sarvodaya Garden");
address1.setCity("Dombivli");
address1.setState("Maharashtra");
address1.setCountry("India");
address1.setPincode("421201");
Vehicle vehicle1 = new Vehicle();
vehicle1.setVehicleName("BMW");
vehicle1.setUser(user1);
user1.setAddress(address1);
address1.setUser(user1);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(address1);
session.save(vehicle1);
session.save(user1);
session.getTransaction().commit();
}
}
But after running the application i am getting below error
Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: org.dto.Vehicle
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2503)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2782)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at org.dto.hibernate.HibernateInsert.main(HibernateInsert.java:30)
Please help me out with this. What exactly needs to be done?