Grails 4.0 无法将域保存到数据库

Grails 4.0 cannot save Domain to DB

我对 grails 不是很熟悉,但是这个案例对我来说很陌生。 我已经创建了 Domain object name Product 并想在 Bootstrap.groovy 中保存一些基本数据,但是 product.save() 没有保存在我的数据库中。 当我尝试使用 Product.getAll() 获取所有产品时,我得到空数组。 但是当我为这个域对象构建所有东西时,grails 使用 ProductService 来执行保存操作并且它们在数据库中可见 我做错了什么或不支持使用 Product.save() 保存吗?

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

Hibernate 5.2+ 需要用于写入和读取操作的事务。

使用您创建的 ProductService 将您的储蓄包装在交易中 - 例如 productService.save(product1)

确保它在事务中的其他方法是在方法上使用@Transactional 或Product.withTransaction { }