Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 366182

Summary: t.get(TupleElement) does not throw java.lang.IllegalArgumentException if tuple element does not correspond to an element in the query result tuple
Product: z_Archived Reporter: Stephen DiMilla <stephen.dimilla>
Component: EclipselinkAssignee: Project Inbox <eclipselink.orm-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: blocker    
Priority: P3 CC: christopher.delahunt, lance.andersen, stephen.dimilla, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Stephen DiMilla CLA 2011-12-09 08:35:37 EST
Build Identifier: eclipselink-2.3.0.v20110604-r9504

Using the entities:

@Entity
@Table(name="CUSTOMER_TABLE")
public class Customer implements java.io.Serializable
{

    // Instance variables
    private String id;
    private String name;
    public Customer()
    {
    }

    public Customer (String id, String name)
    {
    this.id = id;
    this.name = name;
    }
   @Id
    @Column(name="ID")
    public String getId() {
    return id;
    }
    public void setId(String v) {
    this.id = v;
    }

    @Column(name="NAME")
    public String getName() {
    return name;
    }
    public void setName(String v) {
    this.name = v;
    }
}

--------------------
@Entity
@Table(name="ORDER_TABLE")
public class Order implements java.io.Serializable
{


    // Instance variables
    private String id;
    private double totalPrice;

    public Order()
    {
    }

    public Order (String id, double totalPrice)
    {
        this.id = id;
        this.totalPrice = totalPrice;
    }

    public Order (String id)
    {
        this.id = id;
    }

    @Id
    @Column(name="ID")
    public String getId() {
	return id;
    }
    public void setId(String id) {
	this.id = id;
    }

    @Column(name="TOTALPRICE")
    public double getTotalPrice() {
	return totalPrice;
    }
    public void setTotalPrice(double price) {
	this.totalPrice = price;
    }

----------------------
Using the client code:

CriteriaBuilder cbuilder = getEntityManager().getCriteriaBuilder();
et.begin();
CriteriaQuery<Tuple> cquery = cbuilder.createTupleQuery();
Root<Customer> customer = cquery.from(Customer.class);

System.out.println("Use Tuple Query");
cquery.multiselect(customer.get("id"), customer.get("name"));
TypedQuery<Tuple> tq = getEntityManager().createQuery(cquery);

List<Tuple> result = tq.getResultList();

Tuple t = result.get(0);
 System.out.println("Testing valid index");
 System.out.println("value:" + t.get(1, String.class));

System.out.println("Testing invalid index");
try {
        t.get(99, String.class);
        System.out.println("Did not get expected IllegalArgumentException for
invalid index:" + t.get(99, String.class));
} catch (IllegalArgumentException iae) {
        System.out.println("Got expected IllegalArgumentException");
} catch (Exception e) {
        System.out.println("Received unexpected exception", e);
}

------------
Client code:
CriteriaBuilder cbuilder = getEntityManager().getCriteriaBuilder();

et.begin();
CriteriaQuery<Tuple> cquery = cbuilder.createTupleQuery();
System.out.println("Obtained Non-null Criteria Query");
Root<Customer> customer = cquery.from(Customer.class);

System.out.println("Execute first Tuple Query");
cquery.multiselect(customer.get("id").alias("ID"),customer.get("name").alias("NAME"));
cquery.where(cbuilder.equal(customer.get("id"), 1));

TypedQuery<Tuple> tq = getEntityManager().createQuery(cquery);
List<Tuple> result = tq.getResultList();

System.out.println("Number of Tuples from first query:" + result.size());
Tuple t1 = result.get(0);

System.out.println("Tuples Received:" + t1.get(0) + ", " + t1.get(1));


// get second Tuple and second TupleElement inorder to trigger IllegalArgumentException
Root<Order> order = cquery.from(Order.class);

System.out.println("Execute second Tuple Query");

cquery.multiselect(order.get("id").alias("ID"), order.get("totalPrice").alias("TOTALPRICE"));
cquery.where(cbuilder.equal(order.get("id"), 1));

TypedQuery<Tuple> tq2 = getEntityManager().createQuery(cquery);
List<Tuple> result2 = tq2.getResultList();

System.out.println("Number of Tuples from second query:" + result2.size());
Tuple t2 = result2.get(0);

System.out.println("Tuples Received:" + t2.get(0) + ", " + t2.get(1));

List<TupleElement<?>> lte2 = t2.getElements();
TupleElement<?> te2 = lte2.get(1);

System.out.println("TupleElement from second query that will be looked up in the Tuple result returned from first query:" + te2.getAlias());
try {

     // Using a tuple element returned in the second query,  try to get a tuple from the first query using
     // that tuple element
     t1.get(te2);
     System.out.println("Did not get IllegalArgumentException when calling Tuple.get with a TupleElement that doesn't exist");

} catch (IllegalArgumentException iae) {
    System.out.println("Got expected IllegalArgumentException");
} catch (Exception e) {
     System.out.println("Received unexpected exception", e);
}


------------
output:
Execute first Tuple Query
[EL Fine]: 2011-12-09 08:27:10.938--ServerSession(165149691)--Connection(1393000393)--Thread(Thread[main,5,main])--SELECT ID, NAME FROM CUSTOMER_TABLE WHERE (ID = ?)
bind => [1]
Number of Tuples from first query:1
Tuples Received:1, Alan E. Frechette
Execute second Tuple Query
[EL Fine]: 2011-12-09 08:27:10.97--ServerSession(165149691)--Connection(1393000393)--Thread(Thread[main,5,main])--SELECT ID, TOTALPRICE FROM ORDER_TABLE WHERE (ID = ?)
bind => [1]
Number of Tuples from second query:1
Tuples Received:1, 1193.8500000000001
TupleElement from second query that will be looked up in the Tuple result returned from first query:TOTALPRICE
Received unexpected exception
java.lang.ArrayIndexOutOfBoundsException: -1
	at java.util.ArrayList.get(ArrayList.java:324)
	at org.eclipse.persistence.queries.ReportQueryResult.getByIndex(ReportQueryResult.java:474)
	at org.eclipse.persistence.internal.jpa.querydef.TupleImpl.get(TupleImpl.java:82)
	at org.eclipse.persistence.internal.jpa.querydef.TupleImpl.get(TupleImpl.java:30)

Reproducible: Always
Comment 1 Tom Ware CLA 2012-04-05 11:01:25 EDT
Updating target milestone.
Comment 2 Chris Delahunt CLA 2012-11-01 16:39:30 EDT
fixed by 366104 in 2.5

*** This bug has been marked as a duplicate of bug 366104 ***
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:02:49 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink