Solved

HibernateExample Issues

Posted on 2013-02-06
11
733 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

728 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