2

I have an Open JPA entity and it successfully connects a many-to-many relationship. Right now I successfully get the entire table, but I really only want the ID's from that tables. I plan on calling the database later to reconstruct the entities that I need (according to the flow of my program). I need only the ID's (or one column from that table).
1) Should I try and restrict this in my entity beans, or in the stateless session beans that I will be using to call the entity beans 2) If I try and do this using JPA, how can I specify that I only get back the ID's from the table, instead of the whole table? So far looking online, I don't see a way that you can do this. So I am guessing there is no way to do this. 3) If I simply just manipulate the return values, should I create a separate class that I will be returning to the user that will return only the required id list to the user?

I could be completely wrong here, but from the looks of it, I don't think there is a simple way to do this using JPA and I will have to return a custom object instead of the entity bean to the user (this custom object would only hold the id's as opposed to the whole table as it currently does)

Any thoughts... I don't think this is really relevant, but people are always asking for code, so here you go...

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="QUICK_LAUNCH_DISTLIST",
        joinColumns=@JoinColumn(name="QUICK_LAUNCH_ID"),
        inverseJoinColumns=@JoinColumn(name="LIST_ID"))
private List<DistributionList> distributionlistList;

Currently how I get the entire collection of records. Remember I only want the id...

    try
    {
        //int daSize = 0;
        //System.out.println("Testing 1.2..3...! ");
        qlList = emf.createNamedQuery("getQuickLaunch").getResultList();            
    }

This is how I call the Entity beans. I am thinking this is where I will have to programatically go through and create a custom object similar to the entity bean (but it just has the ID's and not the whole table, and attempt to put the id's in there somewhere.

What are your thoughts?

Thanks

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • Right now you get back the "entire table", which I take to mean that distributionlistList contains all of the related DistributionList objects you want. What's wrong with iterating through each DistributionList in distributionlistList, calling its .getId(), and storing those in a new list? JPA returns the whole table because its abstraction is that entire objects, not arbitrary columns, are stored in the database. Usually breaking the abstraction is not the right answer. – Hammer Bro. May 03 '12 at 16:17
  • Well, I would need to do that. But I would also need to create a new object and return that the user instead of the quickLaunch Object. Which is doable. I guess for this particular instance, It would be great to have that functionality. – SoftwareSavant May 03 '12 at 16:44

2 Answers2

4

I believe I just figured out the best solution to this problem.
This link would be the answer: my other stack overflow answer post

But for the sake of those too lazy to click on the link I essentially used the @ElementCollection attribute...

@ElementCollection(fetch=FetchType.EAGER)
        @CollectionTable(name="QUICK_LAUNCH_DISTLIST",joinColumns=@JoinColumn(name="QUICK_LAUNCH_ID"))
        @Column(name="LIST_ID")
private List<Long> distListIDs;

That did it.

Community
  • 1
  • 1
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
1

Sounds like you want something like this in your quickLaunch class:

@Transient
public List<Integer> getDistributionListIds () {
    List<Integer> distributionListIds = new LinkedList<Integer>();
    List<DistributionList> distributionlistList = getDistributionlistList();
    if (distributionlistList != null) {
        for (DistributionList distributionList : distributionlistList)
            distributionListIds.add(distributionList.getId());
    }
    return distributionListIds;
}

I had to guess a little at the names of your getters/setters and the type of DistributionList's ID. But basically, JPA is already nicely handling all of the relationships for you, so just take the values you want from the related objects.

Hammer Bro.
  • 965
  • 1
  • 10
  • 23
  • That's what I was thinking. I would need to replace my many to many relationship right? I could get just get rid of it, and then just get those id's... How are you calling getDistributionLIsts? I guess I would need to keep my ManyToMany. – SoftwareSavant May 03 '12 at 17:43
  • You could potentially change the definition to OneToMany, but you need the ToMany part. After all, you're logically trying to get all of the potentially-many DistributionLists associated with any given QuickLaunch, right? That's OneToMany. But if any DistributionList could belong to more than one QuickLaunch, you're back at ManyToMany. My hunch is that your relationships were set up correctly, and you're just not used to operating them yet. getDistributionLists is the getter for `private List distributionlistList`. If getDistributionListIds works, why change anything else? – Hammer Bro. May 03 '12 at 20:49
  • Would I need to set up an entity manager in my JPA object? How would I actually get the data needed? I tested the transient type btw, and I don't see it in the return results? – SoftwareSavant May 04 '12 at 12:02