- This topic has 15 replies, 4 voices, and was last updated 20 years, 4 months ago by Riyad Kalla.
-
AuthorPosts
-
alexander malicMemberhi i’m new to eclipse and hibernate.
i did my first project using struts,hibernate and db-explorer.
i added the automated hibernateSessionFactory
hibernate cfg also seems to be ok, but when i test the application following line in HibernateSessionFactory throws an exception:——
…
cfg.configure(CONFIG_FILE_LOCATION);
…
——the config file is located in /HelloStruts/hibernate.cfg.xml
i’m using a jboss3.2.4 server / eclipse 3.0 / me 3.8b2
thanx in advance
alexander malicMemberseems also that the hibernate.cfg.xml is not deployed
when i look to the %jbosshome%/server/default/deploy/HelloStruts.war Directory there is no config file.
when i copy it manually i get the same error.
Riyad KallaMemberAlexander,
I believe this is related to an already filed bug dealing with Hibernate support expecting the cfg file in a certain location. Let me check on it.
alexander malicMemberwith tomcat it’s the same.
when i moved the hibernate.cfg.xml file to webroot-directory i got deployed (on jboss and tomcat) i can also view it via http://localhost:8080/HelloStruts/hibernate.cfg.xml . So the file ist definitelly there.here’s my hibernate.cfg.xml from webroot-dir
—————————————————————————————————
<?xml version=’1.0′ encoding=’UTF-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 2.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd”><hibernate-configuration>
<session-factory>
<!– properties –>
<property name=”connection.username”>root</property>
<property name=”connection.url”>jdbc:mysql://localhost:3306/javadevelopment</property>
<property name=”dialect”>net.sf.hibernate.dialect.MySQLDialect</property>
<property name=”connection.password”>*********</property>
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property><!– mapping files –>
<mapping resource=”Users.hbm.xml”/></session-factory>
</hibernate-configuration>
—————————————————————————————————the HibernateSessionFactory.java from package com.workflow.hellostruts.model located in src-dir
————————————————————————————————–
package com.workflow.hellostruts.model;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {/** Location of hibernate.cfg.xml file */
private static String CONFIG_FILE_LOCATION = “/HelloStruts/hibernate.cfg.xml”;/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();/** The single instance of hibernate SessionFactory */
private static SessionFactory sessionFactory;/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println(“%%%% Error Creating SessionFactory %%%%”);
System.err.println(e.getMessage());
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}return session;
}/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);if (session != null) {
session.close();
}
}/**
* Default constructor.
*/
private HibernateSessionFactory() {
}}
————————————————————————————————–Users.java located in com.workflow.hellostruts.model in src
————————————————————————————————–
package com.workflow.hellostruts.model;import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;/** @author Hibernate CodeGenerator */
public class Users implements Serializable {/** identifier field */
private long id;/** persistent field */
private String username;/** persistent field */
private String fullname;/** persistent field */
private String password;/** full constructor */
public Users(java.lang.String username, java.lang.String fullname, java.lang.String password) {
this.username = username;
this.fullname = fullname;
this.password = password;
}/** default constructor */
public Users() {
}public long getId() {
return this.id;
}public void setId(long id) {
this.id = id;
}public java.lang.String getUsername() {
return this.username;
}public void setUsername(java.lang.String username) {
this.username = username;
}public java.lang.String getFullname() {
return this.fullname;
}public void setFullname(java.lang.String fullname) {
this.fullname = fullname;
}public java.lang.String getPassword() {
return this.password;
}public void setPassword(java.lang.String password) {
this.password = password;
}public String toString() {
return new ToStringBuilder(this)
.append(“id”, getId())
.toString();
}public boolean equals(Object other) {
if ( !(other instanceof Users) ) return false;
Users castOther = (Users) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}}
————————————————————————————————–Users.hbm.xml located in com.workflow.hellostruts.model in src-dir
————————————————————————————————–
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 2.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd”>
<hibernate-mapping>
<class name=”com.workflow.hellostruts.model.Users” table=”users”>
<id name=”id” column=”id” type=”long”>
<generator class=”native”/>
</id>
<property name=”username” column=”username” type=”string” length=”50″ not-null=”true”/>
<property name=”fullname” column=”fullname” type=”string” length=”100″ not-null=”true”/>
<property name=”password” column=”password” type=”string” length=”50″ not-null=”true”/>
</class>
</hibernate-mapping>
————————————————————————————————–LoginAction.java in com.workflow.hellostruts.action
————————————————————————————————–
…
Session session=HibernateSessionFactory.currentSession();
Query query=session.createQuery(“select u from Users as u where u.username=’:username'”);
query.setString(“username”,username);Users user=null;
Iterator it=query.iterate();if (it.hasNext()){
user=(Users)it.next();
}else{
loginForm.setPassword(“”);
errors.add(“username”,new ActionError(“errors.usernotfound”));
saveErrors(request,errors);
return mapping.findForward(“failure”);
}
…
————————————————————————————————–i’m using the
hope this helps find the bug (maybe i did something wrong ???)
alexander malicMemberchanged the mapping-file path in hibernate.cfg.xml file to
————————————————————————————————–
<!– mapping files –>
<mapping resource=”/com/workflow/hellostruts/model/Users.hbm.xml”/>
————————————————————————————————–Also did some changes in project-properties/MyEclipse Hibernate
changed the Configuration-File-Path from
/hibernate.cfg.xml
to
/WebRoot/hibernate.cfg.xml
where i moved the file so it can be deployeddidn’t help anyway
need fast help
preparing a workshop for my company for tomorrow (it’s 1:24 am local time / need to go to sleep)
Scott AndersonParticipantAlexander,
We’ll do what we can to help but unfortunately some of the staff that handle the new persistance stuff are on vacation. I forwarded the URL along to one of the other devs to see if he can help.
alexander malicMemberthanx.
still working on my powerpoint-presentation
Riyad KallaMemberOn a side note, when you are giving this presentation you might want to note that its in Beta if anyone makes a big deal about ‘hibernate support not working right yet’. Hibernate support is very high on our priority list, so we will work with any users willing to work with us to iron this out. Thank you for your patience.
support-jeffMemberAlexander –
The SessionFactory wants the config file to be classpath-relative, so try the following:
(1) move hibernate.cfg.xml into the default package space of a source directory (anything that ends up putting the file in the WEB-INF/classes build directory
(2) change the project-properties/MyEclipse Hibernate Configuration-File-Path to:
hibernate.cfg.xmlThis should resolve the config file problem.
alexander malicMemberdidn’t work :/
i want to send you a screenshot.
just mail me a emailaddr. so i can send u one.greets
alexander malicMember@support-rkalla wrote:
On a side note, when you are giving this presentation you might want to note that its in Beta if anyone makes a big deal about ‘hibernate support not working right yet’. Hibernate support is very high on our priority list, so we will work with any users willing to work with us to iron this out. Thank you for your patience.
no problem,
they all know what beta means.
btw.: i can tell em, that it was my mistake 🙂
greets
alexander malicMemberanything that ends up putting the file in the WEB-INF/classes build
don’t understand this sentence (i’m from austria / u know arnold schwarzenegger 🙂 )
do you mean that i should put the file manually into the classes directory
then set the path in projectproperties to “hibernate.cfg.xml” and also set the path in the HibernateSessionFactory to “hibernate.cfg.xml” or “/hibernate.cfg.xml”
support-jeffMemberWhat is the source build bath for the HelloStruts project? Put hibernate.cfg.xml at the root of that path. The file will now automatically be copied to WEB-INF/classes dir upon rebuild by eclipse.
Yes, sorry, you also need to manually change the Session Factgory to use /hibernate.cfg.xml.
support-jeffMemberBTW, please post the execution logs here if this does not resolve the issue. Logs might include eclipse Console output, or standard out/err (or other server logs) if you are trying to run outside of eclipse.
alexander malicMemberi put it in the src folder, now deployment works fine.
thanx for your help.the presentation did well. i just didn’t redeploy the app 🙂
but now it’s no problem to redeploy it.
thanx for your help
you can close the thread.have a nice day
i’ll be back 8) -
AuthorPosts