-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate 3 Tools and SessionFactory
PostPosted: Sun Sep 11, 2005 12:14 am 
Newbie

Joined: Tue Jan 11, 2005 12:58 pm
Posts: 15
I am new to the tools and hibernate 3. I am using eclipse 3.1. I used the tools to generate my code and config files I created a class with a main to test the generated files and classes. the class is follows:

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
ClientHome chome = new ClientHome();
Client c = chome.findById("1234");
System.out.println(c.getClientname());

}

}

The following exception is thrown:
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at com.wasc.swifttest.home.ClientHome.getSessionFactory(ClientHome.java:30)
at com.wasc.swifttest.home.ClientHome.<init>(ClientHome.java:22)
at Test.main(Test.java:10)

I want to test it standalone firts and then use it in a web app.
What caused this and how can it be resolved?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 12:33 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
In you hibernate.cfg.xml file, you might have a name attribute for the
session-factory element. remove the name attribute.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject: SessionFactory
PostPosted: Sun Sep 11, 2005 9:49 am 
Newbie

Joined: Tue Jan 11, 2005 12:58 pm
Posts: 15
I do not have name attribute on the sessionfactory. The xml is listed below. Is it possible to set up a JNDI for it to use?

<?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.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/swiftcambio</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">swiftcambio</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/wasc/swiftcambio/Client.hbm.xml" />
<mapping resource="com/wasc/swiftcambio/ClientStatus.hbm.xml" />
<mapping resource="com/wasc/swiftcambio/ClientIdtype.hbm.xml" />
<mapping resource="com/wasc/swiftcambio/ClientCategory.hbm.xml" />
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 10:45 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
Can you post the ClientHome.java source code?

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject: here is the clienthome
PostPosted: Sun Sep 11, 2005 6:10 pm 
Newbie

Joined: Tue Jan 11, 2005 12:58 pm
Posts: 15
package com.wasc.swiftcambio.home;


import java.util.*;
import org.apache.commons.logging.*;
import javax.naming.InitialContext;
import org.hibernate.*;

import com.wasc.swiftcambio.Client;

import static org.hibernate.criterion.Example.*;

/**
* Home object for domain model class Client.
* @see com.wasc.swiftcambio.Client
* @author Hibernate Tools
*/
public class ClientHome {

private static final Log log = LogFactory.getLog(Client.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(Client transientInstance) {
log.debug("persisting Client instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
}
catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}

public void attachDirty(Client instance) {
log.debug("attaching dirty Client instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
}
catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Client instance) {
log.debug("attaching clean Client instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
}
catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void delete(Client persistentInstance) {
log.debug("deleting Client instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
}
catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Client merge(Client detachedInstance) {
log.debug("merging Client instance");
try {
Client result = (Client) sessionFactory.getCurrentSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
}
catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public Client findById(String id) {
log.debug("getting Client instance with id: " + id);
try {
Client instance = (Client) sessionFactory.getCurrentSession()
.get("com.wasc.swiftcambio.Client", id);
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(Client instance) {
log.debug("finding Client instance by example");
try {
List results = sessionFactory.getCurrentSession()
.createCriteria("com.wasc.swiftcambio.Client")
.add( create(instance) )
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
}
catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 10:43 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
Your session factory is not registered in your name service or name service is not up. Since you wanted to test it in standalone mode, you could simply programatically create sessionfactory instead of obtaining it from JNDI.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 9:56 pm 
Newbie

Joined: Tue Jan 11, 2005 12:58 pm
Posts: 15
could you post an example of this?


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