Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Having an interesting problem. I have an entity, Parent, which has a one-to-many relationship with Kid. Each kid has a Parent. I can retrieve a Parent by id, and get with it all of its kids, no problem. But when I retrieve a Kid that has that same Parent, I get a message stating that 'No row with the given identifier exists.' Looking at the SQL, the answer seems to be that the SQL generated for getting that parent appends a "1=2" clause on the end - real problem for finding ANY rows. Note that when I do get that same parent record, I'm seeing in the logs that it's being retrieved using the query for BaseUser, rather than Parent)...
Looking at the snapshot selects for BaseUser and Parent, neither appends "1=2"...
[junit] [@APPNAME@] DEBUG [main] AbstractEntityPersister.logStaticSQL(2450) | Snapshot select: /* get current state com.kajeet.model.user.BaseUser */ select baseuser_.id, baseuser_.lastUpdated as lastUpda3_1_, baseuser_.username as username1_, baseuser_.password as password1_, baseuser_.firstName as firstName1_, baseuser_.lastName as lastName1_, baseuser_.email as email1_, baseuser_.passwordHint as password9_1_, baseuser_.enabled as enabled1_, baseuser_.accountLocked as account11_1_, baseuser_.accountExpired as account12_1_, baseuser_.credentialsExpired as credent13_1_ from users baseuser_ where baseuser_.id=?
[junit] [@APPNAME@] DEBUG [main] AbstractEntityPersister.logStaticSQL(2450) | Snapshot select: /* get current state com.kajeet.model.user.Parent */ select parent_.id, parent_.lastUpdated as lastUpda3_1_, parent_.username as username1_, parent_.password as password1_, parent_.firstName as firstName1_, parent_.lastName as lastName1_, parent_.email as email1_, parent_.passwordHint as password9_1_, parent_.enabled as enabled1_, parent_.accountLocked as account11_1_, parent_.accountExpired as account12_1_, parent_.credentialsExpired as credent13_1_, parent_.customerNumber as custome14_1_, parent_.walletId as walletId1_, parent_.photoId as photoId1_, parent_.addressId as addressId1_, parent_.gender as gender1_, parent_.homePhoneNumber as homePho19_1_, parent_.dob as dob1_, parent_.ssn as ssn1_, parent_.creditCardId as creditC22_1_ from users parent_ where parent_.id=?
Interested in ideas for what we've foobarred.
Hibernate version: 3.1.3
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.kajeet.model.user.BaseUser"
table="users"
discriminator-value="U"
>
<id
name="id"
column="id"
>
<generator
class="sequence"
>
<param name="sequence">
<![CDATA[userId_seq]]>
</param>
</generator>
</id>
<discriminator
column="userType"
type="char"
/>
<version
name="lastUpdated"
column="lastUpdated"
type="timestamp"
/>
<property
name="username"
not-null="true"
unique="true"
>
</property>
<property
name="password"
not-null="true"
>
</property>
<property
name="firstName"
length="50"
not-null="true"
>
</property>
<property
name="lastName"
length="50"
not-null="true"
>
</property>
<property
name="email"
not-null="true"
unique="true"
>
</property>
<property
name="passwordHint"
not-null="false"
>
</property>
<set
name="roles"
table="userRoles"
lazy="false"
cascade="save-update"
>
<key
column="userId"
>
</key>
<many-to-many
class="com.kajeet.model.Role"
column="roleName"
>
</many-to-many>
</set>
<property
name="enabled"
type="yes_no"
>
</property>
<property
name="accountLocked"
type="yes_no"
not-null="true"
>
</property>
<property
name="accountExpired"
type="yes_no"
not-null="true"
>
</property>
<property
name="credentialsExpired"
type="yes_no"
not-null="true"
>
</property>
<subclass
name="com.kajeet.model.user.NonCustomerUser"
discriminator-value="N"
>
</subclass>
<subclass
name="com.kajeet.model.user.BaseWebUser"
discriminator-value="W"
abstract="true"
>
<property
name="customerNumber"
not-null="true"
>
<column
name="customerNumber"
index="customerNoIdx"
/>
</property>
<many-to-one
name="wallet"
class="com.kajeet.model.Wallet"
column="walletId"
lazy="false"
>
</many-to-one>
<many-to-one
name="photo"
class="com.kajeet.model.KajeetImage"
column="photoId"
>
</many-to-one>
<set
name="calendarEvents"
lazy="false"
inverse="true"
cascade="all"
>
<key
column="userId"
>
</key>
<one-to-many
class="com.kajeet.model.calendar.CalendarEvent"
/>
</set>
<subclass
name="com.kajeet.model.user.Parent"
discriminator-value="P"
abstract="true"
>
<many-to-one
name="address"
class="com.kajeet.model.Address"
column="addressId"
cascade="all"
>
</many-to-one>
<property
name="gender"
not-null="true"
>
</property>
<property
name="homePhoneNumber"
not-null="false"
>
</property>
<property
name="dob"
not-null="true"
>
</property>
<property
name="ssn"
length="11"
not-null="true"
>
</property>
<many-to-one
name="creditCard"
class="com.kajeet.model.CreditCard"
column="creditCardId"
lazy="false"
>
</many-to-one>
<set
name="kids"
lazy="false"
cascade="all"
>
<key
column="parent"
>
</key>
<one-to-many
class="com.kajeet.model.user.Kid"
/>
</set>
</subclass>
<subclass
name="com.kajeet.model.user.PhoneUser"
discriminator-value="H"
abstract="true"
>
<many-to-one
name="KRewards"
class="com.kajeet.model.Wallet"
column="kayRewardId"
lazy="false"
>
</many-to-one>
<many-to-one
name="phone"
class="com.kajeet.model.Phone"
column="ESN"
cascade="all"
insert="false"
update="false"
lazy="false"
>
</many-to-one>
<property
name="subscriberSince"
>
</property>
<many-to-one
name="serviceProfile"
class="com.kajeet.model.user.ServiceProfile"
column="serviceProfileId"
cascade="all"
>
</many-to-one>
<subclass
name="com.kajeet.model.user.SingleAccount"
discriminator-value="S"
abstract="true"
>
<many-to-one
name="address"
class="com.kajeet.model.Address"
column="addressId"
cascade="all"
>
</many-to-one>
<property
name="gender"
not-null="true"
>
</property>
<property
name="homePhoneNumber"
not-null="false"
>
</property>
<property
name="dob"
not-null="true"
>
</property>
<property
name="ssn"
length="11"
not-null="true"
>
</property>
<many-to-one
name="creditCard"
class="com.kajeet.model.CreditCard"
column="creditCardId"
lazy="false"
>
</many-to-one>
</subclass>
<subclass
name="com.kajeet.model.user.Kid"
discriminator-value="K"
abstract="true"
>
<many-to-one
name="address"
class="com.kajeet.model.Address"
column="addressId"
cascade="all"
>
</many-to-one>
<property
name="gender"
not-null="true"
>
</property>
<property
name="homePhoneNumber"
not-null="false"
>
</property>
<property
name="dob"
not-null="true"
>
</property>
<property
name="ssn"
length="11"
not-null="true"
>
</property>
<many-to-one
name="parent"
class="com.kajeet.model.user.Parent"
cascade="save-update"
lazy="false"
>
</many-to-one>
</subclass>
</subclass>
</subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
[junit] [@APPNAME@] DEBUG [main] DefaultLoadEventListener.doLoad(304) | attempting to resolve: [com.kajeet.model.user.Parent#11]
[junit] [@APPNAME@] DEBUG [main] DefaultLoadEventListener.doLoad(340) | object not resolved in any cache: [com.kajeet.model.user.Parent#11]
[junit] [@APPNAME@] DEBUG [main] AbstractEntityPersister.load(2723) | Fetching entity: [com.kajeet.model.user.Parent#11]
[junit] Hibernate: /* load com.kajeet.model.user.Parent */ select parent0_.id as id1_0_, parent0_.lastUpdated as lastUpda3_1_0_, parent0_.username as username1_0_, parent0_.password as password1_0_, parent0_.firstName as firstName1_0_, parent0_.lastName as lastName1_0_, parent0_.email as email1_0_, parent0_.passwordHint as password9_1_0_, parent0_.enabled as enabled1_0_, parent0_.accountLocked as account11_1_0_, parent0_.accountExpired as account12_1_0_, parent0_.credentialsExpired as credent13_1_0_, parent0_.customerNumber as custome14_1_0_, parent0_.walletId as walletId1_0_, parent0_.photoId as photoId1_0_, parent0_.addressId as addressId1_0_, parent0_.gender as gender1_0_, parent0_.homePhoneNumber as homePho19_1_0_, parent0_.dob as dob1_0_, parent0_.ssn as ssn1_0_, parent0_.creditCardId as creditC22_1_0_ from users parent0_ where parent0_.id=? and 1=2
[junit] [@APPNAME@] DEBUG [main] LongType.nullSafeSet(80) | binding '11' to parameter: 1
[junit] [@APPNAME@] INFO [main] DefaultLoadEventListener.onLoad(95) | Error performing load command
[junit] org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.kajeet.model.user.Parent#11]
[junit] at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:128)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
[junit] at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
[junit] at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:830)
[junit] at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
[junit] at org.hibernate.type.EntityType.resolve(EntityType.java:303)
[junit] at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
[junit] at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
[junit] at org.hibernate.loader.Loader.doQuery(Loader.java:717)
[junit] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
[junit] at org.hibernate.loader.Loader.doList(Loader.java:2145)
[junit] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
[junit] at org.hibernate.loader.Loader.list(Loader.java:2024)
[junit] at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
[junit] at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
[junit] at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
[junit] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
[junit] at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:826)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:817)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:813)
[junit] at com.kajeet.dao.hibernate.UserDAOHibernate.getUserByUsername(UserDAOHibernate.java:90)
[junit] at com.kajeet.dao.UserDAOTest.testGetKidUser(UserDAOTest.java:44)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:585)
[junit] at junit.framework.TestCase.runTest(TestCase.java:154)
[junit] at junit.framework.TestCase.runBare(TestCase.java:127)
[junit] at junit.framework.TestResult$1.protect(TestResult.java:106)
[junit] at junit.framework.TestResult.runProtected(TestResult.java:124)
[junit] at junit.framework.TestResult.run(TestResult.java:109)
[junit] at junit.framework.TestCase.run(TestCase.java:118)
[junit] at junit.framework.TestSuite.runTest(TestSuite.java:208)
[junit] at junit.framework.TestSuite.run(TestSuite.java:203)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:297)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:672)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:567)
Name and version of the database you are using:
Oracle 10g R2
The generated SQL (show_sql=true):
[junit] Hibernate: /* load com.kajeet.model.user.Parent */ select parent0_.id as id1_0_, parent0_.lastUpdated as lastUpda3_1_0_, parent0_.username as username1_0_, parent0_.password as password1_0_, parent0_.firstName as firstName1_0_, parent0_.lastName as lastName1_0_, parent0_.email as email1_0_, parent0_.passwordHint as password9_1_0_, parent0_.enabled as enabled1_0_, parent0_.accountLocked as account11_1_0_, parent0_.accountExpired as account12_1_0_, parent0_.credentialsExpired as credent13_1_0_, parent0_.customerNumber as custome14_1_0_, parent0_.walletId as walletId1_0_, parent0_.photoId as photoId1_0_, parent0_.addressId as addressId1_0_, parent0_.gender as gender1_0_, parent0_.homePhoneNumber as homePho19_1_0_, parent0_.dob as dob1_0_, parent0_.ssn as ssn1_0_, parent0_.creditCardId as creditC22_1_0_ from users parent0_ where parent0_.id=? and 1=2