JPA2向表添加引用约束使得使用延迟提取的条件查询变得复杂,需要建议

时间:2011-01-18 01:45:02

标签: java jpa jpa-2.0

以下是我认为非常简单的问题。问题的根源是我的无知,不是看代码而是建议。

表:使用旧实体映射的Ininvhst(库存架构库存历史)列ihtran(库存历史转移代码):

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

ihtran确实是表Intrnmst的外键(“库存转移大师”,其中包含“转移代码”列表)。这没有在数据库中表达,因此在Ininvhst重新生成JPA2实体类时产生了引用约束:

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

现在我以前使用JPA2从Ininvhst表中选择记录/(Ininvhst实体),其中“ihtran”是一组值之一。我用in.value()来做这个...这里是一个片段:

        cq = cb.createQuery(Ininvhst.class);
        ...
        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);

我的问题是,Ininvhst曾经包含一个名为ihtran的字符串,但现在它包含了Ininvhst ...所以我现在需要一个路径表达式:

    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);
    }

我可以将ihtran添加回实体以及同时引用“IHTRAN”的连接列吗?或者我应该使用投影以某种方式返回Ininvhst以及ihtran字符串,该字符串现在是Intrnmst实体的一部分。或者我应该使用投影返回Ininvhst并以某种方式限制Intrnmst仅仅是ihtran字符串。

更多信息:我在Web应用程序中使用所选Ininvhst对象的结果列表,包含Ininvhst对象列表的类将转换为json对象。可能有很多序列化方法可以导航对象图,问题是我当前的提取策略是懒惰的,因此它会遇到连接实体(Intrnmst intrnmst),并且此时没有可用的实体管理器。此时我已阻止对象序列化连接列,但现在我错过了一个关键数据。

我想我已经说了太多但不太了解我不知道JPA专家需要什么。我想要的是我的原始对象既有一个字符串对象又能够加入同一列(ihtran),但如果这是不可能或不可取的,我想听听我应该做什么以及为什么。

伪代码/英文非常好。

1 个答案:

答案 0 :(得分:1)

  

我可以将ihtran添加回实体吗?   以及两者的连接列   引用“IHTRAN”?

是。只需将其中一个设为只读(insertable / updateable = false)

如果您正在使用EclipseLink,您还可以为外键添加QueryKey。

如果在序列化之前访问该关系,那么它将可用。否则将其设为EAGER,或者在查询中加入获取它。