-->
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.  [ 6 posts ] 
Author Message
 Post subject: SessionFactory not getting created when using many-to-one
PostPosted: Mon Oct 11, 2004 9:04 pm 
Newbie

Joined: Mon Oct 11, 2004 8:57 pm
Posts: 5
Location: Studio City, CA, USA
I can't figure this out, and am hoping it's just a stupid beginner issue...

I am using hibernate with tomcat, and have two classes right now, User and Organization. When I put a many-to-one property inside the User class, the SessionFactory doesn't get created. When I take it out, everything works fine, but of course the relationship isn't established.

At this point, I only need a unidirectional relationship (as in, I only need to find out which organization the user belongs to).

My two mappings:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.dcd.pojos.User" table="USERS">

<id name="id" type="string" unsaved-value="null">
<column name="ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="userName">
<column name="USERNAME" sql-type="char(255)"/>
</property>
<property name="password">
<column name="PASSWORD" sql-type="char(255)"/>
</property>

<!-- when the next line is included, the SessionFactory isn't created -->
<many-to-one name="organization" class="com.dcd.pojos.Organization" column="ORGANIZATION_ID"/>

</class>
</hibernate-mapping>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.dcd.pojos.Organization" table="ORGANIZATIONS">

<id name="id" type="string" unsaved-value="null">
<column name="ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name">
<column name="NAME" sql-type="char(255)"/>
</property>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: SessionFactory not getting created when using many-to-on
PostPosted: Tue Oct 12, 2004 1:24 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
cazzaran wrote:

<many-to-one name="organization" class="com.dcd.pojos.Organization" column="ORGANIZATION_ID"/>


take a look in the reference-doc on page 35 (i use version 2.1.4) ...
The column-property is used to name the column which is the foreign-key to your organization ... you don't have a property with name "ORGANIZATION_ID" in the user-mapping ;)

gtx
curio


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue Oct 12, 2004 2:32 am 
Newbie

Joined: Mon Oct 11, 2004 8:57 pm
Posts: 5
Location: Studio City, CA, USA
I see exactly what you are talking about, and as I suspected, a stupid mistake... ;) Thanks...

However, I added the property to my User.hbm.xml and still get a null SessionFactory... in fact, looking at the startup logs, it looks like it tried to create one, but then just stops. No errors, nothing... It says:

2368 [main] INFO net.sf.hibernate.impl.SessionFactoryImpl - building session factory
2371 [main] DEBUG net.sf.hibernate.impl.SessionFactoryImpl - instantiating session factory with properties: {show_sql......[long line omitted]

Here's my updated User.hbm.xml and my hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.dcd.pojos.User" table="USERS">

<id name="id" type="string" unsaved-value="null">
<column name="ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="userName">
<column name="USERNAME" sql-type="char(255)"/>
</property>
<property name="password">
<column name="PASSWORD" sql-type="char(255)"/>
</property>
<property name="organizationId">
<column name="ORGANIZATION_ID" sql-type="char(255)"/>
</property>

<many-to-one name="organization" class="com.dcd.pojos.Organization" column="ORGANIZATION_ID"/>

</class>
</hibernate-mapping>


<?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>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/data</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--<property name="jta.UserTransaction">java:comp/UserTransaction</property>-->
<!-- mapping files -->
<mapping resource="com/trial/pojos/User.hbm.xml"/>
<mapping resource="com/trial/pojos/Organization.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Again, if I remove the many-to-one, it creates the factory as expected *SIGH*...

Thanks in advance for any help...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 2:52 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Maybe it helps if you post your code between sessionFactory.openSession() and session.close(). Also your HibernateUtil code or whathever you're using to initialize Hibernate.

HTH
Ernst


Top
 Profile  
 
 Post subject: The "Open Session in View" pattern
PostPosted: Tue Oct 12, 2004 3:12 am 
Newbie

Joined: Mon Oct 11, 2004 8:57 pm
Posts: 5
Location: Studio City, CA, USA
I'm using the SessionManager pattern, where a servlet Filter handles the initialization and the session management...

It's init'd here:

public void init(FilterConfig filterConfig) throws ServletException {
// Initialize hibernate
log.debug("INIT FILTER");
try {
//Here's where it fails... it begins this, but doesn't finish
factory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
e.printStacktrace();
}
}

And I can retrieve a session in my classes by calling this static method (note, the factory variable is a static class member of type SessionFactory):

public static Session getSession() throws HibernateException {
log.debug("getSession()");
Session sess = (Session) hibernateHolder.get();
Transaction tx = (Transaction) txHolder.get();

if (sess == null) {
//I get a NullPointerException here when I call this method in my class (but only when I have the many-to-one in my user mapping)
sess = factory.openSession();
txHolder.set(sess.beginTransaction());
hibernateHolder.set(sess);
} else {
if (tx == null) {
throw new IllegalStateException("Transaction was already rolled back");
}
}
return sess;
}

It's all set up at each request with (hibernateHolder and txHolder are ThreadLocals):

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.fatal("doFilter()");
if (hibernateHolder.get() != null)
throw new IllegalStateException("A session is already associated with this thread! Someone must have called getSession() outside of the context of a servlet request.");

try {
chain.doFilter(request, response);
} finally {
Session sess = (Session) hibernateHolder.get();
Transaction tx = (Transaction) txHolder.get();
if (sess != null) {
hibernateHolder.set(null);

try {
if (tx != null) {
tx.commit();
}
sess.close();
} catch (HibernateException ex) {
throw new ServletException(ex);
}
}
}
}[/b]


Top
 Profile  
 
 Post subject: Figured it out
PostPosted: Tue Oct 12, 2004 4:51 pm 
Newbie

Joined: Mon Oct 11, 2004 8:57 pm
Posts: 5
Location: Studio City, CA, USA
Again, a stupid oversight on my side. I didn't put a property called organization of type Organization in my User class, so hibernate couldn't populate it.

I was able to figure this out eventually because I found a catch clause that was swallowing all the execptions (hence the no errors).

Thanks for all the help!!!!!


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