我在Grails应用程序中进行了集成测试,当我尝试保存类型的实体时失败 Member
invitingMember.save(flush: true)
Run Code Online (Sandbox Code Playgroud)
这引发了以下异常
org.hibernate.AssertionFailure:在com.mycompany.member.MemberConnectionService.addOrUpdateContact(MemberConnectionService.groovy:939)中,flush()未处理集合[com.mycompany.facet.Facet.channels].
在事务的早期,我将一个对象添加到集合属性中invitingMember.我的猜测是异常在上面的行中抛出,因为只有在这一点上才会保留添加到集合中的对象.
我对使用静态hasOne映射和在域类中组合对象之间的区别感到困惑.两者有什么不同?即.
class DegreeProgram {
String degreeName
Date programOfStudyApproval
static hasOne = [committee:GraduateCommittee]
}
Run Code Online (Sandbox Code Playgroud)
与
class DegreeProgram {
String degreeName
Date programOfStudyApproval
GraduateCommittee committee
}
Run Code Online (Sandbox Code Playgroud)
GraduateCommittee是另一个GORM领域模型类.
我正在尝试hasMany使用mapping语句设置我的属性的默认排序.我正在关注grails doc,但它对我不起作用(grails 1.3.5).我的代码看起来像:
class Note {
Calendar sendDate
static belongsTo = Message
}
class Message {
static hasMany = [notes: Note]
static mapping = {
notes sort:'sendDate desc'
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息如下所示:
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'notes0_.sendDate' in 'order clause'
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
...
Run Code Online (Sandbox Code Playgroud)
你看到我的代码中有任何错误吗?
我想在一个关联链中加载一个结构,两个层次.有点像:
class TopLevel {
String name
LevelOne levelOne
}
class LevelOne {
String name
LevelTwo levelTwo
}
class LevelTwo {
String name
}
Run Code Online (Sandbox Code Playgroud)
我想加载整个结构.搜索我发现这个例子,但它没有用."println"生成了对LevelTwo表的查询.
def result = TopLevel.withCriteria {
eq('name', 'test')
fetchMode "levelOne", FetchMode.JOIN
levelOne {
fetchMode "levelTwo", FetchMode.JOIN
}
}
println result.levelOne.levelTwo.name
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助!
- 史蒂夫
编辑:onload()方法更改为afterLoad():否则对象可能无法正确传递给地图.
我目前正在使用一些具有大量动态,复杂属性的域类,我需要持续定期更新.
我将这些保存在每个类的Map结构中,因为这样可以很容易地在我的控制器等中进行引用.
但是,由于Grails似乎无法在DB中持久保存像List和Map这样的复杂属性类型,因此我使用以下方法通过JSON String对象实现此目的:
class ClassWithComplexProperties {
Map complexMapStructure //not persisted
String complexMapStructureAsJSON //updated and synched with map via onload,beforeInsert,beforeUpdate
static transients = ['complexMapStructure']
def afterLoad() { //was previously (wrong!): def onLoad() {
complexMapStructure=JSON.parse(complexMapStructureAsJSON)
}
def beforeInsert() {
complexMapStructureAsJSON= complexMapStructure as JSON
}
def beforeUpdate() {
complexMapStructureAsJSON= complexMapStructure as JSON
}
static constraints = {
complexMapStructureAsJSON( maxSize:20000)
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用,因为我只从数据库加载数据,但是当我想将我的更改保存到数据库时,我遇到了麻烦.例如,当我执行以下操作时
/* 1. Load the json String, e.g. complexMapStructureAsJSON="""{
data1:[[1,2],[3,4]],//A complex structure of nested integer lists
data1:[[5,6]] //Another one
}""" …Run Code Online (Sandbox Code Playgroud) 我的Grails服务存在一个问题,即与事务无关的吞没异常导致事务回滚,即使它与域对象的持久性无关.
在我的服务中,我有一些东西
updateSomething(domainObj) {
def oldFilename = domainObj.filename
def newFilename = getNewFilename()
domainObj.filename = newFilename
domainObj.save(flush: true)
try {
cleanUpOldFile(oldFilename)
} catch (cleanupException) {
// oh well, log and swallow
}
}
Run Code Online (Sandbox Code Playgroud)
我所看到的是,当我清理旧文件时遇到异常,我记录它并吞下它,但它仍然导致事务回滚,即使我已经完成了更新域对象.
如何在清理之前限制范围事务完成,或者是否有其他方法可以使清理异常不导致回滚?
只是为了记录我使用Grails 2.1.1
我知道已经有一个问题,但我认为导致的问题可能因他们所说的而有所不同.我有以下设置:
这个grails应用程序有多个数据源(我不确定这个事实是否与它有任何关系)并且它发生在从第二个数据源调用Domain对象之后,该数据源已经使用grails反向工程插件进行了反向设计(已完成)在一个单独的项目上,然后导入到我的项目.
这是导致Controller层和服务层出现问题的GORM调用
def campusAttributes = CampusAttribute.findAllByNameLike("%Next Option%")
Run Code Online (Sandbox Code Playgroud)
我发现了一些其他的抱怨,这似乎是Maven和grails hibernate插件版本的一个问题,但在那种情况下,问题是grails 2.3.1和特定的hibernate插件版本非常特别.
我所做的只是一个grails run-app(没有用maven构建应用程序也没有任何嘲弄)
提前致谢
在我的Grails 2.5.0应用程序的域模型中,我有两个类Income,Benefit它们具有相同的属性.我想将它们存储在单独的数据库表中,但将公共字段移动到基类中.我提出的模型是:
class Assessment {
Date dateCreated = new Date()
User user
static hasMany = [incomes: Income, benefits: Benefit]
}
class Benefit extends IncomeSource {}
class Income extends IncomeSource {}
abstract class IncomeSource {
String name
BigDecimal amount
PaymentFrequency frequency
static belongsTo = [assessment: Assessment]
static mapping = {
tablePerHierarchy false
}
}
Run Code Online (Sandbox Code Playgroud)
这将导致之间的关系要产生的下表Assessment和Benefit

表对之间的关系创建Assessment和Benefit是(勿庸置疑)相同.
而不是assessment_benefit在assessment和之间有一个连接表benefit,我宁愿assessment_id在benefit表中有一个外键,从而不需要连接表.
如何更改我的域模型以实现此目的?
我有这样的代码:
Book.list().each {
// real code actually does something more useful
println "My id is " + it.id
}
Run Code Online (Sandbox Code Playgroud)
这让我觉得有点浪费,每本书的整个对象只是为了访问id而被加载.Grails中有一个load()方法,当你只想对ID进行操作时,我想知道这里是否有一个等价的加载所有域实例?我应该使用HQL吗?或者我应该保持原样?
PS:这让我想知道是否应该为大多数GORM方法(查找器等)提供一个选项,使其仅"加载"而不是"获取"目标类
我正在寻找扩展Grails CRUD生成功能的最佳方法.它应该是一个Grails插件,它为以下功能提供了额外的生成器:
Grails开箱即用的脚本应该尽可能小.到目前为止,我确定了3个实施设计策略:
任何解决方案都有利有弊.实施它的最佳方法是什么?也许有一种更简单的方法.
grails-orm ×10
grails ×9
hibernate ×2
grails-2.0 ×1
java ×1
json ×1
persistence ×1
transactions ×1