jpamany-to-manyentity-bean

possible to return only one column using JPA


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


Solution

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