zetriv
zetriv

Reputation: 13

Grails 4.0 cannot save Domain to DB

i am not very familiar with grails but this case is strange to me. I have create Domain object name Product and wanted to save some basic data in Bootstrap.groovy but product.save() is not saved in my DB. When i try to get all product with Product.getAll() i get empty array. But when i scaffolded everything for this domain object grails use ProductService in order to perform save operation and they are visible in DB Am i doing something wrong or Save with Product.save() is not supported?

class Product {
    UUID id
    String name

    static constraints = {
    }

    static mapping = {
        id generator : 'uuid2', type: 'pg-uuid' // pg-uuid because I use postgresql
    }
}
class BootStrap {

    def init = { servletContext ->

        def product = new Product(name: 'Launch day 1')
        product.save()

        def product1 = new Product(name: 'Launch day 2')
        product1.save()
        def product2 = new Product(name: 'Launch day 3')
        product2.save()
    }
    def destroy = {
    }
}


@Service(Product)
interface ProductService {

    Product get(Serializable id)

    List<Product> list(Map args)

    Long count()

    void delete(Serializable id)

    Product save(Product product)

}
dataSource:
  pooled: true
  driverClassName: org.postgresql.Driver
  dialect: org.hibernate.dialect.PostgreSQLDialect
  username: "postgres"
  password: "postgres"
  dbCreate: create-drop
  url: jdbc:postgresql://localhost:6543/grails_t
  properties:
    jmxEnabled: true
    initialSize: 5
    maxActive: 50
    minIdle: 5
    maxIdle: 25
    maxWait: 10000
    maxAge: 10 * 60000
    timeBetweenEvictionRunsMillis: 5000
    minEvictableIdleTimeMillis: 60000
    validationQuery: "SELECT 1"
    validationQueryTimeout: 3
    validationInterval: 15000
    testOnBorrow: true
    testWhileIdle: true
    testOnReturn: false
    jdbcInterceptors: "ConnectionState;StatementCache(max=200)"
    defaultTransactionIsolation: java.sql.Connection.TRANSACTION_READ_COMMITTED

Upvotes: 1

Views: 803

Answers (1)

erichelgeson
erichelgeson

Reputation: 2340

Hibernate 5.2+ requires transactions for write & read operations.

Wrap your saves in a transaction by using the ProductService you created - eg productService.save(product1)

Other ways you can ensure it's in a transaction are by using either @Transactional on the method or Product.withTransaction { }

Upvotes: 4

Related Questions