Skip to main content

The class of the argument for the object comparison is incorrect.

4 replies [Last post]
mariusw
Offline
Joined: 2007-06-01

I'm about to tear my hair out over this one, guys. Hope you can help me while I still have some left...

I have a many-to-one relationship between entities Access and Person: A person can have access to many services, but a service access record is applicable for one person only.

Using JPA, I have mapped the relationships using XML, as shown here: http://www.bluebricks.no/web/bbwebno.nsf/(files)/java.net/$file/orm.xml.txt
Code for the Person entity: http://www.bluebricks.no/web/bbwebno.nsf/(files)/java.net/$file/Person.java.txt
Code for the Access entity: http://www.bluebricks.no/web/bbwebno.nsf/(files)/java.net/$file/Access.java.txt
Full stacktrace: http://www.bluebricks.no/web/bbwebno.nsf/(files)/java.net/$file/stacktrace.txt

When I'm trying to fetch an access record from the database using a session bean, I get the following error:
Exception [TOPLINK-6078] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.QueryException
Exception Description: The class of the argument for the object comparison is incorrect.
Expression: [
Base com.bluebricks.net.entity.Access]
Mapping: [oracle.toplink.essentials.mappings.OneToOneMapping[person]]
Argument: [2 983]
Query: ReportQuery(com.bluebricks.net.entity.Access)

I try fetching the record by creating a query with the EntityManager, using the following JPQL:
SELECT x FROM Access x WHERE x.person=2983 AND x.service=12

Both the person and the service columns in the bb_access table are integer

If I try the following SQL query by hand to the database, the query succeeds:
SELECT * FROM bb_core.bb_access WHERE person=2983 AND service=12

This is the bean method:

public T findRecord( Class entityClass, String strQuery ) throws DatabaseConnectionException {

try {
Query query = getEntityManager().createQuery( strQuery );
return entityClass.cast( query.getSingleResult( ) );
} catch (NoResultException noResult) {
throw new DatabaseConnectionException( noResult.getMessage(), DatabaseConnectionException.RECORD_NOT_FOUND_ERROR );
} catch (EntityNotFoundException notFound) {
throw new DatabaseConnectionException( notFound.getMessage(), DatabaseConnectionException.RECORD_NOT_FOUND_ERROR );
} catch (NonUniqueResultException nonUnique) {
throw new DatabaseConnectionException( nonUnique.getMessage(), DatabaseConnectionException.RECORD_NOT_FOUND_ERROR );
} // try
} // findRecord

The same method is used for all other entities as well, working as a charm. It is only when using the Access class I get an error, and I'm unable to see why.

Hopefully, some of you seeing this with "fresh eyes" will see something I can't.

Regards
Marius

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04

Hi Marius,

I'm not sure how your entity actually look like - the code in the link has all annotations commented out, but the person field is either a relationship (and must be marked as such) or a BLOB (i.e. expected to be stored as a serializable stream). In any case, you can't compare it to a String or an integer. When you use SQL, you most probably compare the value in the FK column directly, which of course works.

Regards,
-marina

mariusw
Offline
Joined: 2007-06-01

Hi Marina

Thanks for answering. I do not fully understand what you mean.

The annotations in the entity are commented out because I use XML instead of annotations. So the entity looks exactly like the one in the link.

If you see the link to the XML-file, you can see how I've defined the relationship between the entities.

The person field is marked as a relationship in the orm.xml file:

target-entity="com.bluebricks.net.entity.Person"
fetch="EAGER">


I've tried with the person column as an ordinary integer column and also when it is defined as a foreign key. No difference.

In the Access entity, the person field is of type Person, of course.

The first reference to one of my classes in the stack trace is to the bean class, line 98, which is this line:
return entityClass.cast( query.getSingleResult( ) );

This might mean that the object returned by the getSingleResult() method is NOT of the class represented by the entityClass variable (which is Access.class).
I don't know what other class it could be, as the jpql query points to the Access class.

(The reason for the cast is that I use generics so that the same bean can be used by any entity)

Marius

mvatkina
Offline
Joined: 2005-04-04

Hi Marius,

Sorry, I didn't notice the link to the orm.xml.

You can't compare an object reference (x.person) to a value 2983, and this is what the exception complains about.

If that value represents the id of the related person, change the left side of the comparison to the person's id field (x.person.id). Or you can compare 2 persons (x.person = :person) and pass a reference to an existing person as a parameter to the query.

You'll need to fix the 'service' comparison as well.

Regards,
-marina

mariusw
Offline
Joined: 2007-06-01

Thank you!! I thought the persistence provider would automatically use the id of the person object, and I was not aware that you could point to the id field using dot notation in JPQL. Now it seems very obvious (of course).

Thanks again, and you managed to save some strands of hair as well.

You're an angel.

Marius