简体   繁体   English

Grails:保存对象时出错,难以理解,不要使用'property'关键字

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

I get a domain class like that : 我得到这样的域类:

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

When populating it into a controller : 将其填充到控制器中时:

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()

I get a strange behaviour when validating, saving or whatever on this object. 在验证,保存或对此对象进行任何操作时,我会遇到奇怪的行为。 It is the only domain object domain wich do that. 它是唯一这样做的域对象域。

I get two different stacktrace, tha's the most strange part of my problem, the first stacktrace apear when I'm calling my controller the first time : 我得到了两个不同的堆栈跟踪,这是我问题中最奇怪的部分,第一次是在我第一次调用控制器时的第一个堆栈跟踪:

| 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

The second is alway the last stacktrace : 第二个是最后一个堆栈跟踪:

| 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

So which stacktrace is the correct ? 那堆栈跟踪是正确的?

How can I solve this ? 我怎么解决这个问题?

No stacktrace if I'm not calling save() or validate() message. 如果我没有调用save()或validate()消息,则没有堆栈跟踪。

Adding failOnError: true did not change the stacktrace. 添加failOnError:true并未更改堆栈跟踪。

Making : 制造 :

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

did not change the stacktrace. 没有改变堆栈跟踪。

Thanks for reading, 谢谢阅读,

Snite 施奈特

After hours of research, I finally solve my issue. 经过几个小时的研究,我终于解决了我的问题。

Property myProperty

Don't know why, but 'property' as variable name (and not domain class name) seems to be a reserved keyword... 不知道为什么,但'property'作为变量名(而不是域类名)似乎是一个保留关键字...

Hope it will be usefull. 希望它会有用。

I found out what is wrong and and what was blocking . 我发现了什么是错的,什么是阻塞的。

Ok, editing my classes, I see that is a problem about abstract class. 好的,编辑我的类,我发现这是抽象类的问题。

Remember my Offer class : 记住我的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
    }
}

Commenting out the (Member Variable of the above class ) Property solve my problem. 注释掉(上面类的成员变量)属性解决了我的问题。

Here is my Property class : 这是我的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
    }
}

And here is my Content class : 这是我的Content类:

abstract class Content {

    String name
    ExternalSystem externalSystem

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

    @Override
    public String toString() { name }

}

Hibernate does not generate any 'property' table, but the 'content' table is like that after a desc : 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    |                |
+---------------------+--------------+------+-----+---------+----------------+

My Content class extends lot of classes. 我的Content类扩展了很多类。

So, I suppose the problem is about the fact no 'property' table is created. 所以,我想问题是关于没有创建'property'表的事实。

Why this behavior? 为什么会这样? And is this something wrong about my content abstract class? 我的内容抽象类是错误的吗?

About your question of the content table, 关于您对content表的问题,

"At the database level Grails by default uses table-per-hierarchy mapping with a discriminator column called class so the parent class (Content) and its subclasses (BlogEntry, Book etc.), share the same table." “在数据库级别,Grails默认使用table-per-hierarchy映射和一个名为class的鉴别器列,因此父类(Content)及其子类(BlogEntry,Book等)共享同一个表。”

The Grails Framework-Reference Documentation-7.2.3 Inheritance in GORM Grails框架 - 参考文档-7.2.3 GORM中的继承

Hope this helps. 希望这可以帮助。

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

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