在没有addTo的情况下保存hasMany关系

时间:2013-08-26 12:45:07

标签: hibernate grails gorm has-many

我们有两个域类

class FeeGroup {

String name;

static hasMany = [priceRanges:PriceRange]

class PriceRange {

Integer from;
Integer to;
Boolean include;

static belongsTo = [feeGroup:FeeGroup]

从我的前端应用程序中,我收到表格为JSON

{
    "name":"fee group 1",
    "priceRanges":[
                    {"from":10, "to":20, "include":true},
                    {"from":20, "to":40, "include":true},
                    {"from":30, "to":60, "include":true}
                  ]
}

现在我想保存简单的所有数据

def model = new FeeGroup(data)
model.save()

使用此代码,还可以保存priceRanges。这有什么问题?因为这只省费了! Wierd就是你

的时候
println model.priceRanges

在model.save()之后,它还会打印PriceRanges的新ID - 但是数据库中没有任何内容,也没有Hibernate中的内容(这里没有帮助)。

Documentations中的每个地方都是我应该使用model.AddToPriceRanges,但为什么呢?为什么一对一的工作完美,而且不是一次性的?

我已经尝试将FeeGroup域类更改为

class FeeGroup {

String name;
Set priceRanges
static hasMany = [priceRanges:PriceRange]

现在很简单,保存工作完美,但GORM创建了一个连接表fee_group_price_range ...我不希望有3个表用于hasMany连接。这不是一个好的解决方案。

感谢您的建议

修改

3表不是因为FeeGroup Class的变化,而是因为将PriceRange Class更改为

class PriceRange {

Integer from;
Integer to;
Boolean include;

static belongsTo = FeeGroup

0 个答案:

没有答案
相关问题