Grails(Hibernate)在插入之前进行选择而不传递 id 参数值

发布于 2024-12-02 04:15:27 字数 1402 浏览 0 评论 0原文

我正在开始对我的 grails Web 应用程序进行更严格的测试,是时候从 hsqldb 迁移到 postgres 了。一切都或多或少顺利,但是我在创建测试数据时在 Bootstrap 中遇到错误。 Hibernate 尝试在将行插入数据库之前进行选择。 不幸的是,这样做时,它不会将参数传递给选择,并且我收到异常,说未指定参数 1 值。

这是我的域类:

class User {

static constraints = {
    deviceId(
            blank: false,
            unique: true,
    )
    userName(nullable: true, size: 5..15, blank: false, unique: true)
    password(nullable: true, size: 5..15, blank: false)
    email(nullable: true, email: true, blank: false, unique: true)
    blockedUsers(unique: true)
}
static mapping = {
    table "player"
    deviceId indexColumn: [name: "deviceId", unique: true]
}
static mappedBy = [inbox: 'userTo', outbox: 'userFrom']
static hasMany = [profiles: Profile, inbox: Message, outbox: Message, blockedUsers: User]

List<User> blockedUsers = new ArrayList<User>()
String deviceId
String userName
String email
String password
Boolean readyForExport = false
Boolean newUser = false
[...]
}

所以它没有什么特别的。 在此错误之前,我在其他域上有很多 .save(flush:true) 命令,一切都很好。正是这一行引发了错误:/

这些是导致此错误的行。

User userA = new User(userName: "test", password: "dkjughey3htg", email: "[email protected]", deviceId: 'intTestDevice')
userA.save(flush:true)

有什么提示为什么会发生这种情况吗?

I'm starting more serious tests of my grails web-app and it was time to move from hsqldb to postgres. Everything went more or less ok, however I'm getting an error in Bootstrap when creating test data. Hibernate tries to do a select before inserting a row into the database.
Unfortunately, when doing so it does not pass an argument to the select and I'm getting exception saying the parameter 1 value was not specified.

Here's my domain class:

class User {

static constraints = {
    deviceId(
            blank: false,
            unique: true,
    )
    userName(nullable: true, size: 5..15, blank: false, unique: true)
    password(nullable: true, size: 5..15, blank: false)
    email(nullable: true, email: true, blank: false, unique: true)
    blockedUsers(unique: true)
}
static mapping = {
    table "player"
    deviceId indexColumn: [name: "deviceId", unique: true]
}
static mappedBy = [inbox: 'userTo', outbox: 'userFrom']
static hasMany = [profiles: Profile, inbox: Message, outbox: Message, blockedUsers: User]

List<User> blockedUsers = new ArrayList<User>()
String deviceId
String userName
String email
String password
Boolean readyForExport = false
Boolean newUser = false
[...]
}

so there's nothing special about it.
Before this error I've got quite a few .save(flush:true) commands on other domains and everything is fine. It's just this one that throws the error :/

These are the lines responsible for this error.

User userA = new User(userName: "test", password: "dkjughey3htg", email: "[email protected]", deviceId: 'intTestDevice')
userA.save(flush:true)

Any tips why this would happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-12-09 04:15:27

我还没有测试过这个,但我认为你的问题在于:

blockedUsers(unique:true)

你将blockedUsers指定为List而不是GORM默认的Set是否有原因?使用 Set,成员的唯一性是自动的,然后您不必使用唯一约束,这在一对多属性上不会像您期望的那样工作。另外,这是不相关的,但明文密码是顽皮的。

I haven't tested this, but I think your problem is in:

blockedUsers(unique:true)

Is there a reason why you're specifying blockedUsers as a List rather than GORM's default of Set? With Set, uniqueness of members is automatic, and then you don't have to use the unique constraint, which doesn't work as you might expect on one-to-many properties. Also, this is unrelated, but plaintext passwords are naughty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文