-->
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.  [ 3 posts ] 
Author Message
 Post subject: MappingNotFoundException
PostPosted: Mon Aug 16, 2010 10:13 am 
Newbie

Joined: Mon Aug 16, 2010 9:50 am
Posts: 2
Hi
I am an newbie to hibernate annotations.
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: course_new not found


course_new.java
Code:
package data;

import java.io.Serializable;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;



@Entity
@Table(name="course")
public class course_new implements  Serializable {
String id = UUID.randomUUID().toString();

    @Id
    @Column(name = "course_id", updatable = true, nullable = false, unique = true)
    private long courseId;

    @Column(name = "course_name", updatable = true, nullable = false, length = 255)
    private String courseName;



       
        public long getCourseId() {
        return this.courseId;
    }



    public void setCourseId(long courseId) {
        this.courseId = courseId;
    }

   

    public String getCourseName() {
        return this.courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
   

    }


HibernateUtil.java


Code:
package data;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;

    static
    {
        try
        {
            AnnotationConfiguration config = new AnnotationConfiguration();
            config.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
            config.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
            config.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/mydatabase");
            config.setProperty("hibernate.connection.username","root");
            config.setProperty("hibernate.connection.password","root");
            config.setProperty("hibernate.connection.pool_size","1");
            config.setProperty("hibernate.connection.autocommit","true");
            config.setProperty("hibernate.cache.provider_class","org.hibernate.cache.NoCacheProvider");
            config.setProperty("hibernate.hbm2ddl.auto","create-drop");
            config.setProperty("hibernate.show_sql","true");
            config.setProperty("hibernate.transaction.factory_class","org.hibernate.transaction.JDBCTransactionFactory");
            config.setProperty("hibernate.current_session_context_class","thread");
           
           config.addResource("course_new");
sessionFactory = config.buildSessionFactory();
        }
        catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

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



I added the below line in HibernateUtil.java
config.addResource("course_new");

rather than in xml file like

<!-- Mapping files -->
<mapping resource="course_new.hbm.xml" />
which I am not mapping using an XML file, doing it directly from HibernateUtil.java

but getting exception.
Folder structure is
NetBeansProjects\hibernate\src\data ------->all the source files
NetBeansProjects\hibernate\build\classes\data------->all the class files.

tried using slash and dot while class name in specifying config.addResourse(course_new), but of no use.

thanks
Mukunda


Top
 Profile  
 
 Post subject: Re: MappingNotFoundException
PostPosted: Mon Aug 16, 2010 10:22 am 
Beginner
Beginner

Joined: Tue Nov 03, 2009 9:38 am
Posts: 24
The annotations provide a default for the XML mapping, so you don't need to provide it.

You should be fine without that line.


Top
 Profile  
 
 Post subject: Re: MappingNotFoundException
PostPosted: Mon Aug 16, 2010 11:30 am 
Newbie

Joined: Mon Aug 16, 2010 9:50 am
Posts: 2
@ThomasGo
If I remove the said line I get exception as
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: course_new is not mapped [from course_new where course_id='1234']


Querydemo.java

to get the details from the table
Code:
package data;

import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class querydemo {
    public static void main(String args[]){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();
     
     String SQL_QUERY = "from course_new where course_id='1234'";
     Query query = session.createQuery(SQL_QUERY);
     
     for(Iterator it=query.iterate();it.hasNext();){
course_new cou=(course_new)it.next();
System.out.println("ID: " + cou.getCourseId());
   System.out.println("Course Name: " + cou.getCourseName());
     }
    session.close();
    }



}



regards
Mukunda


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.