gorm

Grails 3 and Java 8 time support

ε祈祈猫儿з 提交于 2019-12-06 13:49:28
In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects. Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes? Edit : Grails 3.1.1 has hibernate 5 so this is not an issue anymore... Grails I finally found a solution. First of all hibernate 5 implements persistent for

belongsTo multiple Domain

醉酒当歌 提交于 2019-12-06 13:40:32
问题 I have 4 classes, incidents,problems, requests and another is Attachment. Every domain look like......... Class Incidents { // other fields static hasOne = [attachment: Attachment] static constraints = [attachment nullable:true] } Class Problems { // other fields static hasOne = [attachment: Attachment] static constraints = [attachment nullable:true] } Class Requests { // other fields static hasOne = [attachment: Attachment] static constraints = [attachment nullable:true] } Class Attachment {

Grails “max” subquery with an association, to get only the latest of a hasMany

假装没事ソ 提交于 2019-12-06 11:24:37
The simplified domain model: 'Txn' (as in Transaction) hasMany 'TxnStatus'. TxnStatus has a dateTime This is a legacy mapping so I cant change the DB, the mapping on Txn: static mapping = { txnStatus column: 'MessageID', ignoreNotFound: true, fetch: 'join' } I need to get Txns based on a number of dynamically built criteria, currently using GORM's 'where' query, it works well; BUT I need to also get only the latest txnStatus. Tried: def query = Txn.where { txnStatus { dateTime == max(dateTime) } } gives: java.lang.ClassCastException: org.hibernate.criterion.DetachedCriteria cannot be cast to

Changing the data type of the [hasMany:] reference to a list?

做~自己de王妃 提交于 2019-12-06 11:24:16
Is there a way to change the data type of the static hasMany = [myList: Stuff] definition in grails? I tried List<Stuff> myList hasMany = [myList : Stuff] but my existing tests started throwing Stuff._MyContainer_mylistBackref; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value which indicates the two aren't equivalent in terms of how they're being handled. What am I doing wrong here? Dónal As described in section 5.2.4 of the Grails manual , this is the correct way to make the collection a List . I suspect the problem is that by

How to optimize this GORM query

纵然是瞬间 提交于 2019-12-06 09:58:17
问题 I have a query which makes 2 database queries. I am new to GORM and don't know how to optimize it into 1 query using criteria, or a where clause or something else. I'm using springsecurity as well and have a User class for login security. I have a Profile class with a User object: class Profile { User user } In my controller the session stores the user id. When I want to load the profile to update it my code is currently: def user = User.get(springSecurityService.principal.id) // 1st db query

How to implements tablePerHierarchy with disciminator in grails?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 09:12:19
问题 I have a class hirarchy : class Item {} class Participation extends Item{} class Contribution extends Participation{} class Question extends Participation{} I would like to have a table per class so, I add tablePerHierarchy false in Item I need a discrimator to implements a query : where class = "Contribution" I try a lot of implementation but it's not working. How to do that ? Thanks 回答1: Do you want table per hierarchy or table per class? It's not clear in your question. With the following

Grails: Raw SQL query in the current transaction

坚强是说给别人听的谎言 提交于 2019-12-06 09:03:54
问题 I am trying to execute a raw sql query using something similar to def dataSource; Sql sql = new Sql(dataSource); But, it seems that this runs in a separate transaction of its own. It therefore misses all the (uncommitted) changes done before it in the service method. What is the best way to run a raw sql query in the current transaction? 回答1: The above code would create a new connection, hence a new transaction. You can use the current Hibernate session(injecting sessionFactory) to execute a

How to override the DomainClass.list() in GORM (Grails)

点点圈 提交于 2019-12-06 08:11:20
问题 People, I'm facing a problem with grails GORM , my Application is totally dependent of the DomainClass.list() method, it is in all of my create/edit GSPs, but now I need a particular behavior for listing objects. Being more specific I need to filter these lists (All of them) by one attribute. The problem is I'm hoping not to change all the appearances of these methods calling, so is there a way to customize the behavior of the default list() method ? I need it to function just the way it does

Can you combine controller list params with Hibernate criteria?

时间秒杀一切 提交于 2019-12-06 07:57:00
I have noticed that you can pass "params" straight in to the boilerplate code below: [fooInstanceList: Foo.list(params), fooInstanceTotal: Foo.count()] Is it possible to pass "params" in as part of a Hibernate criteria for example the one below? def c = Foo.createCriteria() def results = c { not { eq("bar","test") } } [fooInstanceList: results, fooInstanceTotal: results.size()] I am looking to use the "max" and "offset" params so I can use it for paging for example. I would also like to use the equivalent of count that counts all non-paged results. I think results.size() would only give me

How to handle GORM exceptions

拥有回忆 提交于 2019-12-06 07:56:38
问题 I'm trying to implement exception handling for Optimistic lock type exceptions that are thrown by Hibernate but I've encountered a strange issue. It seems I'm unable to catch any Gorm exceptions. For example I have this code in my service: try { User user = User.get(1); Thread.sleep(10000); user.viewedAt(new Date()); user.save(flush:true); } catch (OptimisticLockingException ex) { log.error("Optimistic lock exception"); } catch (StaleObjectStateException ex) { log.error("Optimistic lock