0

I know you'll be mad at me.but I'll ask anyway. i tried to every solution in stackoverflow for problem.but unresolved. i generate to hibernate dao pojos and hbm.xml.and when i try to add something with dao,i am getting error "Could not locate SessionFactory in JNDI".***

KullanicilarHome.java

public class KullanicilarHome {

    private static final Log log = LogFactory.getLog(KullanicilarHome.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");
        }
    }

    public void persist(Kullanicilar transientInstance) {
        log.debug("persisting Kullanicilar instance");
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

and MainClass.java (test)

public class MainClass {
    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Kullanicilar user = new Kullanicilar();
        user.setAd("Ergin");
        user.setSoyad("DURAN");
        user.setUniversite("Kxxx");
        user.setBolum("bxx");
        user.setCepTel("5xxxxxx");
        user.setEmail("exxxx");
        user.setVeliTel("55xxxxx");
        KullanicilarHome x = new KullanicilarHome();
        x.persist(user);

    }

and hbm.cfg.xml

<hibernate-configuration>
    <session-factory name="SessionFactory">
        <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:3306/db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.session_factory_name">SessionFactory</property>
    </session-factory>
</hibernate-configuration>

and tomcat version: v7.0.50 i say again Before you start to get angry. i tried to all solutions in stackoverflow.but did not work. ->enter link description here ->enter link description here ->enter link description here ->enter link description here

have a same problem and there solutions did not help me or did not understand there solutions. help me please.. I apologize for my bad english thanks***

Community
  • 1
  • 1
erginduran
  • 1,678
  • 5
  • 28
  • 51

1 Answers1

0

I think you are confused between ejb and hibernate, I do not see where you are trying to set the sessionfactory into the JNDI. You have to do the following

public class KullanicilarHome {

private static final Log log = LogFactory.getLog(KullanicilarHome.class);
private final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
.
.
.
.

Note: Make sure you have the .property file in the class path. Usually I put it in the WEB-INF folder.

Ref: link

Zeus
  • 6,386
  • 6
  • 54
  • 89
  • Thank you for your answer. I do what they say.property and hbm.cfg.xml files on same directory. src>main>java/mainclass & hibernate.properties & hibernate.cfg.xml and I am getting an error like: http://codepaste.net/q3xyzt – erginduran Apr 24 '14 at 20:30
  • If i put the files(properties and hbm.cfg.xml) into the web-inf,I'm getting "hibernate.cfg.xml" not found error – erginduran Apr 24 '14 at 20:32
  • http://stackoverflow.com/questions/18736594/location-of-hibernate-cfg-xml-in-project – Zeus Apr 24 '14 at 21:15
  • thanks a lot.i can did it in the end.but in addition I had to do for each blabla.hbm.xml to add hibernate.cfg.xml and its very tiring. thanks again – erginduran Apr 24 '14 at 22:45