标签: grails-orm

使用joinTable进行Grails一对多映射

我有两个域类.一个是"合作伙伴",另一个是"客户".客户可以是合作伙伴的一部分,合作伙伴可以拥有一个或多个客户:

class Customer {
    Integer id
    String name
    static hasOne = [partner:Partner]
    static mapping = {
        partner joinTable:[name:'PartnerMap',column:'partner_id',key:'customer_id']
    }
}

class Partner {
    Integer id
    static hasMany = [customers:Customer]
    static mapping = {
        customers joinTable:[name:'PartnerMap',column:'customer_id',key:'partner_id']
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,每当我试图查看客户是否是合作伙伴的一部分时,就像这样:

def customers = Customer.list()
customers.each {
     if (it.partner) {
          println "Partner!"
     }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select this_.customer_id as customer1_162_0_, this_.company as company162_0_, this_.display_name as display3_162_0_, this_.parent_customer_id as parent4_162_0_, this_.partner_id as partner5_162_0_, this_.server_id as server6_162_0_, this_.status as status162_0_, …
Run Code Online (Sandbox Code Playgroud)

mapping grails groovy grails-orm

2
推荐指数
1
解决办法
6944
查看次数

如何实现软删除

任何人都可以告诉我什么是实现软删除的好方法?我可以deleted在班上找到一个属性,但我的问题是如何轻松忽略deleted = true我的搜索,列表等中的实例.

所以,而不是说Domain.findByDeleted(true)只是Domain.list()忽略已删除的实例,而不是说Domain.findByPropertyAndDeleted('property', true)只是说Domain.findByProperty('property').

有这么好的方法吗?

grails grails-orm grails-domain-class

2
推荐指数
1
解决办法
1210
查看次数

如何使用多个多对一关系保存GORM对象?

假设我有以下层次的域类.

class School {
   String name
   static hasMany = [teachers: Teacher, students: Student]
}

class Teacher {
   String name
   static belongsTo = [school: School]
   static hasMany = [students: Student]
}

class Student {
   String name
   static belongsTo = [school: School, teacher: Teacher]
}
Run Code Online (Sandbox Code Playgroud)

我尝试了两种不同的方法来拯救学校,老师和学生.

尝试1:

def school = new School(name: "School").save()
def teacher = new Teacher(name: "Teacher", school: school).save()
def student = new Student(name: "Student", school: school, teacher: teacher).save(flush: true)
Run Code Online (Sandbox Code Playgroud)

它看起来保存得当但是我跑的时候:

println(school.students*.name)
Run Code Online (Sandbox Code Playgroud)

它打印null.

所以我决定尝试不同的方法.

尝试2:

def school = …
Run Code Online (Sandbox Code Playgroud)

grails groovy grails-orm

2
推荐指数
1
解决办法
2861
查看次数

Grails GORM服务类测试withNewSession()

我试图更新我的测试,因为我对我的服务类进行了更改.在我添加的服务方法中("withNewSession")

Domain.withNewSession {
  .. ...
  domain.save()
}
Run Code Online (Sandbox Code Playgroud)

因为我正在使用"withNewSession",所以我得到方法缺少异常,因为这个域在我的测试类中被模拟.

grails integration-testing hibernate grails-orm

2
推荐指数
1
解决办法
1411
查看次数

Grails GORM:如何创建复合主键并将其用于表关系?

我有两个表,其中一个(遗留表:A)有两个字段应该作为复合外键,另一个(新表:B)应该使用复合主键作为each row:A has one row:B关系.如何根据GORM描述这些表格?

到目前为止,我已经能够创建一个反映旧表的域类:A

class A {

    ...
    //composite foreign key to link B class
    String className;
    String eventName;

    B b; //instance of B to be related

    static mapping = {
        table 'a_table';            
        id column: 'id';
        className column: 'class_name';
        eventName column: 'event_name';
        //b: ???
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个有效,但我无法创造new class:B和关系.

我试图将B声明为:

class B implements Serializable{

    static auditable = true;

    String name;
    String className;
    String eventName;

    static mapping = {
        //supposed to make a composite PK
        id …
Run Code Online (Sandbox Code Playgroud)

grails grails-orm

2
推荐指数
1
解决办法
1万
查看次数

如何在Grails应用程序中防止XSS攻击

在我的grails应用程序中,我正在从中提取文本params并将其用作我的域查询中的参数:

例:

def color = Colors.findByName(params.colorname)
Run Code Online (Sandbox Code Playgroud)

我想有人可以摆弄params.colorname参数来对我的mysql数据库运行错误的查询.

有哪些好的做法可以防止这些事情发生?

xss grails grails-orm

2
推荐指数
1
解决办法
2566
查看次数

GORM查询以查找最新的条目

ORM如何工作的新功能所以请耐心等待.我有一个域类

class MyClass()
{
    String myName
    Date mydate
}
Run Code Online (Sandbox Code Playgroud)

我用相同的字符串和不同的日期引导了几个例子.然后在我的gsp中,我对控制器方法执行ajax调用

def MyAjaxCall
{
    def classes = MyClass.findAll()
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能改变这一点,所以我只返回myName最新的mydate?谢谢

ajax grails grails-orm

2
推荐指数
2
解决办法
1641
查看次数

Grails GORM级联删除映射

如何on delete cascade使用外键GORM映射在数据库级别实现?

我不是指应用程序级别的级联删除(由GORM完成的顺序删除).

grails hibernate grails-orm

2
推荐指数
1
解决办法
5653
查看次数

在gorm/groovy上使用mongodb启动时的springboot初始化错误

使用MongoDB/GORM/Groovy on Java 1.7.0_55,gradle 1.11spring-boot-gradle-plugin:1.2.1.RELEASE.获取此springboot项目的启动问题.

我正在按原样运行项目,application.yml我的远程mongodb 的以下更改除外:

spring:
    mongodb:
        host: "10.160.8.1"
        databaseName: "citydb"
Run Code Online (Sandbox Code Playgroud)

**在启动时,我看到这个bean初始化错误 mappingMongoConverter

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.data.mongodb.core.convert.MongoConverter]: : Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.data.mongodb.core.mapping.MongoMappingContext]: : …
Run Code Online (Sandbox Code Playgroud)

groovy spring grails-orm mongodb spring-boot

2
推荐指数
1
解决办法
3885
查看次数

Grails GORM:更改外键列名称

我正在尝试更改Visitor表中用于Userid 的外键列名.该列现在命名user_id,我想将其更改为who_id.

最小User域类:

class User {

    static hasMany = [
        visitor: Visitor
    ]

    String uid

    ...

}
Run Code Online (Sandbox Code Playgroud)

最小Visitor域类:

class Visitor {

  static belongsTo = [user: User]

  ....

}
Run Code Online (Sandbox Code Playgroud)

题:

我试过mappedBy但没有成功,是否有另一种方法可以使用属性User作为外键Visitor

grails grails-orm jointable

2
推荐指数
1
解决办法
1159
查看次数