这可以在createCriteria()中转换吗?
SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10
Run Code Online (Sandbox Code Playgroud)
我知道有一个'in'运算符,这是我到目前为止所拥有的:
def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
node {
eq('type', 'act')
}
maxResults(10)
}
Run Code Online (Sandbox Code Playgroud)
只是想看看这是否可行.否则,我猜这在HQL中是可能的吗?
我在Grails中有一些投影问题.能帮我复习一下并为我提出解决方案吗?
我想查询许多表上的数据,这些表具有多对一的关系,并在两个表上的某些属性上进行投影.例如:
class Person {
int id
String name
String address
static hasMany = [cars : Car]
}
class Car {
int id
String brand
long price
Person owner
static belongsTo = [owner : Person]
}
Run Code Online (Sandbox Code Playgroud)
那么,我如何只使用一个查询withCriteria(应用投影)来获取指定汽车的信息(包括品牌,价格和所有者名称)?是否可以使用:
Car.withCriteria {
projections {
property("brand")
property("price")
property("owner.name")
}
eq("id", carId)
}
Run Code Online (Sandbox Code Playgroud)我可以使用投影获取一个指定人员的信息以及他所有车辆的名称吗?例如:[01,Perter,01 Street A,[Mercedes,Toyota,Ducatti]]?
特殊情况:(与上述人员类别)
一个人可以加入许多组织,组织可以有一个"父"组织(反之亦然,一个组织可以有许多其他依赖组织).但是有一条规则:一个人只能加入一个特定组织的一个儿童组织.因此,对于给定的组织O和人P,获得P的信息的最快方式是什么,以及具有P作为成员的O的依赖组织的名称.我更喜欢使用Grails投影.
这是数据模型:
class Person {
int id
String name
String address
static hasMany = [joinedOrgs : Organization]
}
class Organization {
int id
String name
Organization parentOrg
static …Run Code Online (Sandbox Code Playgroud)很抱歉,如果这是一个新手问题,但我非常感谢社区可以提供的关于我在Grails服务,LocationService中存储以下方法的问题的任何见解.
Location locate(String target, String locator, Application app, boolean sync = true) {
if (!target) throw new IllegalArgumentException("Illegal value for msid: " + target)
def locRequest = Request.create(target, Type.LOCATE)
if (!locRequest.save()) {
return Location.error(target, "Error persisting location request")
}
locationSource.locateTarget(target, locator, app, sync)
}
Run Code Online (Sandbox Code Playgroud)
我有一个域类,Request,以及默认的GORM方法也有一些额外的域方法,例如.下面的create()方法
@EqualsAndHashCode
class Request {
String reference
String msid
Type type
Status status
Destination destination
DateTime dateCreated
DateTime dateCompleted
static create(String msid, Type type, Destination destination = Destination.DEFAULT) {
new Request(reference: reference(type), type: type, status: Status.INITIATED, …Run Code Online (Sandbox Code Playgroud) 目前,应用程序正在处理数据库中的多个模式,因此我们有一个存储主表的公共模式,而我们存储客户特定数据的其他模式.
所以,具体情况就像
(下表仅供参考)
主表AnimalType这是居住common模式,而Animal表是适用于所有客户端的模式,如schema1,schema2... schemaN.
我们正在使用Grails哪个默认使用Hibernate,关系就像
class AnimalType {
String type
static mapping = {
datasources(['index'])
}
}
class Animal {
String name
AniamlType animalType
}
Run Code Online (Sandbox Code Playgroud)
因此,当我启动应用程序时,它显示以下异常:
引起:org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败; 嵌套异常是org.hibernate.MappingException:表动物的关联引用了一个未映射的类:org.sample.AnimalType
我从中理解的是因为,Animal试图引用AnimalType它自己的模式,但AnimalType在那里不存在.
所以,基本上我想映射Animal到AnimalType指向common架构.
Grails中的语法如下所示
class Animal {
String name
@(POINTING TO COMMON SCHEMA)
AnimalType animalType
}
Run Code Online (Sandbox Code Playgroud) 我尝试按照此引用,现在这是我的域代码:
class SnbrActVector {
long nid
String term
double weight
static mapping = {
version false
nid index:'Nid_Idx'
}
static constraints = {
term(blank:false)
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是为nid列添加索引键.我删除了现有的表并再次运行应用程序,然后重新创建表.然而,当我检查索引列表中,我看不到的Nid_Idx,只有PRIMARY.我是否必须手动创建索引并Nid_idx在我的mysql数据库中命名?
我想得到一个域对象的实际实例.也就是说,我需要序列化对象,并且我试图在httpinvoker链的两侧使用域对象.有没有办法获得一个没有任何grails连接的完全加载的域对象,以便我可以序列化它?
class Contact {
String name
String number
}
class Message {
String text
String number
Contact contactInfo //If any
}
Run Code Online (Sandbox Code Playgroud)
我需要加入Message.number = Contact.number.有关使用非主键列在Grails/GORM中创建关联的任何想法?
我正在使用Grails,并且我有一个具有多个hasMany属性的域模型到同一个域类,如下所示:
static hasMany = [ posts : Post, likes : Post, dislikes : Post ]
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我在帖子列表中添加内容时,它也会以某种方式将其变成喜欢和不喜欢的列表.至少,当我遍历每个列表时,它就是这样的样子.
我认为问题是我在Post域中也有以下关系:
static belongsTo = [ contributer : Contributer ]
Run Code Online (Sandbox Code Playgroud)
配置这些关系以使我的模型工作的最佳方法是什么?有什么建议?
@Wayne,
我也试过使用你的测试,它成功通过了.所以,我唯一能想到的是我的PostController中的save方法有问题.我已粘贴下面的相关代码(我正在使用Spring Security Core插件,而我的Contributer类扩展了使用该插件创建的User类):
@Secured(['IS_AUTHENTICATED_FULLY'])
def save = {
def props = [title:params.title, post:params.post, category:Category.get(params.category.id)]
def user = Contributer.get(springSecurityService.principal.id)
def postInstance = new Post(props)
postInstance.contributer = user
if (postInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'post.label', default: 'Post'), postInstance.id])}"
redirect(action: "show", id: postInstance.id)
}
else {
render(view: "create", model: [postInstance: postInstance])
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用grails mongo插件在mongodb中持久化spring-security-acl域对象.执行以下代码行时
aclUtilService.addPermission Phone.class, phoneInstance.id, new PrincipalSid(username), BasePermission.ADMINISTRATION
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
此实现的GORM目前不支持像[executeQuery]这样的基于字符串的查询.使用标准代替.. Stacktrace如下:消息:此实现的GORM目前不支持像[executeQuery]这样的基于字符串的查询.改用标准.
任何艰难的?
**Grails Configuration Details:**
app.grails.version=2.0.3
app.name=eateri
app.servlet.version=2.5
app.version=0.1
plugins.mongodb=1.0.0.RC5
plugins.spring-security-acl=1.1
plugins.spring-security-core=1.2.7.2
Run Code Online (Sandbox Code Playgroud) 使用grails 3.3.8
如果在发出保存的服务方法中抛出了捕获的异常,则不会保存任何domainclass.save(flush:true,failOnError:true).即
try {
//some code that throws exception
} catch (Exception exception) {
print 'some message'
}
domainclass.save(flush:true,failOnError:true)
Run Code Online (Sandbox Code Playgroud) grails ×10
grails-orm ×10
criteria ×1
groovy ×1
has-many ×1
hibernate ×1
mocking ×1
mongodb ×1
multi-tenant ×1
projection ×1
proxy ×1
security ×1
spock ×1
stub ×1