[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Order of adding Join Attribute Expressions when using Dynamic Persistence

Hello All

I am responding to an earlier unanswered mail thread that I had started on a query that is similar to the one asked before. In order to shed more light on the issue, I have included a more specific example in this mail.

I have attached a program to give you an idea of where I am facing a problem. Each type corresponds to a table in the database.

I have the following questions:

1. I am querying customer type for all orders placed by her. Each order has a corresponding invoice. However when I query the order dynamic type for the join attribute invoice9.order9.invoiceid then I do not get any invoices. Moreover the type of e2 is IndirectList. If I do not add the exp4 join attribute then I do get the invoice corresponding to the order (List e2 is not empty). Why is this happening?

2. If I use setUsesIndirection to false for all the mappings, the first entity does not have any invoices as above however for all other entities I get a Vector instead of an IndirectList which has the expected objects of the dynamic type invoice. In this case the number of accesses to the database is also large. Could you please explain the behavior?

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies


On 12/20/2010 10:21 AM, Rohit Banga wrote:
Hi Christopher

Here is the stack trace:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
ÂÂÂ at java.util.ArrayList.add(Unknown Source)
ÂÂÂ at org.eclipse.persistence.internal.queries.JoinedAttributeManager.addExpressionAndBaseToGroupedList(JoinedAttributeManager.java:828)
ÂÂÂ at org.eclipse.persistence.internal.queries.JoinedAttributeManager.prepareJoinExpressions(JoinedAttributeManager.java:780)
ÂÂÂ at org.eclipse.persistence.queries.ObjectLevelReadQuery.prePrepare(ObjectLevelReadQuery.java:2057)
ÂÂÂ at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrePrepare(ObjectLevelReadQuery.java:834)
ÂÂÂ at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:817)
ÂÂÂ at org.eclipse.persistence.queries.DatabaseQuery.prepareCall(DatabaseQuery.java:1584)

Thanks

Can you post the exception?

Â


----- Original Message -----
From: rohit.banga@xxxxxxxxxx
To: eclipselink-users@xxxxxxxxxxx
Sent: Saturday, December 18, 2010 6:27:18 AM GMT -05:00 US/Canada Eastern
Subject: [eclipselink-users] Order of adding Join Attribute Expressions when using Dynamic Persistence

Hello All

I have a customer table, a phone table and an order table.
The customer table has a one-to-many with both the phone and order tables.

I am querying the order table.

If I add the join attributes in the following sequence then it works fine:

ÂÂÂ ÂÂÂ query.addJoinedAttribute(query.getExpressionBuilder().get("cust9.order9.custid"));
ÂÂÂ ÂÂÂ query.addJoinedAttribute(query.getExpressionBuilder().get("cust9.order9.custid").anyOf("cust9.phone9.custid"););

However, if I add in this sequence I get an IndexOutOfBounds Exception.

ÂÂÂ ÂÂÂ query.addJoinedAttribute(query.getExpressionBuilder().get("cust9.order9.custid").anyOf("cust9.phone9.custid"););
ÂÂÂ ÂÂÂ query.addJoinedAttribute(query.getExpressionBuilder().get("cust9.order9.custid"));


Could you please explain the reason for this exception? Is it expected that the join attributes be passed in a particular order?

Thanks in Advance.

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies
_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies
import java.util.List;

import org.eclipse.persistence.dynamic.DynamicClassLoader;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.dynamic.DynamicHelper;
import org.eclipse.persistence.dynamic.DynamicTypeBuilder;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.internal.sessions.DatabaseSessionImpl;
import org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.sessions.DatabaseLogin;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.sessions.DatabaseSession;

/**
 * @author rbanga
 *
 */
public class JoinOrderProblem {

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
    {
        DatabaseLogin login = new DatabaseLogin();
        login.setUserName("******");
        login.setPassword("******");
        login.setDriverURLHeader("******");
        login.setDriverClassName("******");
        login.setDatabaseURL("**************");
        DatabaseSession session = new DatabaseSessionImpl(login);

        session.login();

        DynamicHelper helper = new DynamicHelper(session);
        DynamicClassLoader dcl = helper.getDynamicClassLoader();

        Class<?> custType = dcl.createDynamicClass("jpatest.customer");
        Class<?> invoiceType = dcl.createDynamicClass("jpatest.invoice");
        Class<?> orderType = dcl.createDynamicClass("jpatest.order");
        Class<?> bookType = dcl.createDynamicClass("jpatest.book");
        Class<?> orderBookType = dcl.createDynamicClass("jpatest.orderbook");

        DynamicTypeBuilder custBuilder = new JPADynamicTypeBuilder(custType, null, "cust9");
        custBuilder.setPrimaryKeyFields("custid");
        custBuilder.addDirectMapping("custid", int.class, "custid");
        custBuilder.addDirectMapping("name", String.class, "name");

        DynamicTypeBuilder invoiceBuilder = new JPADynamicTypeBuilder(invoiceType, null, "invoice9");
        invoiceBuilder.setPrimaryKeyFields("invoiceid");
        invoiceBuilder.addDirectMapping("invoiceid", int.class, "invoiceid");
        invoiceBuilder.addDirectMapping("value", int.class, "value");
        
        DynamicTypeBuilder orderBuilder = new JPADynamicTypeBuilder(orderType, null, "order9");
        orderBuilder.setPrimaryKeyFields("orderid");
        orderBuilder.addDirectMapping("orderid", int.class, "orderid");
        orderBuilder.addDirectMapping("qty", int.class, "qty");
        
        DynamicTypeBuilder bookBuilder = new JPADynamicTypeBuilder(bookType, null, "book9");
        bookBuilder.setPrimaryKeyFields("bookid");
        bookBuilder.addDirectMapping("bookid", int.class, "bookid");
        bookBuilder.addDirectMapping("title", String.class, "name");

        DynamicTypeBuilder orderBookBuilder = new JPADynamicTypeBuilder(orderBookType, null, "order9_book9");
        orderBookBuilder.setPrimaryKeyFields("bookid", "orderid");

        custBuilder.addOneToManyMapping("cust9.order9.custid", orderBuilder.getType(), "order9.custid");
        orderBuilder.addOneToOneMapping("cust9.order9.custid", custBuilder.getType(), "order9.custid");

        orderBuilder.addOneToManyMapping("invoice9.order9.invoiceid", invoiceBuilder.getType(), "invoice9.orderid");
        invoiceBuilder.addOneToOneMapping("invoice9.order9.invoiceid", orderBuilder.getType(), "invoice9.orderid");

        orderBuilder.addOneToManyMapping("order9.order9_book9.orderid", orderBookBuilder.getType(), "order9_book9.orderid");
        orderBookBuilder.addOneToOneMapping("order9.order9_book9.orderid", orderBuilder.getType(), "order9_book9.orderid");

        bookBuilder.addOneToManyMapping("book9.order9_book9.bookid", orderBookBuilder.getType(), "order9_book9.bookid");
        orderBookBuilder.addOneToOneMapping("book9.order9_book9.bookid", bookBuilder.getType(), "order9_book9.bookid");

        helper.addTypes(false, true, custBuilder.getType(),
                orderBuilder.getType(), invoiceBuilder.getType(),
                bookBuilder.getType(), orderBookBuilder.getType()
            );

        ReadAllQuery query = helper.newReadAllQuery(custBuilder.getType().getDescriptor().getAlias());
        Expression exp1 = new ExpressionBuilder().anyOf("cust9.order9.custid");
        Expression exp2 = new ExpressionBuilder().anyOf("cust9.order9.custid").anyOf("order9.order9_book9.orderid");
        Expression exp3 = new ExpressionBuilder().anyOf("cust9.order9.custid").anyOf("invoice9.order9.invoiceid");
        Expression exp4 = new ExpressionBuilder().anyOf("cust9.order9.custid").anyOf("order9.order9_book9.orderid").get("book9.order9_book9.bookid");

        query.addJoinedAttribute(exp1);
        query.addJoinedAttribute(exp2);
        query.addJoinedAttribute(exp3);
        query.addJoinedAttribute(exp4);

        query.prepareCall(session, new DatabaseRecord());

        System.out.println("query: " + query.getSQLString());
        
        for (DynamicEntity entity : (List<DynamicEntity>) session.executeQuery(query))
        {
            System.out.println("custid:" + entity.get("custid"));
            System.out.println("name:" + entity.get("name"));

            for (DynamicEntity e1 : (List<DynamicEntity>) entity.get("cust9.order9.custid"))
            {
                System.out.println("orderid: " + e1.get("orderid") );
                System.out.println("qty: " + e1.get("qty") );
                System.out.println(e1.get("invoice9.order9.invoiceid").getClass().getName());
                List<DynamicEntity> e2 = (List<DynamicEntity>) e1.get("invoice9.order9.invoiceid");
                System.out.println("number of entities: " + e2.size());
                for (DynamicEntity e3 : e2)
                {
                    System.out.println("invoiceid: " + e3.get("invoiceid") );
                    System.out.println("value: " + e3.get("value") );
                }
            }
            
            System.out.println();
        }
    }

}