Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use hibernate with .hbm.xml file instead of annotations?

 
ranjani ra
Greenhorn
Posts: 3
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a sample hibernate application using annotations and it runs fine. To call stored procedures, my pl says it s better to use <classname>.hbm.xml instead of annotations. However when I try to use the xml file for mapping column names to pojo elements, I am getting an exception:
Could not parse mapping document from resource Dsl_member.hbm.xml
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Dsl_member.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:602)
at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:957)
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:629)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1047)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at com.TestConnectCCR.main(TestConnectCCR.java:23)
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:783)
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:599)
... 15 more
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:773)
... 17 more
java.lang.NullPointerException
at com.TestConnectCCR.main(TestConnectCCR.java:50)

Table: CREATE TABLE DSL_MEMBER(ID INT NOT NULL, FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(20), STATUS VARCHAR2(2), SALARY INT); ID is the primary key.

Given below is the POJO Dsl_member with annotated table and column names
package com;

import java.io.Serializable;
import java.util.Date;

public class Dsl_member implements Serializable{

private int id;
private String firstName;
private String lastName;
private String status;
private int salary;
//getters and setters, cosntructors

My hibernate-cfg.xml:
<?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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- url, userid and pwd mappings for my db -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="com.Dsl_member"></mapping>
</session-factory>
</hibernate-configuration>

Dsl_member.hbm.xml file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Dsl_member" table="DSL_MEMBER">
<meta attribute="class-description">
This class contains the dsl_member detail.
</meta>
<id name="id" type="int" column="ID">
</id>
<property name="firstName" column="FIRST_NAME" type="String"/>
<property name="lastName" column="LAST_NAME" type="String"/>
<property name="status" column="STATUS" type="String"/>
<property name="salary" column="SALARY" type="int"/>
</class>
</hibernate-mapping>

Test class:
Session session = null;
try{
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction tx = null;
tx = session.beginTransaction();

//Create new instance of Contact and set

List<Dsl_member> userList=new ArrayList<Dsl_member>();
//select values from DSL_MEMBER table
userList=session.createQuery("from Dsl_member").list();
for(Dsl_member userEntity:userList){
//Print the values
}



 
udaykiran pulipati
Greenhorn
Posts: 12
Hibernate Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update your hibernate.cfg.xml file with below one

Your Dsl_member.hbm.xml file is not correctly mapped in hibernate.cfg.xml file.
use resource attribute for connect to database because in this file table is mentioned not in pogo class.

My hibernate-cfg.xml:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic