Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • 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;

@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
gudii9
Asked:
gudii9
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Sathish David Kumar NCommented:
Its Config file problem . As per i am not able to find any bug .

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

System not able to load DB or mapping class ? thats is the problem
0
 
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
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!

 
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/> ?
0
 
gudii9Author Commented:
<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
 
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
<mapping-class>"org.gp.gpr.dto.UserDetails"</mapping-class>  
 <mapping-class="org.gp.gpr.dto.UserDetails"/>
0
 
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"
      "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
 
Sharon SethCommented:
Your app does not seem to be using this DTD , but picking up some other DTD
0
 
gudii9Author Commented:
How to find which DTD it is looking at. Please advise
0
 
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">
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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