-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Exception in thread "main" org.hibernate.MappingEx
PostPosted: Fri Feb 20, 2009 2:25 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
Hello i am new to hybernate. . .
i have created a sample project ..

Employee.java
Code:
package com.mycompany.HybernateTry7;



import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
   public Employee() {

   }
   @Id
   @Column(name = "id")
   Integer id;

   @Column(name = "name")
   String name;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}


HybernateUtil.java

Code:
package com.mycompany.HybernateTry7;



import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   static {
      try {
         // Create the SessionFactory from hibernate.cfg.xml
         sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
      } catch (Throwable ex) {
         // Make sure you log the exception, as it might be swallowed
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
}



Example1.java

Code:
package com.mycompany.HybernateTry7;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Example1 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        /** Getting the Session Factory and session */
        SessionFactory session = HibernateUtil.getSessionFactory();
        Session sess = session.getCurrentSession();
        /** Starting the Transaction */
        Transaction tx = sess.beginTransaction();
        /** Creating Pojo */
        Employee pojo = new Employee();
        pojo.setId(new Integer(5));
        pojo.setName("XYZ");
        /** Saving POJO */
        sess.save(pojo);
        /** Commiting the changes */
        tx.commit();
        System.out.println("Record Inserted");
        /** Closing Session */
        session.close();
    }
}


Hybernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="session1">
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.1.200.32:1521:orcl</property>
        <property name="hibernate.connection.username">XXXX</property>
        <property name="hibernate.connection.password">XXXX</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>


i am getting following exception

Code:
WARNING: You are running embedded Maven builds, some build may fail due to incompatibilities with latest Maven release.
         To set Maven instance to use for building, click here.
Scanning for projects...
[#process-resources]
[resources:resources]
Using default encoding to copy filtered resources.
[#compile]
[compiler:compile]
Compiling 2 source files to /home/jjoshi/NetBeansProjects/HybernateTry7/target/classes
[exec:exec]
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mycompany.HybernateTry7.Employee
        at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
        at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:96)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
        at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
        at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
        at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
        at $Proxy0.save(Unknown Source)
        at com.mycompany.HybernateTry7.Example1.main(Example1.java:23)
[ERROR]The following mojo encountered an error while executing:
[ERROR]Group-Id: org.codehaus.mojo
[ERROR]Artifact-Id: exec-maven-plugin
[ERROR]Version: 1.1
[ERROR]Mojo: exec
[ERROR]brought in via: Direct invocation
[ERROR]While building project:
[ERROR]Group-Id: com.mycompany
[ERROR]Artifact-Id: HybernateTry7
[ERROR]Version: 1.0-SNAPSHOT
[ERROR]From file: /home/jjoshi/NetBeansProjects/HybernateTry7/pom.xml
[ERROR]Reason: Result of /bin/sh -c cd /home/jjoshi/NetBeansProjects/HybernateTry7 && /usr/lib/jvm/java-6-sun/bin/java -classpath /home/jjoshi/NetBeansProjects/HybernateTry7/target/classes:/home/jjoshi/.m2/repository/org/slf4j/slf4j-log4j12/1.5.6/slf4j-log4j12-1.5.6.jar:/home/jjoshi/.m2/repository/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar:/home/jjoshi/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar:/home/jjoshi/.m2/repository/hibernate-commons-annotations/hibernate-commons-annotations/3.0.0.GA/hibernate-commons-annotations-3.0.0.GA.jar:/home/jjoshi/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.jar:/home/jjoshi/.m2/repository/org/hibernate/hibernate/3.2.1.ga/hibernate-3.2.1.ga.jar:/home/jjoshi/.m2/repository/net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar:/home/jjoshi/.m2/repository/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar:/home/jjoshi/.m2/repository/commons-collections/commons-collections/2.1.1/commons-collections-2.1.1.jar:/home/jjoshi/.m2/repository/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar:/home/jjoshi/.m2/repository/asm/asm-attrs/1.5.3/asm-attrs-1.5.3.jar:/home/jjoshi/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/home/jjoshi/.m2/repository/antlr/antlr/2.7.6/antlr-2.7.6.jar:/home/jjoshi/.m2/repository/cglib/cglib/2.1_3/cglib-2.1_3.jar:/home/jjoshi/.m2/repository/asm/asm/1.5.3/asm-1.5.3.jar:/home/jjoshi/.m2/repository/org/hibernate/hibernate-annotations/3.2.0.ga/hibernate-annotations-3.2.0.ga.jar:/home/jjoshi/.m2/repository/javax/persistence/persistence-api/1.0/persistence-api-1.0.jar com.mycompany.HybernateTry7.Example1 execution is: '1'.
------------------------------------------------------------------------
For more information, run with the -e flag
------------------------------------------------------------------------
BUILD FAILED
------------------------------------------------------------------------
Total time: 16 seconds
Finished at: Fri Feb 20 11:45:07 IST 2009
Final Memory: 119M/198M
------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 6:41 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Add one more line in Hybernate.cfg.xml
Code:
<mapping class="com.mycompany.HybernateTry7. Employee"/>


I think you are using Oracle as Database server, Then change the value of "hibernate.dialect" from
Quote:
org.hibernate.dialect.DerbyDialect
to
Quote:
org.hibernate.dialect.Oracle9Dialect


Top
 Profile  
 
 Post subject: new exception
PostPosted: Fri Feb 20, 2009 7:07 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
now i have changed that Maven project into simple java application. . .

config.xml file is as follows rest of the code is same

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@192.1.200.32:1521:orcl</property>
    <property name="hibernate.connection.username">****</property>
    <property name="hibernate.connection.password">******</property>
      <property name="current_session_context_class">thread</property>
      <mapping class="javaapplication42.Employee"/>
  </session-factory>
</hibernate-configuration>



Exception i am getting is





Code:
0 [main] INFO annotations.Version - Hibernate Annotations 3.4.0.GA
29 [main] INFO cfg.Environment - Hibernate 3.2.3
35 [main] INFO cfg.Environment - hibernate.properties not found
43 [main] INFO cfg.Environment - Bytecode provider name : cglib
50 [main] INFO cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
133 [main] INFO common.Version - Hibernate Commons Annotations 3.1.0.GA
139 [main] INFO cfg.Configuration - configuring from resource: /hibernate.cfg.xml
141 [main] INFO cfg.Configuration - Configuration resource: /hibernate.cfg.xml
283 [main] INFO cfg.Configuration - Configured SessionFactory: null
296 [main] INFO search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
348 [main] INFO cfg.AnnotationConfiguration - Hibernate Validator not found: ignoring
357 [main] INFO connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
358 [main] INFO connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
359 [main] INFO connection.DriverManagerConnectionProvider - autocommit mode: false
384 [main] INFO connection.DriverManagerConnectionProvider - using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@192.1.200.32:1521:orcl
385 [main] INFO connection.DriverManagerConnectionProvider - connection properties: {user=student, password=****}
3358 [main] INFO cfg.SettingsFactory - RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
3363 [main] INFO cfg.SettingsFactory - JDBC driver: Oracle JDBC driver, version: 10.2.0.1.0XE
3385 [main] INFO dialect.Dialect - Using dialect: org.hibernate.dialect.OracleDialect
3435 [main] INFO transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
3440 [main] INFO transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
3442 [main] INFO cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
3443 [main] INFO cfg.SettingsFactory - Automatic session close at end of transaction: disabled
3443 [main] INFO cfg.SettingsFactory - JDBC batch size: 15
3444 [main] INFO cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
3446 [main] INFO cfg.SettingsFactory - Scrollable result sets: enabled
3447 [main] INFO cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
3448 [main] INFO cfg.SettingsFactory - Connection release mode: auto
3450 [main] INFO cfg.SettingsFactory - Default batch fetch size: 1
3451 [main] INFO cfg.SettingsFactory - Generate SQL with comments: disabled
3452 [main] INFO cfg.SettingsFactory - Order SQL updates by primary key: disabled
3453 [main] INFO cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
3457 [main] INFO ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
3458 [main] INFO cfg.SettingsFactory - Query language substitutions: {}
3459 [main] INFO cfg.SettingsFactory - JPA-QL strict compliance: disabled
3460 [main] INFO cfg.SettingsFactory - Second-level cache: enabled
3460 [main] INFO cfg.SettingsFactory - Query cache: disabled
3461 [main] INFO cfg.SettingsFactory - Cache provider: org.hibernate.cache.NoCacheProvider
3463 [main] INFO cfg.SettingsFactory - Optimize cache for minimal puts: disabled
3464 [main] INFO cfg.SettingsFactory - Structured second-level cache entries: disabled
3472 [main] INFO cfg.SettingsFactory - Statistics: disabled
3473 [main] INFO cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
3474 [main] INFO cfg.SettingsFactory - Default entity-mode: pojo
3475 [main] INFO cfg.SettingsFactory - Named query checking : enabled
6360 [main] INFO impl.SessionFactoryImpl - building session factory
6374 [main] INFO impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.MappingException: Unknown entity: javaapplication42.Employee
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
at javaapplication42.Example1.main(Example1.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
Code:
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 7:15 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
Do you have a mapping file for javaapplication42.Employee?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 9:58 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
I think your AnnotationConfiguration needs to be told where to look for annotated classes, which the mapping in your XML probably should have achieved.

You could try removing that from the XML and doing it programmatically instead by restructuring your HibernateUtil setup slightly, along the lines of:

AnnotationConfiguration config = new AnnotationConfiguration();
config.addClass(javaapplication42.Employee.class);
sessionFactory = config.buildSessionFactory();


--
Stephen Souness


Top
 Profile  
 
 Post subject: Re: Exception in thread "main" org.hibernate.MappingEx
PostPosted: Mon Aug 01, 2011 9:43 pm 
Newbie

Joined: Mon Aug 01, 2011 9:39 pm
Posts: 1
I got the same error i simply solved it by adding <mapping resource="com/hiber/annot/Employee.hbm.xml"/> in cfg.xml file


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.