简体   繁体   English

Grails map 域 class 到 oracle 数据库中已经存在的视图

[英]Grails map domain class to a view that already exists in oracle database

I have a database view already created called FACULTY_VIEW and would like to map a grails domain class to it.我已经创建了一个名为 FACULTY_VIEW 的数据库视图,并且想要 map grails 域 class 到它。 Here is the Domain:这是域:

class FacultyInformation {

    def faculty_pidm
    def faculty_person_id
    def faculty_first_name
    def faculty_last_name

    static mapping = {
        table 'FACULTY_VIEW'
        version false
        cache 'read-only',inlcude:'non-lazy'

        faculty_pidm column:'FACULTY_PIDM'
        faculty_person_id column:'FACULTY_PERSON_ID'
        faculty_first_name column:'FACULTY_FIRST_NAME'
        faculty_last_name column:'FACULTY_LAST_NAME'        
    }

}

And here is my Controller snippet for that Domain that is trying to do a search:这是我尝试搜索的域的 Controller 片段:

facultySearchList = FacultyInformation.createCriteria().list (max: params.max, offset: params.offset, sort:params.sort, order: params.order) {
    ilike("faculty_last_name", searchTerm )
}

Upon start-up I get this error message(which I would expect):启动时,我收到此错误消息(这是我所期望的):

| | Error 2015-11-11 14:58:33,675 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table FACULTY_VIEW add id number(19,0) not null |错误 2015-11-11 14:58:33,675 [localhost-startStop-1] 错误 hbm2ddl.SchemaUpdate - 不成功:更改表 FACULTY_VIEW 添加 ID 号(19,0)而不是 null | Error 2015-11-11 14:58:33,677 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - ORA-00942: table or view does not exist错误 2015-11-11 14:58:33,677 [localhost-startStop-1] 错误 hbm2ddl.SchemaUpdate - ORA-00942:表或视图不存在

And then once I perform the search I get this error mesaage:然后一旦我执行搜索我得到这个错误消息:

org.hibernate.QueryException: could not resolve property: faculty_last_name of: facultyinformation.FacultyInformation org.hibernate.QueryException:无法解析属性:faculty_last_name of:facultyinformation.FacultyInformation

I of course don't plan on inserting or deleting from this view, as I just was to search it.我当然不打算从此视图中插入或删除,因为我只是要搜索它。

I'm guessing in one way or another this is a hibernate issue / configuration issue.我以某种方式猜测这是一个 hibernate 问题/配置问题。

Any help would be greatly appreciated:-)任何帮助将不胜感激:-)

Part 2: Here is my db changelog to create the view:第 2 部分:这是我创建视图的数据库变更日志:

databaseChangeLog = {
        changeSet(author: "jdannucci (generated)", id: "1447432810497-1") {
            createView("""
      SELECT DISTINCT SIRASGN_PIDM AS faculty_pidm, SPRIDEN_ID AS faculty_person_id, SPRIDEN_FIRST_NAME AS faculty_first_name, SPRIDEN_LAST_NAME AS faculty_last_name
    FROM SSBSECT , SCBCRSE, SCRLEVL SC, SIRASGN SA, SPRIDEN
    WHERE SSBSECT_TERM_CODE = (SELECT MAX(STVTERM_CODE) FROM STVTERM
                                            WHERE TRUNC(SYSDATE) BETWEEN TRUNC(STVTERM_START_DATE) AND TRUNC(STVTERM_END_DATE))
    AND SSBSECT_SUBJ_CODE=SCBCRSE_SUBJ_CODE
    AND SSBSECT_CRSE_NUMB=SCBCRSE_CRSE_NUMB
    AND SSBSECT_CRN = SIRASGN_CRN
    AND SSBSECT_SUBJ_CODE != 'FE'
    AND SIRASGN_PRIMARY_IND = 'Y'
    AND SSBSECT_TERM_CODE = SIRASGN_TERM_CODE
    AND SCRLEVL_LEVL_CODE = '01'
    AND SCRLEVL_SUBJ_CODE = SSBSECT_SUBJ_CODE
    AND SCRLEVL_CRSE_NUMB = SSBSECT_CRSE_NUMB
    AND SCRLEVL_EFF_TERM = (SELECT MAX(SCRLEVL_EFF_TERM)
                    FROM SCRLEVL
                    WHERE SCRLEVL_SUBJ_CODE = SC.SCRLEVL_SUBJ_CODE
                    AND  SCRLEVL_CRSE_NUMB = SC. SCRLEVL_CRSE_NUMB
                    AND SCRLEVL_EFF_TERM <= SSBSECT_TERM_CODE)
    AND SCBCRSE_EFF_TERM = (SELECT MAX(SCBCRSE_EFF_TERM)
         FROM SATURN.SCBCRSE X
         WHERE X.SCBCRSE_SUBJ_CODE=SSBSECT_SUBJ_CODE
         AND X.SCBCRSE_CRSE_NUMB=SSBSECT_CRSE_NUMB
         AND X.SCBCRSE_EFF_TERM <= SSBSECT_TERM_CODE)
    AND SPRIDEN_PIDM = SIRASGN_PIDM
    AND SPRIDEN_CHANGE_IND IS NULL
    ORDER BY faculty_last_name
    """, viewName: 'FACULTY_VIEW')
         }
    }

And here is my new Domain:这是我的新域:

package facultyinformation

class FacultyInformation {

    def faculty_pidm
    def faculty_person_id
    def faculty_first_name
    def faculty_last_name

    static mapping = {
        table 'FACULTY_VIEW'
        version false
        cache 'read-only',inlcude:'non-lazy'

        faculty_pidm column:'FACULTY_PIDM'
        faculty_person_id column:'FACULTY_PERSON_ID'
        faculty_first_name column:'FACULTY_FIRST_NAME'
        faculty_last_name column:'FACULTY_LAST_NAME'        
    }

}

Datasource:数据源:

development {
        dataSource {
            dbCreate = "none" // one of 'create', 'create-drop','update'
            url = "jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:PPRD"

            dialect = "org.hibernate.dialect.Oracle10gDialect"
        }
    }

Here's controller code that gives me the error:这是给我错误的 controller 代码:

facultySearchList = FacultyInformation.createCriteria().list (max: params.max, offset: params.offset, sort:params.sort, order: params.order) {
                        ilike("faculty_last_name", searchTerm )
                }

Here's the error:这是错误:

org.hibernate.QueryException: could not resolve property: faculty_last_name of: facultyinformation.FacultyInformation

Getting very frustrated.. I'm wondering if it would just be easier to avoid hibernate all together???变得非常沮丧.. 我想知道一起避免 hibernate 是否会更容易???

Based on this part of the error message, the database connection doesn't see your view: 根据错误消息的这一部分,数据库连接看不到您的视图:

ORA-00942: table or view does not exist 

Obviously, make sure the view does exist. 显然,请确保该视图确实存在。

If the view does exist, check the datasource configuration and ensure it is using to the right database and that the user specified in the connection parameters has access to the view. 如果该视图确实存在,请检查数据源配置,并确保它已用于正确的数据库,并且连接参数中指定的用户有权访问该视图。

I created some views with the db-migration plugin by executing the SQL explicitly, ie using sql('''CREATE OR REPLACE VIEW instead of createView(""" 我通过显式执行SQL使用db-migration插件创建了一些视图,即使用sql('''CREATE OR REPLACE VIEW而不是createView("""

Also, you'll want to exclude your views from GORM table updates by implementing DdlFilterConfiguration as described in this presentation by Burt Beckwith: http://www.slideshare.net/gr8conf/gorm-burt-beckwith2011 另外,您将希望按照Burt Beckwith的本演示文稿中所述实现DdlFilterConfiguration ,从而将视图从GORM表更新中排除: http : DdlFilterConfiguration

In this case, the easiest way is to add an ID column to the view.在这种情况下,最简单的方法是向视图添加一个 ID 列。 Since all of the tables in the query have a unique sequence number index, you can add the following column to the view (I don't want to reveal the real proprietary column names, so we'll call the index <table_name>_ID, even though that's fake).由于查询中的所有表都有一个唯一的序列号索引,您可以将以下列添加到视图中(我不想透露真正的专有列名,所以我们将索引称为 <table_name>_ID,尽管那是假的)。

SSBSECT_ID||'-'||SCBCRSE_ID||'-'||SCRLEVL_ID||'-'||SIRASGN_ID||'-'||SPRIDEN_ID as SURROGATE_ID

Then you change the domain class so that it looks like this:然后更改域 class,使其如下所示:

package facultyinformation

class FacultyInformation { class 师资信息{

String id
def faculty_pidm
def faculty_person_id
def faculty_first_name
def faculty_last_name

static mapping = {
    table 'FACULTY_VIEW'
    version false
    id column: 'SURROGATE_ID'
    cache 'read-only',inlcude:'non-lazy'

    faculty_pidm column:'FACULTY_PIDM'
    faculty_person_id column:'FACULTY_PERSON_ID'
    faculty_first_name column:'FACULTY_FIRST_NAME'
    faculty_last_name column:'FACULTY_LAST_NAME'        
}

} }

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

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