gorm

Getting the ID of a one-to-many loaded object without another trip to the DB with GORM

孤人 提交于 2019-12-19 06:17:11
问题 I have to GORM domains, A & B, that relate to database tables. A has a one-to-many relationship with B. Because of this, the classes look similar to: class A { B b Long id } class B { Long id } When I retrieve an instance of A the ID of the corresponding instance of B is retrieved from the database. However, when I attempt to access that ID via something like: A a = A.get(11) Long bid = a.b.id the whole object is loaded from the database. In some cases I only want the ID of B (which has

Null in hasMany assosiation list after removing item

[亡魂溺海] 提交于 2019-12-19 03:37:24
问题 There is domain object: class Book { List<Picture> pictures static hasMany = [pictures:Picture] static mapping = { pictures lazy: false, cache: 'nonstrict-read-write' } } Sometimes, after deleting pictures from list by code it produce null item in pictures list. .. book.refresh() def pic = Picture.get(params.id) subject.removeFromPictures(pic) subject.save() It looks like, GORM not update idx field in assosiation table. I can't reproduce it, but I got few times it on production server In my

golang 将数据库转换为gorm结构

大憨熊 提交于 2019-12-18 11:07:19
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> gormt 一款mysql数据库转 struct 工具,可以将mysql数据库自动生成golang sturct结构,带大驼峰命名规则。带json标签 1. 通过当前目录 config.yml 文件配置默认配置项 out_dir : "." # 输出目录 singular_table : false # 表名复数,是否大驼峰构建 参考:gorm.SingularTable simple : false #简单输出 is_json_tag : false #是否打json标记 is_foreign_key : true #是否导出外键关联 mysql_info : host : "127.0.0.1" port : 3306 username : "root" password : "qwer" database : "oauth_db" 2. 可以使用命令行工具更新配置项 ./gormt -H=127.0.0.1 -d=oauth_db -p=qwer -u=root --port=3306 3. 查看帮助 ./gormt --help or ./gormt -h ------------------------------------------------------- base on gorm tools

Grails 1.0.3 Upgrade Problems

戏子无情 提交于 2019-12-18 05:24:13
问题 I am trying to upgrade a Grails 1.0.3 project to 1.3.7 and am having what I believe are related issues. The old project has Hibernate xml files in conf/hibernate/Domain1.hbm.xml I am guessing that GORM didn't exist in 1.0.3? Do I need to essentially convert what is in the xml files into Groovy code in the Domain classes in domain/ Any other details are helpful. Thanks. UPDATE - All of these changes are the result of a org.hibernate.DuplicateMappingException It looks like I can just move the

Grails/GORM default fetch strategy: When to set fetchMode to “eager”? (eager vs. lazy)

蓝咒 提交于 2019-12-18 04:16:08
问题 What are some general guidelines on when to set fetchMode to "eager" in a domain class? Pros and cons of fetchMode "eager" vs. the default "lazy"? Please include some specific examples/use-cases showing when to use "eager" (fetchMode=eager), and when not to (fetchMode=lazy). 回答1: Basically lazy loading has more benefits than the eager alternative (performance, use of resources) . Since it's the default grails setting for all relations (since Grails 1.1) you should generally not configure it

Do I ever need to explicitly flush GORM save calls in grails?

筅森魡賤 提交于 2019-12-17 15:23:15
问题 I have a strange situation which appears to indicate a GORM cacheing problem //begin with all book.status's as UNREAD Book.list().each { book.status = Status.READ ; book.save() } println (Book.findAllByStatus (Status.READ)) //will print an empty list println (Book.list().findAll (it.status == Status.READ)) // will print all books I cannot understand why the last two queries could return different results. However if I make the following modification of book.save(flush:true) . Both of the

MappedSuperclass Alternatives in Grails 2.0

我只是一个虾纸丫 提交于 2019-12-14 00:59:29
问题 The problem is the same as in the older SO question but the solution is no longer valid for Grails 2.0 - abstract domain class is not handled as @MappedSuperclass but is always persisted in it's own table. If I move it outside grails-app/domain it doesn't work at all. So is there a way to have an abstract superclass (or even better a mixin) that would behave like @MappedSuperclass (without creating own table with shared id and common fields) ? 回答1: we had the same problem and solved it with

Grails hasOne and hasMany with same domain and cascade operation

馋奶兔 提交于 2019-12-13 20:25:44
问题 Is there any way of making following structure: class Parent { String name static hasOne = [firstChild: Child] static hasMany = [otherChildren: Child] } class Child{ String name static belongsTo = [parent: Parent] } Now when i try to run a simple code: Parent p = new Parent(name: "parent", firstChild: new Child(name: 'child')) p.addToOtherChildren(new Child(name: "child2")); p.addToOtherChildren(new Child(name: "child3")); p.save(flush: true) It saves the object but when I try to do a list

different types of null in groovy

久未见 提交于 2019-12-13 20:12:48
问题 I have a method that looks like this: static UserEvent get(long userId, long eventId) { UserEvent.find 'from UserEvent where user.id=:userId and event.id=:eventId', [userId: userId, eventId: eventId] } I'm calling it two times with some test data: println UserEvent.get(1, 1) //I know this has value println UserEvent.get(1,2) //I know this does not The above two statements result in: scheduler.UserEvent : null null Question What is the difference? How can I write an If condition for when

Grails data binding - Cannot make an immutable entity modifiable

坚强是说给别人听的谎言 提交于 2019-12-13 19:13:48
问题 On a Grails 2.1.0 I am trying to dynamically updating a field on a domain class. The object gets binded and it looks fine, until the save method is called, which throws the following exception: java.lang.IllegalStateException: Cannot make an immutable entity modifiable. try { def bindParams = [:] bindParams."$paramsFieldName" = "$paramsValue" def domainClass = grailsApplication.domainClasses.find { it.clazz.simpleName == paramsDomain }.clazz def objectInstance = domainClass.findById(paramsId)