Solved

HibernateExample Issues

Posted on 2013-02-06
11
675 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
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:Sharon Seth
Comment Utility
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
Comment Utility
<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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:Sharon Seth
Comment Utility
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
Comment Utility
>>> 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
Comment Utility
Your app does not seem to be using this DTD , but picking up some other DTD
0
 
LVL 7

Author Comment

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

Accepted Solution

by:
Sharon Seth earned 500 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
mapBully challenge 6 88
JDeveloper 12c for 32 bit 4 34
Error with Java/Cache JDBC Classpath 2 19
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now