-->
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.  [ 3 posts ] 
Author Message
 Post subject: Many-To-One with not-null="false"
PostPosted: Tue Mar 07, 2006 5:05 am 
Newbie

Joined: Tue Mar 07, 2006 4:54 am
Posts: 6
Hibernate version:
2.0
Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="State" table="states" lazy="true">

<id name="id" column="id" type="string" unsaved-value="null">
<generator class="native"/>
</id>
<property name="countryId">
<column name="country_id" not-null="true"/>
</property>
<property name="name">
<column name="name" not-null="true"/>
</property>

</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="User" table="sp">

<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="email">
<column name="email" not-null="true"/>
</property>
<many-to-one name="state"
class="State"
column="state_id"
not-null="false" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
SEVERE: Exception initializing proxy
net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: , of class: State
at net.sf.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:24)
at net.sf.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:1945)
at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:53)
at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60)
at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164)
at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi,

I have the above mapping for a user which has a country and a state. Now, not all users have states and therefore the many-to-one mapping (User-->State) might NOT contain a state. I keep getting the above exception for users without states. I have defined the not-null="false" in hope this will work but it has not.

Is there a better mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 4:21 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
Your mapping seems fine for this. If the not-null was a problem you would see an exception about this constraint being violated.

Do you have existing data in the database? It seems as if you have a blank column value for one of the state_id columns. That's why hibernate is complaining about not being able to find a State with basically a blank PK.

_________________
-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 1:25 pm 
Newbie

Joined: Tue Mar 07, 2006 4:54 am
Posts: 6
Indeed some rows in the User are empty for their state. But that's what I need and that's why hibernate is exceptioning I guess.

But how to solve this?

Thanks,

Assaf


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