Grails4.0无法将域保存到DB
我对grails不是很熟悉,但这个案例对我来说很奇怪。 我已经创建了域对象名产品,并希望在Bootstrap.groovy中保存一些基本数据,但Product.save未保存在我的数据库中。 当我尝试使用product.getAll获取所有产品时,我得到的是空数组。 但是,当我为这个域对象构建所有内容时,grails使用ProductService来执行保存操作,它们在DB中可见 我是否做错了什么,或者使用Product.Save保存不受支持Grails4.0无法将域保存到DB,grails,groovy,gorm,Grails,Groovy,Gorm,我对grails不是很熟悉,但这个案例对我来说很奇怪。 我已经创建了域对象名产品,并希望在Bootstrap.groovy中保存一些基本数据,但Product.save未保存在我的数据库中。 当我尝试使用product.getAll获取所有产品时,我得到的是空数组。 但是,当我为这个域对象构建所有内容时,grails使用ProductService来执行保存操作,它们在DB中可见 我是否做错了什么,或者使用Product.Save保存不受支持 class Product { UUID i
class Product {
UUID id
String name
static constraints = {
}
static mapping = {
id generator : 'uuid2', type: 'pg-uuid' // pg-uuid because I use postgresql
}
}
Hibernate 5.2+需要用于写和读操作的事务 使用您创建的ProductService(例如ProductService.saveproduct1)将您的保存包装到事务中
确保它位于事务中的其他方法是在方法或产品上使用@Transactional。withTransaction{}Hibernate 5.2+需要进行写和读操作的事务 使用您创建的ProductService(例如ProductService.saveproduct1)将您的保存包装到事务中
您可以通过在方法或产品上使用@Transactional来确保它位于事务中的其他方法。withTransaction{}尝试使用。SaveFailOneError:true并查看错误我已经尝试过,但没有任何错误。尝试使用。SaveFailOneError:true并查看错误我已经尝试过,但没有任何错误。
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