Kliver Max
Kliver Max

Reputation: 5299

Hibernate not see a mapping xml file

im new in hibernate and try to write data in DB. my code

hibernate.cfg.xml

<?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="postsess">
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgis</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.default_schema">public</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
            <!-- Mapping files -->
    <mapping resource="net.sf.hibernate.examples.quickstart.Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>

HibernateUtin

package net.sf.hibernate.examples.quickstart;

 import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import net.sf.hibernate.*;
//import net.sf.hibernate.examples.quickstart.hibernate.cfg.xml;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (HibernateException ex) {
        throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
    }
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
        s = sessionFactory.openSession();
        session.set(s);
    }
    return s;
}

public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
        s.close();
}
}

And get an exeption

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.sf.hibernate.examples.quickstart.hib_main.main(hib_main.java:14)
Caused by: java.lang.RuntimeException: Configuration problem: Resource: net.sf.hibernate.examples.quickstart.Cat.hbm.xml not found
at net.sf.hibernate.examples.quickstart.HibernateUtil.<clinit>  (HibernateUtil.java:20)
... 1 more
 Caused by: org.hibernate.MappingException: Resource: net.sf.hibernate.examples.quickstart.Cat.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:444)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1313)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1285)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1267)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1234)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1162)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1148)
at net.sf.hibernate.examples.quickstart.HibernateUtil.<clinit>(HibernateUtil.java:18)
... 1 more

What im doing wrong. It a second try to execute simple example and again same exeption.

UPDATE

after changing path to mapping file in hibernate.cfg.xml i get another exeption in line session.save(princess);

 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for  this class must be manually assigned before calling save(): net.sf.hibernate.examples.quickstart.Cat
at org.hibernate.id.Assigned.generate(Assigned.java:32)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:477)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:472)
at net.sf.hibernate.examples.quickstart.hib_main.main(hib_main.java:23)

Cat.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15.08.2012 12:46:22 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
    <id name="id" type="java.lang.String">
        <column name="cat_id" />
        <generator class="assigned" />
    </id>
    <property name="name" type="java.lang.String">
        <column name="name" />
    </property>
    <property name="sex" type="char">
        <column name="sex" />
    </property>
    <property name="weight" type="float">
        <column name="weight" />
    </property>
</class>
</hibernate-mapping>

and main class.

package net.sf.hibernate.examples.quickstart;

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

public class hib_main
{

/**
 * @param args
 */
public static void main(String[] args)
{
    Session session = HibernateUtil.currentSession();

    Transaction tx= session.beginTransaction();

    Cat princess = new Cat();
    princess.setName("Princess");
    princess.setSex('F');
    princess.setWeight(7.4f);

    session.save(princess);
    tx.commit();

    HibernateUtil.closeSession();

}

}

Upvotes: 0

Views: 6856

Answers (4)

dimas
dimas

Reputation: 6073

Try to define your mapping resource in such way (using "/" instead of "."):

<mapping resource="net/sf/hibernate/examples/quickstart/Cat.hbm.xml"/>

I think this is a reason of your problem.

UPDATE:

About your second problem - I think that you forgot to define id generation strategy in the Cat.hbm.sql. For example:

<id name="id" column="CREDIT_CARD_ID" type="long">
    <generator class="native"></generator>
</id>

UPDATE 2:

Hibernate uses it's own types. So in the declaration of id type yous should use not java.lang.String or java.lang.Integer, but simple string and integer. For example if you want to use string id you should define it as:

<id name="id" column="cat_id" type="integer">
    <generator class="native"/>
</id>

Here you can find the list of hibernate's mapping types.

Upvotes: 6

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

You have two hibernate.cfg.xml.So remove one hibernate.cfg.xml.Try to test with below structure.And resource mapping is not trouble to you.You can declare only like this

       <mapping resource="Cat.hbm.xml"/>

enter image description here

Upvotes: 2

HRgiger
HRgiger

Reputation: 2790

I would rename Cat.hbm.xml as Cat_hbm_xml as each '.' referring to a sub package.

Upvotes: 0

Jaanus
Jaanus

Reputation: 16561

You should use / infront of path:

<mapping resource="/net/sf/hibernate/examples/quickstart/Cat.hbm.xml"/>

Upvotes: 1

Related Questions