Object comparation never matches

#1

Hi!

I'm trying to compare 2 identical objects in SELECT with no success:

@Embeddable
public class PhoneNumber implements Serializable {
    int countryCode;
    long number;

 

Which is a part of Customer object:

@MappedSuperclass
@NamedQueries({
    @NamedQuery(
        name="authentication.getByPrincipal",
        query="SELECT a FROM Customer c WHERE c.principal = :principal"
    )
})
public abstract class Customer implements Serializable {
    Object principal;

The entity is:

@Entity
public class MobileCustomer extends Customer {
    @Id @GeneratedValue
    private Long id;
public void setPrincipal(PhoneNumber number){
    principal = number;
}

 

And making a query:

Customer customer = em.createNamedQuery("authentication.getByPrincipal", Customer.class)
                .setParameter("principal", authentication.getPrincipal()).getSingleResult();

According to ObjectDB explorer, the record is stored as expected.

... principal | PhoneNumber | {}

        countryCode | int | 123

        number | long | 4567890

 

Then I'm constructing exactly the same object PhoneNumber and passing it to above search query as "principal" and getting "[ObjectDB 2.4.6] javax.persistence.NoResultException" "No matching results for a unique query (error 782)"  on getSingleResult

PhoneNumber contains exactly 2 fields. So I'm sure that both objects are same.

What am I doing wrong?

thanks!

#2

In queries you can compare entity objects and compare simple values, but not embedded objects.

Try the following query:

SELECT a FROM Customer c
WHERE c.principal.countryCode = :countryCode
  AND c.principal.number = :number
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

But what about: http://www.objectdb.com/java/jpa/query/jpql/comparison

Section "Comparable Data Types"

Comparison is supported for values of the following data types:

.........

Instances of user defined classes (entity classes and embeddable classes) can be compared by using the equality operators (=, <>, ==, !=). For entities, e1 = e2 if e1 and e2 have the same type and the same primary key value. For embeddable objects, e1 = e2 if e1 and e2 have exactly the same content.

 

though it is exactly what I'm trying to do.

#4

You are right. It was supported, even though it was never widely used.

Build 2.4.7_16 includes an attempt to enable this feature again.

However, there are still reasons to prefer value comparison as suggested in #2 above:

  • Value comparison is compatible with JPA and therefore more portable.
  • Value comparison could be more efficient (especially if indexes are defined).
  • Embedded comparison does not support changes to the schema of the embeddable class. After such a change, old objects and new objects will not be considered as equal.
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

I'll consider your suggestions.

Thanks for quick response! Your product is really impressive!


Post Reply

To post a reply and/or subscribe to update notifications - please login