[英]Grails 4.0 cannot save Domain to DB
我對 grails 不是很熟悉,但這種情況對我來說很奇怪。 我創建了域 object 名稱 Product 並希望在 Bootstrap.groovy 中保存一些基本數據,但 product.save() 未保存在我的數據庫中。 當我嘗試使用 Product.getAll() 獲取所有產品時,我得到空數組。 但是當我為這個域 object grails 搭建所有東西時,使用 ProductService 來執行保存操作,並且它們在 DB 中可見我做錯了什么還是不支持使用 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 { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.