Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Joins using Hiberntes

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Now I'm in the learning stage of hibernates. Now I'm learning Association and Mapping I wrote a program, while try to compile the program it is throwing exceptions My code is following

Product.java
------------

package com.mapping;

public class Product {

private int id;
private int did;
private String name;
private double price;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}


Dealer.java
-----------
package com.mapping;

public class Dealer {
private int id;
private String name;
private int did;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

JoinExample.java
----------------

package com.mapping;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.HibernateSessionFactory;

public class JoinExample {
public static void main(String args[]){

Session session=null;

try{

session = HibernateSessionFactory.getInstance().getCurrentSession();
session.beginTransaction();
String sql_query = "from Product p inner join p.dealer as d";
Query query = session.createQuery(sql_query);
Iterator ite = query.list().iterator();
System.out.println("Dealer Name\t"+"Product Name\t"+"Price");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
Product pro = (Product) pair[0];
Dealer dea = (Dealer) pair[1];
System.out.print(pro.getName());
System.out.print("\t"+dea.getName());
System.out.print("\t\t"+pro.getPrice());
System.out.println();
}
session.close();

}catch(Exception e){
e.printStackTrace();
}

}

}

Product.hbm.xml
--------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/
hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.mapping.Product" table="product">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment"/>
</id>

<property name="name" type="java.lang.String" column="name"/>
<property name="did" type="java.lang.Integer" column="did"/>
<property name="price" type="java.lang.Double" column="price"/>
<many-to-one name="dealer" class="com.mapping.Dealer" column="did" insert="false" update="false"/>

</class>

</hibernate-mapping>

Dealer.hbm.xml
--------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/
hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mapping.Dealer" table="dealer">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment"/>
</id>

<property name="name" type="java.lang.String" column="name"/>
<property name="did" type="java.lang.Integer" column="did"/>
<bag name="product" inverse="true" cascade="all,delete-orphan">
<key column="did"/>
<one-to-many class="com.mapping.Product"/>
</bag>
</class>
</hibernate-mapping>

hibernate.cfg.xml
-------------------
<?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/mapping</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">update</property>
<!-- Mapping files -->
<mapping resource="com/mapping/Dealer.hbm.xml"/>
<mapping resource="com/mapping/Product.hbm.xml"/>
</session-factory>
</hibernate-configuration>

While run the Join Example Program it shows the following error

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
%%%% Error Creating HibernateSessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/mapping/Dealer.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:575)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at com.HibernateSessionFactory.initSessionFactory(HibernateSessionFactory.java:39)
at com.HibernateSessionFactory.getInstance(HibernateSessionFactory.java:23)
at com.mapping.JoinExample.main(JoinExample.java:19)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:514)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:572)
... 8 more
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505)
... 9 more
org.hibernate.HibernateException: Could not initialize the Hibernate configuration
at com.HibernateSessionFactory.initSessionFactory(HibernateSessionFactory.java:56)
at com.HibernateSessionFactory.getInstance(HibernateSessionFactory.java:23)
at com.mapping.JoinExample.main(JoinExample.java:19)

Can any one give me an idea to resolve this

regards,
Maheshwaran Devaraj
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please repost all that, but within CODE tags, there is a CODE button below that will create the tags. Without them the code is not indented and becomes difficult to read.

Thanks

Mark
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic