Grails addTo repeated data entry for hasMany relationships

I have a pretty simple hasMany relationship where the "product" has many "packages". I am running a script to try to populate data that works great the first time it runs. However, in the second run, it starts to introduce duplicates, which, although I was not available for hasMany

package = Package.findBySourceId(packageId) ?: new Package(name:packageName, price:packagePrice, sourceId:packageId).save(flush:true)

product = Product.findBySourceId(productId)                 
product.addToPackages(package)
product.save(flush:true)

When I turn on sql logging, I see that sometimes the selection that should be triggered when addToPackages is called does not start. He simply makes a choice to find the product, and then directly insert it into the connection table.

I don't want to add exact queries due to work, but basically it looks like

Select -> for the initial package which it finds

Select -> for product which it finds by the product id

Insert -> insert into the join table without even a select to check if an entry exists

, . , . , script, , - ?

+5
2

"" " "? Set, . , , .

, "" equals() hashCode().

+3
0

All Articles