Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How to use Fetchgroups

Hi Rohit,

If you are just looking for the data (i.e. the id and the firstname you want returned do not need to be stored in an EmpInfo), you do not need a fetch group, simply use a ReportQuery and add those two fields as items. Your results will contain just the data (not a list of EmpInfos, but a list of id, firstname)

BTW: PersistenceUnitProperties.WEAVING_FETCHGROUPS will be true by default when weaving is enabled.

-Tom

Rohit Banga wrote:
  Hi All

I am trying to fetch a specific set of attributes using Eclipselink fetchgroups.
To invoke the program I use the following command:

java -cp "%CLASSPATH%" -javaagent:<path of eclipselink.jar> <class name>

The SQL query generated is

SELECT id, firstname from empinfo;

which is correct.

However for each iteration of the loop, an extra SQL query is made. Am I missing anything here?


Here is the code I am using:


        DatabaseSession session = new DatabaseSessionImpl(login);

*        session.setProperty(PersistenceUnitProperties.WEAVING, "true");
session.setProperty(PersistenceUnitProperties.WEAVING_FETCHGROUPS, "true");
        session.login();
*
        DynamicHelper helper = new DynamicHelper(session);
        DynamicClassLoader dcl = helper.getDynamicClassLoader();

        Class<?> empType = dcl.createDynamicClass("jpatest.empinfo");
DynamicTypeBuilder typeBuilder = new JPADynamicTypeBuilder(empType, null, "empinfo");
        typeBuilder.setPrimaryKeyFields("id");
        typeBuilder.addDirectMapping("id", int.class, "id");
typeBuilder.addDirectMapping("firstname", String.class, "firstname");
        typeBuilder.addDirectMapping("lastname", String.class, "lastname");

        helper.addTypes(false, false, typeBuilder.getType());

ReadAllQuery query = new DynamicHelper(session).newReadAllQuery(typeBuilder.getType().getDescriptor().getAlias());
*        FetchGroup fg = new FetchGroup();
        fg.addAttribute("id");
        fg.addAttribute("firstname");
        query.setFetchGroup(fg);
*
        query.prepareCall(session, new DatabaseRecord());

        System.out.println("query: " + query.getSQLString());

List<DynamicEntity> emps = (List<DynamicEntity>) session.executeQuery(query);

        for (DynamicEntity entity : emps)
        {
            System.out.println("id:" + entity.get("id"));
            System.out.println("lastname:" + entity.get("lastname"));
            System.out.println();
        }


--
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


Back to the top