Solved

HibernateExample Issues

Posted on 2013-02-06
11
724 Views
Last Modified: 2014-04-22
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;

@Entity
public class UserDetails {
@Id
      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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <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"/>  
</session-factory>
</hibernate-configuration>

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.setUserId(1);
      user.setUserName("first User");
      
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();
      session.beginTransaction();
      session.save(user);
      session.getTransaction().commit();
      
}
}





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
hibernateEx1.jpg
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 38862731
Its Config file problem . As per i am not able to find any bug .

Which database your using ?
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 38862732
As per your coding is correct but .

System not able to load DB or mapping class ? thats is the problem
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38862760
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38863101
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/> ?
0
 
LVL 7

Author Comment

by:gudii9
ID: 38873639
<mapping-class>"org.gp.gpr.dto.UserDetails"</mapping-class>  


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


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


what is difference between above 3.

SEcond statement seems worked fine
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38874910
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
<mapping-class>"org.gp.gpr.dto.UserDetails"</mapping-class>  
 <mapping-class="org.gp.gpr.dto.UserDetails"/>
0
 
LVL 7

Author Comment

by:gudii9
ID: 38878108
>>> 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"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

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 jar 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-->

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


0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38879269
Your app does not seem to be using this DTD , but picking up some other DTD
0
 
LVL 7

Author Comment

by:gudii9
ID: 38881212
How to find which DTD it is looking at. Please advise
0
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 500 total points
ID: 38883527
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">
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 84
Eclipse with gitlab 1 164
jsp insert to database example 2 64
Is there a simpler dropbox system? 10 44
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question