所以最近我问了很多关于GORM的问题,因为这是我第一次使用它,并且每次我在对象之间的关系和保存它们方面都遇到一些问题。
所以这是一类:
class TesterUser {
@Id
private String id
private String userId
static belongsTo = Dashboard
static constraints = {
userId nullable: true
}
static mapping = {
id column: 'id', generator: 'assigned'
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
这是另一类:
类TestingClass {
@Id
private String id
private Date created
private Date modified
private String title
private ClassName className
static hasMany = [testUsers : TesterUser, sheets : Sheet]
static belongsTo = ClassName
static constraints = {
modified nullable: true
title nullable: true
className nullable: true
}
static mapping = {
sheets column:'testingClassId',joinTable: false
testUsers column:'testingClassId',joinTable: false
id column: 'id', generator: 'assigned'
title column: "title", length: 90000
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public DavUser getClassName() {
return className;
}
public void setClassName(ClassName className) {
this.className = className;
}
public Date getDeleted() {
return deleted;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我已经将对象保存在数据库中,但是现在我想设置它们之间的关系,当我调用并保存它们时,它就无法正常工作:
TesterUser testU = TesterUser.findById(uId)
TestingClass testC = TestingClass.findById(cId)
if(testU != null && testC != null){
amountOfRelations++
testC.addToDashboardUsers(testU)
if(!dtestC.save(flush:true, failOnError: true)){
amountOfUnsaved++
}
else{
amountOfsaved++
}
由于某种原因,我没有任何错误。不仅如此,我可以看到查询将进入我的数据库,但是什么也没有发生。没有更新,也没有错误。
我不知道为什么它不起作用。
任何的想法?
最佳答案
最终,我无法保存我的实体。
我的“解决方案”是首先使用addTo
方法保存实体。
我仍然不知道为什么它不起作用,或者我需要做些什么才能保存我的实体,但是仍然有解决方法。
关于grails - GORM没有保存我的hasMany实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36675498/