[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 791
  • Last Modified:

HibernateExample Issues

I was trying following hibernate example.

UserDetails.java looks like below

package org.gp.gpr.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

//import org.hibernate.annotations.Entity;

public class UserDetails {
      private int userId;
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;
private String userName;

hibernate.cfg.xml looks like below

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

  <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
  <property name="hibernate.connection.username">postgres</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibersnate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">create</property>
  <mapping class="org.gp.gpr.dto.UserDetails"/>  

hibernate test class looks like below

package org.gp.gpr.hibernate;

import org.gp.gpr.dto.UserDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {
public static void main(String[] args){
      UserDetails user=new UserDetails();
      user.setUserName("first User");
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();

When I run I am gettint error as below

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.MappingException: invalid configuration
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
      at org.gp.gpr.hibernate.HibernateTest.main(HibernateTest.java:14)
Caused by: org.xml.sax.SAXParseException: Element type "mapping-class" must be declared.
      at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
      at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
      at org.dom4j.io.SAXReader.read(SAXReader.java:465)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
      ... 3 more

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
  • 4
  • 3
  • 2
  • +1
1 Solution
Sathish David Kumar NArchitectCommented:
Its Config file problem . As per i am not able to find any bug .

Which database your using ?
Sathish David Kumar NArchitectCommented:
As per your coding is correct but .

System not able to load DB or mapping class ? thats is the problem
chaitu chaituCommented:
In your DOCTYPE statement, you have reference to hibernate-mapping-3.0.dtd. This .dtd is part of hibernate.jar file. You application looks for hibernate.jar in CLASSPATH. Make sure that the hibernate.jar is included in the project and it has the hibernate-mapping-3.0.dtd in it
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Sharon SethCommented:
Caused by: org.xml.sax.SAXParseException: Element type "mapping-class" must be declared.

Why is it looking for mapping-class when you have declared <mapping class/> ?
gudii9Author Commented:


 <mapping class="org.gp.gpr.dto.UserDetails"/>  

what is difference between above 3.

SEcond statement seems worked fine
Sharon SethCommented:
Note the missing hyphen . Your DTD is expecting <mapping-class> while your config had <mapping class> . In situations like this , it helps to check  the DTD . It would show wwhat are the permissible elements and values for the xml

<mapping-class> is the tag .
<mapping class>  -  mapping is a tag and class is an element in that tag

The below two are same , it's only different in the way the tags are closed
gudii9Author Commented:
>>> Your DTD is expecting <mapping-class> while your config had <mapping class> . In situations like this , it helps to check  the DTD . It would show wwhat are the permissible elements and values for the xml

<mapping-class> is the tag .
<mapping class>  -  mapping is a tag and class is an element in that tag

How to and where to check in DTD what it is expecting. I opened DTD as below. I do not see anything with name <mapping-class> or mapping class

<!-- Hibernate file-based configuration document.

<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

An instance of this document contains property settings and references
to mapping files for a number of SessionFactory instances to be listed
in JNDI.


<!ELEMENT hibernate-configuration (session-factory,security?)>

<!ELEMENT property (#PCDATA)>
<!ATTLIST property name CDATA #REQUIRED>

<!ELEMENT mapping EMPTY> <!-- reference to a mapping file -->
<!ATTLIST mapping resource CDATA #IMPLIED>
<!ATTLIST mapping file CDATA #IMPLIED>
<!ATTLIST mapping package CDATA #IMPLIED>
<!ATTLIST mapping class CDATA #IMPLIED>

<!ELEMENT class-cache EMPTY>
<!ATTLIST class-cache class CDATA #REQUIRED>
<!ATTLIST class-cache region CDATA #IMPLIED>
<!ATTLIST class-cache usage (read-only|read-write|nonstrict-read-write|transactional) #REQUIRED>
<!ATTLIST class-cache include (all|non-lazy) "all">

<!ELEMENT collection-cache EMPTY>
<!ATTLIST collection-cache collection CDATA #REQUIRED>
<!ATTLIST collection-cache region CDATA #IMPLIED>
<!ATTLIST collection-cache usage (read-only|read-write|nonstrict-read-write|transactional) #REQUIRED>

<!ELEMENT event (listener*)>
<!ATTLIST event type (auto-flush|merge|create|create-onflush|delete|dirty-check|evict|flush|flush-entity|load|load-collection|lock|refresh|replicate|save-update|save|update|pre-load|pre-update|pre-insert|pre-delete|pre-collection-recreate|pre-collection-remove|pre-collection-update|post-load|post-update|post-insert|post-delete|post-collection-recreate|post-collection-remove|post-collection-update|post-commit-update|post-commit-insert|post-commit-delete) #REQUIRED>

<!ELEMENT listener EMPTY>
<!ATTLIST listener type (auto-flush|merge|create|create-onflush|delete|dirty-check|evict|flush|flush-entity|load|load-collection|lock|refresh|replicate|save-update|save|update|pre-load|pre-update|pre-insert|pre-delete|pre-collection-recreate|pre-collection-remove|pre-collection-update|post-load|post-update|post-insert|post-delete|post-collection-recreate|post-collection-remove|post-collection-update|post-commit-update|post-commit-insert|post-commit-delete) #IMPLIED>
<!ATTLIST listener class CDATA #REQUIRED>

<!ELEMENT session-factory (property*, mapping*, (class-cache|collection-cache)*, event*, listener*)>
<!ATTLIST session-factory name CDATA #IMPLIED> <!-- the JNDI name -->

<!ELEMENT security (grant*)>
<!ATTLIST security context CDATA #REQUIRED> <!--the JACC contextID-->

<!ATTLIST grant entity-name CDATA #REQUIRED>
<!ATTLIST grant actions CDATA #REQUIRED>

Sharon SethCommented:
Your app does not seem to be using this DTD , but picking up some other DTD
gudii9Author Commented:
How to find which DTD it is looking at. Please advise
Sharon SethCommented:
I don't know of a straight fwd way , but I would go by cutting down access to each DTD (web,local) and relaunching the app until the app complains . This way I would get to know which DTD is being used .
You can also force the app to use a local  DTD , smthng like this:
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   "/hibernate-configuration-2.0.dtd">

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now