JPA2 adding referential contraint to table complicates criteria query with lazy fetch, need advice

Posted by Quaternion on Stack Overflow See other posts from Stack Overflow or by Quaternion
Published on 2011-01-18T01:45:01Z Indexed on 2011/01/18 1:53 UTC
Read the original article Hit count: 219

Filed under:
|
|

Following is a lot of writing for what I feel is a pretty simple issue. Root of issue is my ignorance, not looking so much for code but advice.

Table: Ininvhst (Inventory-schema inventory history) column ihtran (inventory history transfer code) using an old entity mapping I have:

@Basic(optional = false)
@Column(name = "IHTRAN")
private String ihtran;

ihtran is really a foreign key to table Intrnmst ("Inventory Transfer Master" which contains a list of "transfer codes"). This was not expressed in the database so placed a referential constraint on Ininvhst re-generating JPA2 entity classes produced:

@JoinColumn(name = "IHTRAN", referencedColumnName = "TMCODE", nullable = false)
@ManyToOne(optional = false)
private Intrnmst intrnmst;

Now previously I was using JPA2 to select the records/(Ininvhst entities) from the Ininvhst table where "ihtran" was one of a set of values. I used in.value() to do this... here is a snippet: cq = cb.createQuery(Ininvhst.class); ... In in = cb.in(transactionType); //Get in expression for transacton types for (String s : transactionTypes) { //has a value in = in.value(s);//check if the strings we are looking for exist in the transfer master } predicateList.add(in);

My issue is that the Ininvhst used to contain a string called ihtran but now it contains Ininvhst... So I now need a path expression:

    this.predicateList = new ArrayList<Predicate>();
    if (transactionTypes != null && transactionTypes.size() > 0) { //list of strings has some values
        Path<Intrnmst> intrnmst = root.get(Ininvhst_.intrnmst); //get transfermaster from Ininvhst
        Path<String> transactionType = intrnmst.get(Intrnmst_.tmcode); //get transaction types from transfer master
        In<String> in = cb.in(transactionType); //Get in expression for transacton types
        for (String s : transactionTypes) { //has a value
            in = in.value(s);//check if the strings we are looking for exist in the transfer master
        }
        predicateList.add(in);
    }

Can I add ihtran back into the entity along with a join column that is both references "IHTRAN"? Or should I use a projection to somehow return Ininvhst along with the ihtran string which is now part of the Intrnmst entity. Or should I use a projection to return Ininvhst and somehow limit Intrnmst just just the ihtran string.

Further information: I am using the resulting list of selected Ininvhst objects in a web application, the class which contains the list of Ininvhst objects is transformed into a json object. There are probably quite a few serialization methods that would navigate the object graph the problem is that my current fetch strategy is lazy so it hits the join entity (Intrnmst intrnmst) and there is no Entity Manager available at that point. At this point I have prevented the object from serializing the join column but now I am missing a critical piece of data.

I think I've said too much but not knowing enough I don't know what you JPA experts need. What I would like is my original object to have both a string object and be able to join on the same column (ihtran) and have it as a string too, but if this isn't possible or advisable I want to hear what I should do and why.

Pseudo code/English is more than fine.

© Stack Overflow or respective owner

Related posts about java

Related posts about jpa