繁体   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