2

私は休止状態が初めてです。Eclipse indigo の Hibernate 3.0 を使用します。

トピックはここで議論されており、答えは役に立ちませ .

何か不足していますか?誰でもこれを手伝ってもらえますか?

エラーは次のとおりです。

Feb 6, 2013 3:59:05 PM PatternsHome getSessionFactory
SEVERE: Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342)
at javax.naming.InitialContext.lookup(InitialContext.java:409)
at PatternsHome.getSessionFactory(PatternsHome.java:26)
at PatternsHome.<init>(PatternsHome.java:21)
at OutputProcessing.saveData(OutputProcessing.java:47)
at OutputProcessing.FPFileOutputWriter(OutputProcessing.java:110)
at OrderPatternFileCreate.main(OrderPatternFileCreate.java:84)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at PatternsHome.getSessionFactory(PatternsHome.java:29)
at PatternsHome.<init>(PatternsHome.java:21)
at OutputProcessing.saveData(OutputProcessing.java:47)
at OutputProcessing.FPFileOutputWriter(OutputProcessing.java:110)
at OrderPatternFileCreate.main(OrderPatternFileCreate.java:84)

ハイバネート構成ファイル:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <property name="show sql">true</property>
  <mapping resource="hibernate_db_mapping.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

DAO ファイルは Hibernate によって生成され、アウトラインは次のようになります。

public class PatternsHome {

private static final Log log = LogFactory.getLog(PatternsHome.class);

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
    try {
        return (SessionFactory) new InitialContext()
                .lookup("SessionFactory");
    } catch (Exception e) {
        log.error("Could not locate SessionFactory in JNDI", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory in JNDI");
    }
}
    .....
   }
4

1 に答える 1

0

スタンドアロンでこの作業を行うことはできません。しかし、HibernateUtil でジェネリック DAO を作成し、sessionFactory を使用して作成しました

 sessionFactory = new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory();

を使用してDAOのDBにアクセスしました

Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try{
        transaction = session.beginTransaction();
        session.save(myData);
        transaction.commit();
        System.out.println("Data is Saved");
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        session.close();
    }

これはうまくいきました。

于 2013-03-05T17:33:19.443 に答える