-->
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.  [ 8 posts ] 
Author Message
 Post subject: Select with "and 1=2" returns no results
PostPosted: Wed May 24, 2006 8:57 pm 
Newbie

Joined: Wed May 24, 2006 5:11 pm
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 10:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's no query or criteria in there. Where's the java code that produces the weird SQL?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 11:42 pm 
Newbie

Joined: Wed May 24, 2006 5:11 pm
Posts: 4
The trigger code:
Code:
Kid kidUser = (Kid) dao.getUserByUsername("tomcat");


How the DAO actually does it:
Code:
    public BaseUser getUserByUsername(String username) {
       List list = getHibernateTemplate().find("from BaseUser u where u.username=?",username);
        if(list != null && list.size() > 0){
            return (BaseUser) list.get(0);
        }
        return null;
    }


Note that the item that has the problem is the Parent user which is related to the Kid, not the Kid itself. If we pull the Parent in directly by id (which by examining the logs is I believe how the Kid actually accomplishes the Parent population), we get the same result. I had wanted to check that to make sure there was nothing about the relationship itself that was causing the problem.

Code to get parent by id:
Code:
   public Parent getParent(Long userId) {
        Parent user = (Parent) getHibernateTemplate().get(Parent.class, userId);

        if (user == null) {
            log.warn("uh oh, user '" + userId + "' not found...");
            throw new ObjectRetrievalFailureException(Parent.class, userId);
        }

        return user;
   }


We're running within Spring, so the getHibernateTemplate() comes from Spring's HibernateDAOSupport abstract class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 12:00 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I don't see anything wrong in what you've done. Could someone in your team have put in some debug code and accidendantlly left it in longer than they intended?

Try it without the hibernate template. Get the session from your usual place (getHibernateTemplate().getCurrentSession()? I dunno), and use session.get(Parent.class, userId).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 12:10 am 
Newbie

Joined: Wed May 24, 2006 5:11 pm
Posts: 4
Updated my method to get a Parent by its id to:
Code:
public Parent getParent(Long userId) {
      
        //Parent user = (Parent) getHibernateTemplate().get(Parent.class, userId);
       
        Session session = getSession();
        Parent user = (Parent) session.get(Parent.class, userId);
        if (user == null) {
            log.warn("uh oh, user '" + userId + "' not found...");
            throw new ObjectRetrievalFailureException(Parent.class, userId);
        }

        return user;
   }


Same result: generated SQL still has that !@#$ 1=2 on the end. That's somewhat encouraging, though, as even if I resolved the problem with getting directly by id, not sure how I would have forced the get through the Kid's relationship with the parent to have worked the same way.
Quote:
[junit] [@APPNAME@] DEBUG [main] UserDAOTest.testGetParentUserById(136) | testGetParentUserById
[junit] [@APPNAME@] DEBUG [main] DefaultLoadEventListener.proxyOrLoad(153) | loading entity: [com.kajeet.model.user.Parent#11]
[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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 12:29 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It that was happening to me, I'd be presuming that it's a memento of a previous debugging session, and I'd search every java file I have for "1=2".

Perhaps you have a custom session factory? Or a wrapper for sessionImpl? Something that customizes what hibernate does in some proprietary manner?

Last resort is to debug through the entire process, deep into the murky depths of hibernate. Good luck!

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 12:55 am 
Newbie

Joined: Wed May 24, 2006 5:11 pm
Posts: 4
Got it! (Do I get my own credit back? <smile> )

In my object hierarchy, there were parent classes marked as hibernate subclasses which were listed as abstract="true". The 1=2 seems to be Hibernate's way of enforcing that those classes can't be instantiated. I can't say as I yet understand in what instances we'd use abstract="true", but removing it from the parent classes caused my problem to disappear.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 1:27 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Noted. Will add it to my quickanswer page :)

_________________
Code tags are your friend. Know them and use them.


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