0

休止状態で新しく、DBにデータを書き込もうとしています。私のコード

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();
}
}

そして例外を取得します

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

私は何を間違っていますか。簡単な例をもう一度実行して、もう一度同じ例外を実行してみます。

アップデート

でマッピングファイルへのパスを変更した後、hibernate.cfg.xml別の例外が行に表示されます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>

そしてメインクラス。

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();

}

}
4

4 に答える 4

6

そのような方法でマッピング リソースを定義してみてください (「.」の代わりに「/」を使用):

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

これがあなたの問題の理由だと思います。

アップデート:

2 番目の問題について - Cat.hbm.sql で ID 生成戦略を定義するのを忘れたと思います。例えば:

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

更新 2:

Hibernate は独自の型を使用します。したがって、id 型の宣言では、java.lang.String や java.lang.Integer ではなく、単純な文字列と整数を使用する必要があります。たとえば、文字列 ID を使用する場合は、次のように定義する必要があります。

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

ここでは、hibernate のマッピング タイプのリストを見つけることができます。

于 2012-08-15T07:32:44.193 に答える
2

2 つの hibernate.cfg.xml があります。そのため、1 つの hibernate.cfg.xml を削除します。以下の構造でテストしてみてください。また、リソース マッピングは問題ではありません。このようにのみ宣言できます。

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

ここに画像の説明を入力

于 2012-08-15T07:46:47.307 に答える
1

/パスの前に使用する必要があります。

<mapping resource="/net/sf/hibernate/examples/quickstart/Cat.hbm.xml"/>
于 2012-08-15T07:34:40.357 に答える
0

Cat.hbm.xmlの名前をそれぞれCat_hbm_xmlに変更します。'サブ パッケージを 参照.

于 2012-08-15T07:38:11.717 に答える