0

I am looking for a way to request specific columns and have the foreign object present in the root object using CriteriaBuilder. Here is the context: I have EntityA

@Entity
@Table(name = "ENTITY_A")
public class EntityA {
    int id;
    int entityBKey;
    EntityBObject entityBObject;
    int AColumn1;
    int AColumn2;

    @Basic
    public Long getEntityBKey() {
        return entityBKey;
    }

    @ManyToOne
    @JoinColumn(name = "ENTITY_B_FK")
    public EntityBObject getProgramType() {
        return entityBObject;
    }

    @Basic
    @Column(name = "COLUMN_1")
    public String getAColumn1() {
        return AColumn1;
    }
    ...
}

Then I have EntityB

public class EntityB {
    int id;
    int BColumn1;
    int BColumn2;
    ...
}

Now, I want to request column AColumn1 from EntityA and column BColumn1 from EntityB, while having the object EntityB inside the EntityA. How can I achieve this ?

How can I modify the following to get a partial EntityA with an EntityB inside ?

public List<EntityA> findAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<EntityA> criteria = cb.createQuery(EntityA.class);
    Root<EntityA> root = criteria.from(EntityA.class);
    criteria.select(root);

    return em.createQuery(criteria).getResultList(); 
}

Thanks !

Edit @Tassos Bassoukos Yes, that's what I ended up doing, but it would get really messy when the request gets more complex. Ex.: Pull customers with their orders, with items for each orders. There would be so much java to achieve this, I though it could be automated so my object are automatically populated.

public List<EntityA> findAll() {
    ArrayList<EntityA> result = new ArrayList<>();
    Query q = em.createQuery("select eA, eB, from EntityA eA, EntityB eB where eA.key = eB.key");

    @SuppressWarnings("unchecked")
    List<Object[]> abc = q.getResultList();
    for (Object[] array : abc) {
        EntityA eA = (EntityA) array[0];
        EntityB eB = (EntityB) array[1];
        eA.setEntityB(eB);
        result.add(pe);
    }

    return result;
}
  • Would something like [this](http://stackoverflow.com/questions/6877857/jpa-query-that-returns-multiple-entities/6880396#6880396) do the trick? – Tassos Bassoukos Oct 14 '15 at 23:27
  • @TassosBassoukos No :/ Thats what I ended up doing (edited post above), but I am looking for a way that automate the population of sub objects, I also don't want to create extra classes or constructors. Is it do-able ? – user3850453 Oct 15 '15 at 13:47

1 Answers1

-1

First, why do you want a partial entity? That does not make sense from an OO perspective. Is there an actual, specific requirement for this?

Secondly, do you want entities or columns of entities? You can do both with CriteriaBuilder, but you need to be clear on a) what you want to achieve, b) why you want to achieve it.

Thirdly, there's JOIN FETCH.

Community
  • 1
  • 1
Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • Q1: The database could contains so many columns with so much data, that it can impact performance. Q2.a) I want an objcet entityA that represent following {colA1=abc, entityB={colB1=111, colB2=222}}. Q2.b) Replicate an sql request to parse response automaticaly in object. Q3: I tried to look around and couldnt find a complete example, how do you code using Entity_ object ? How would you mark the annotation of the entity ? – user3850453 Oct 16 '15 at 17:28
  • Well, you can create a query like `select eA.colA1, eB from EntityA eA, EntityB eB join fetch eB.colleC where ...` – Tassos Bassoukos Oct 16 '15 at 19:44
  • You would want a partial entity if your system relies on a SQL database that doesn't care about Object Orientation. If the query joins on multiple tables because of the way the domain model is written and you're only looking for a foreign key, then you have a scalability issue. – Sandy Simonton Sep 26 '17 at 16:51