简体   繁体   English

JPA 2与公式派生外键的多对一

[英]JPA 2 Many-To-One with Formula Derived Foreign Key

Migrating an application from Hibernate 3, trying to map this relation in JPA 2 and not sure how it's handled. 从Hibernate 3迁移应用程序,尝试在JPA 2中映射此关系,但不确定如何处理。 In the original application there are these two objects: 在原始应用程序中,有两个对象:

TemplateCourse.java

CourseCatCode.java

The TemplateCourse has a many-to-one reference to CourseCatCode . TemplateCourse具有对CourseCatCode的多对一引用。 There is no foreign key in TemplateCourse , instead a formula is used to select an ID from a 3rd table based on some criteria, and the corresponding CourseCatCode is joined. TemplateCourse没有外键,而是使用一个公式根据一些条件从第三张表中选择一个ID,并连接了相应的CourseCatCode。

In the hibernate 3 application, the mapping looks like this: 在休眠3应用程序中,映射如下所示:

    <many-to-one name="courseCatCode" class="CourseCatCode" not-null="false" insert="false" update="false" lazy="false">
        <formula><![CDATA[
            (
                select
                    smdp_asbch_course.cat_code_id
                from
                    smdp_asbch_course smdp_asbch_course,
                    smdp_template_requirement smdp_template_requirement,
                    smdp_template smdp_template,
                    smdp_degree smdp_degree
                where
                    smdp_degree.degree_id = smdp_template.degree_id
                    and smdp_template.template_id = smdp_template_requirement.template_id
                    and smdp_template_requirement.requirement_id = requirement_id
                    and smdp_asbch_course.school_id = smdp_degree.school_id
                    and smdp_degree.degree_type_code = smdp_asbch_course.degree_type_code
                    and smdp_asbch_course.before_date > SYSDATE
                    and translate(upper(smdp_asbch_course.course_number), 'A-/ ', 'A') = translate(upper(course_number), 'A-/ ', 'A')
            )
        ]]></formula>
    </many-to-one>

Can we do something similar in JPA 2 to map this relation? 我们可以在JPA 2中做类似的事情来映射这种关系吗? Our JPA 2 provider is also Hibernate, so if it requires something hibernate specific that is okay. 我们的JPA 2提供程序也是休眠的,因此,如果需要某些特定于休眠的东西就可以了。

Have you tried 你有没有尝试过

@JoinFormula(referencedColumnName="cat_code_id", value="(select smdp_asbch_course.cat_code_id " +
                                                       "from smdp_asbch_course " +                                                            smdp_asbch_course, " +
                                                       "smdp_template_requirement smdp_template_requirement, " +
                                                       "smdp_template " +
                                                       "smdp_template, " +
                                                       "smdp_degree smdp_degree " +
                                                       "where smdp_degree.degree_id = smdp_template.degree_id " +
                                                       "  and smdp_template.template_id = smdp_template_requirement.template_id " +
                                                       "  and smdp_template_requirement.requirement_id = requirement_id " +
                                                       "  and smdp_asbch_course.school_id = smdp_degree.school_id " +
                                                       "  and smdp_degree.degree_type_code = smdp_asbch_course.degree_type_code " +
                                                       "  and smdp_asbch_course.before_date > SYSDATE " +
                                                       "  and translate(upper(smdp_asbch_course.course_number), 'A-/ ', 'A') = translate(upper(course_number), 'A-/ ', 'A'))")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM