簡體   English   中英

Grails:保存對象時出錯,難以理解,不要使用'property'關鍵字

[英]Grails : Error saving object, hard to understand, don't use 'property' keyword

我得到這樣的域類:

class Offer {

    Property property
    Building building
    ExternalSystem externalSystem
    OfferStatus status
    Double offerPrice
    String currency
    String description
    String adventages
    Date offerDisponibility
    OfferNature offerNature
    Surfaces surfaces

    static hasMany = [units: Unit]

    static constraints = {
        building nullable: true
        externalSystem nullable: true
        description nullable: true
        adventages nullable: true
    }
}

將其填充到控制器中時:

def offer = new Offer()
offer.property = Property.get(1)
offer.building = Building.get(1)
offer.externalSystem = ExternalSystem.get(1)
offer.status = OfferStatus.get(1)
offer.offerPrice = 26860
offer.currency = 'EUR'
offer.description = 'WONDERFULL OFFER !'
offer.offerDisponibility = convertDate('12/04/2014')
offer.offerNature = OfferNature.read(1)
offer.surfaces = Surfaces.get(2)
offer.validate()

在驗證,保存或對此對象進行任何操作時,我會遇到奇怪的行為。 它是唯一這樣做的域對象域。

我得到了兩個不同的堆棧跟蹤,這是我問題中最奇怪的部分,第一次是在我第一次調用控制器時的第一個堆棧跟蹤:

| Error 2013-11-26 11:27:10,971 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver  - ArrayIndexOutOfBoundsException occurred when processing
 request: [GET] /Project/login/auth
0. Stacktrace follows:
Message: 0
    Line | Method
->>  116 | doCall    in com.project.security.LoginController$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

第二個是最后一個堆棧跟蹤:

| Error 2013-11-26 11:01:43,704 [http-bio-8080-exec-3] ERROR [/Project].[default]  - Servlet.service() for servlet [default] in context with path [/Project] threw exception [org.springframework.web.util.NestedServletException: Request processing failed; nested exception is groovy.lang.MissingMethodException: No signature of method: static java.lang.Math.max() is applicable for argument types: (java.lang.Integer, null) values: [4, null]
Possible solutions: max(int, int), max(double, double), max(float, float), max(long, long), min(int, int), wait()] with root cause
Message: No signature of method: static java.lang.Math.max() is applicable for argument types: (java.lang.Integer, null) values: [4, null]
Possible solutions: max(int, int), max(double, double), max(float, float), max(long, long), min(int, int), wait()
    Line | Method
->>  251 | doAppend              in org.apache.log4j.AppenderSkeleton
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     66 | appendLoopOnAppenders in org.apache.log4j.helpers.AppenderAttachableImpl
|    206 | callAppenders . . . . in org.apache.log4j.Category
|    391 | forcedLog             in     ''
|    856 | log . . . . . . . . . in     ''
|    192 | logMessage            in org.slf4j.impl.GrailsLog4jLoggerAdapter
|    167 | error . . . . . . . . in     ''
|    213 | error                 in org.apache.commons.logging.impl.SLF4JLog
|    200 | doFilter . . . . . .  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter              in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker . . . . . . in java.util.concurrent.ThreadPoolExecutor
|    615 | run                   in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . . . . in java.lang.Thread

那堆棧跟蹤是正確的?

我怎么解決這個問題?

如果我沒有調用save()或validate()消息,則沒有堆棧跟蹤。

添加failOnError:true並未更改堆棧跟蹤。

制造 :

if (!offer.save()) {
    log.error offer.errors
}

沒有改變堆棧跟蹤。

謝謝閱讀,

施奈特

經過幾個小時的研究,我終於解決了我的問題。

Property myProperty

不知道為什么,但'property'作為變量名(而不是域類名)似乎是一個保留關鍵字...

希望它會有用。

我發現了什么是錯的,什么是阻塞的。

好的,編輯我的類,我發現這是抽象類的問題。

記住我的Offer課程:

class Offer {

//  Property property
    Building building
    ExternalSystem externalSystem
    OfferStatus status
    Double offerPrice
    String currency
    String description
    String adventages
    Date offerDisponibility
    OfferNature offerNature
    Surfaces surfaces

    static hasMany = [units: Unit]

    static constraints = {
        building nullable: true
        externalSystem nullable: true
        description nullable: true
        adventages nullable: true
    }
}

注釋掉(上面類的成員變量)屬性解決了我的問題。

這是我的Property類:

class Property extends Content {

    AssetManager assetManager
    Owner owner
    Mandat mandat
    AddressContent addressContent

    static hasMany = [
        buildings: Building,
        propertyMandats: PropertyMandat
    ]

    static constraints = {
        assetManager nullable: true
        mandat nullable: true
        owner nullable: true
    }
}

這是我的Content類:

abstract class Content {

    String name
    ExternalSystem externalSystem

    static constraints = {
        name nullable: true
        externalSystem nullable: true
    }

    @Override
    public String toString() { name }

}

Hibernate不會生成任何“屬性”表,但是'content'表就像desc之后的那樣:

+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| id                  | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| version             | bigint(20)   | NO   |     | NULL    |                |
| external_system_id  | bigint(20)   | YES  | MUL | NULL    |                |
| name                | varchar(255) | YES  |     | NULL    |                |
| class               | varchar(255) | NO   |     | NULL    |                |
| sys_admin_client_id | bigint(20)   | YES  | MUL | NULL    |                |
| is_public_offer     | bit(1)       | YES  |     | NULL    |                |
| public_offer        | varchar(255) | YES  |     | NULL    |                |
| fund_id             | bigint(20)   | YES  | MUL | NULL    |                |
| address_content_id  | bigint(20)   | YES  | MUL | NULL    |                |
| asset_manager_id    | bigint(20)   | YES  | MUL | NULL    |                |
| mandat_id           | bigint(20)   | YES  | MUL | NULL    |                |
| owner_id            | bigint(20)   | YES  | MUL | NULL    |                |
| last_name           | varchar(255) | YES  |     | NULL    |                |
| mail                | varchar(255) | YES  |     | NULL    |                |
| phone_number1       | varchar(255) | YES  |     | NULL    |                |
| phone_number2       | varchar(255) | YES  |     | NULL    |                |
| agency_id           | bigint(20)   | YES  | MUL | NULL    |                |
| begin_date          | datetime     | YES  |     | NULL    |                |
| client_owner_id     | bigint(20)   | YES  | MUL | NULL    |                |
| end_date            | datetime     | YES  |     | NULL    |                |
| mandat_number       | varchar(255) | YES  |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+

我的Content類擴展了很多類。

所以,我想問題是關於沒有創建'property'表的事實。

為什么會這樣? 我的內容抽象類是錯誤的嗎?

關於您對content表的問題,

“在數據庫級別,Grails默認使用table-per-hierarchy映射和一個名為class的鑒別器列,因此父類(Content)及其子類(BlogEntry,Book等)共享同一個表。”

Grails框架 - 參考文檔-7.2.3 GORM中的繼承

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM