gorm-hibernate5
gorm-hibernate5 copied to clipboard
Removing an element from a list doesn't actually remove it
Hello. I hope this is the right place for this issue, and that it has enough information:
So I have these two classes:
class Lol {
Long id
String name
List<Omg> omgs = new ArrayList<>()
static hasMany = [
omgs: Omg
]
static mappedBy = [
omgs: 'lol'
]
static mapping = {
id generator: 'sequence', params:[sequence:'lol_seq']
omgs indexColumn: [
name: 'lol_index'
]
}
}
package ai.boost
class Omg {
Long id
String name
Lol lol
static constraints = {
lol nullable: true
}
static mapping = {
id generator: 'sequence', params: [sequence: 'omg_seq']
}
}
And the SQL:
CREATE SEQUENCE lol_seq;
CREATE TABLE lol (
id BIGINT PRIMARY KEY DEFAULT NEXTVAL('lol_seq'),
name text
);
CREATE SEQUENCE omg_seq;
CREATE TABLE omg (
id BIGINT PRIMARY KEY DEFAULT NEXTVAL('omg_seq'),
name text,
lol_id BIGINT REFERENCES lol(id),
lol_index INT
);
Basically, a Lol has an ordered list of omgs.
The following code works fine:
Lol lol = new Lol(name: 'Lol 1');
Omg omg1 = new Omg(name: 'Omg 1')
Omg omg2 = new Omg(name: 'Omg 2')
Omg omg3 = new Omg(name: 'Omg 3')
lol.addToOmgs(omg1)
lol.addToOmgs(omg2)
lol.addToOmgs(omg3)
lol.save(flush: true)
I mean, look at this, it's just what I want: Beatiful!
But, what happens if I want to remove one of the omgs from the list without deleting it?
Lol lol = new Lol(name: 'Lol 1');
Omg omg1 = new Omg(name: 'Omg 1')
Omg omg2 = new Omg(name: 'Omg 2')
Omg omg3 = new Omg(name: 'Omg 3')
lol.addToOmgs(omg1)
lol.addToOmgs(omg2)
lol.addToOmgs(omg3)
lol.save(flush: true)
lol.removeFromOmgs(omg2) // Just added this line
This:
What is this? lol_id
is still 1
, but, in my opinion, should be NULL
, and the same with lol_index
.
Things I tried that had no effect:
-
omg2.setLol(null)
- Adding
nullable: true
to theindexColumn
- Upgrading from gorm 6.1.9 to 6.1.12