简体   繁体   English

Grails:在组合键上映射域类字段

[英]Grails: map domain class field on composite key

I have a domain class reverse engineered from a legacy table 我有一个从旧表反向工程的域类

class AuditLog {

    String className;
    String eventName;
    ...
    AuditEvent event; //need to add this one

    static mapping = {
        ...
        className column: 'class_name';
        eventName column: 'event_name';
        ...
    }
}

className contains the name of domain class that was changed (eg com.test.Class1) and eventName contains the name of the event performed (eg INSERT/UPDATE/DELETE). className包含已更改的域类的名称(例如com.test.Class1), eventName包含执行的事件的名称(例如INSERT / UPDATE / DELETE)。

I need to provide a human-readable description of what happened. 我需要提供发生情况的易于理解的描述。 So I created another domain class 所以我创建了另一个域类

class AuditEvent {

    String name;

    //these should make a composite key
    String className; 
    String eventName;

}

eg AuditEvent[name:"Row inserted into Class1", className:"com.test.Class1", eventName:"INSERT"]. 例如AuditEvent [name:“插入Class1的行”,className:“ com.test.Class1”,eventName:“ INSERT”]。
AuditEvent[name:"Row deleted from Class1", className:"com.test.Class1", eventName:"DELETE"] AuditEvent [名称:“从Class1中删除行”,className:“ com.test.Class1”,eventName:“ DELETE”]

What I want now is to be able to call AuditLog.get(1).event.name and this is where I'm stuck. 我现在想要的是能够调用AuditLog.get(1).event.name ,这就是我AuditLog.get(1).event.name

How do I describe the relation for the event field in AuditLog class to make it load the right AuditEvent object depending on AuditLog.className and AuditLog.eventName? 如何描述AuditLog类中event字段的关系,以使其根据AuditLog.className和AuditLog.eventName加载正确的AuditEvent对象?

Since the classname and eventname are in the AuditEvent object, you don't need them in the AuditLog class. 由于类名和事件名在AuditEvent对象中,因此在AuditLog类中不需要它们。 You will, however have to create/lookup the AuditEvent when you create your AuditLog instances. 但是,在创建AuditLog实例时,必须创建/查找AuditEvent。 ie. 即。 The two fields won't automatically be injected: 这两个字段不会自动注入:

new AuditLog(event: AuditEvent.findOrSaveWhere(className: .., eventName: ..)).save()

You may be better off not making the association in the classes, but rather have a method on the AuditLog class which looks up the AuditEvent: 您最好不要在类中建立关联,而最好在AuditLog类上有一个方法来查找AuditEvent:

class AuditEvent {
    // ..
    static AuditEvent get(className, eventName) {
        find 'from AuditEvent e where e.className = :className and e.eventName = :eventName', [className: className, eventName: eventName]
    }
}

class AuditLog {

    String className;
    String eventName;

    // ...

    String getEventName() {
        AuditEvent.get(className, eventName).name
    }
}

That would make your name lookup like: 这将使您的姓名查找像:

AuditLog.get(id).eventName

Why do you need a composite primary key for AuditEvent ? 为什么需要AuditEvent的复合主键?

What about hasOne ? 那么hasOne呢?

class AuditLog {
    static hasOne = [event: AuditEvent]
}

class AuditEvent {

    String name

    AuditLog auditLog 

}

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

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