gorm

Dynamically creating a query based on params being passed to a controller

我怕爱的太早我们不能终老 提交于 2019-12-04 08:51:48
In my task management application, users should be able to filter tasks based on : assignedTo , priority , status and/or dueDate I am not sure on how to create a dynamic query in that it will build a query based on the available parameters. For example : If I have a URL such as : task/index?assignedTo=1&status=2 I can build a query based on only these two parameters. The method I am used to is the Task.findAllByAssignedToAndStatus( User.get(params.assignedTo), TaskStatus.get(params.status) ) I obviously dont want to implement a DRY method by writing out each findAllBy query for every possible

Stripping trailing whitespace from char fields in a legacy database with Grails GORM

℡╲_俬逩灬. 提交于 2019-12-04 07:55:49
What are the possible solutions for stripping the trailing whitespace when mapping char fields in a legacy database? I see the following options: Calling .trim() at the point of use (controller, view, etc) Override property accessors to return .trim() Using a Hibernate UserType to trim the whitespace I'm leaning toward overriding the property accessor so that the domain properties remain consistent throughout the application. I had a similar problem and I could not alter the legacy data. I ended up overriding the accessor for the sake of transparency to my fellow developers. I would recommend

Grails group by date

半城伤御伤魂 提交于 2019-12-04 07:38:11
I have a domain class with a date-property. class Transaction { LocalDate time BigDecimal amount } How can I query for the sum of all transactions grouped by month? I can´t find any support for group by a date-range in GORM. Add a formula based field to your domain class for the truncated date: class Transaction { LocalTime time BigDecimal amount String timeMonth static mapping = { timeMonth formula: "FORMATDATETIME(time, 'yyyy-MM')" // h2 sql //timeMonth formula: "DATE_FORMAT(time, '%Y-%m')" // mysql sql } } Then you'll be able to run queries like this: Transaction.withCriteria { projections

Does removing a property from a domain class cause an automatic update to the schema, whereby the corresponding column is dropped?

风流意气都作罢 提交于 2019-12-04 03:46:18
问题 I'm sort of new at Grails. I've worked with it a bit, but not that much. I'm pretty familiar with Java though. My question is regarding schema updates. I understand that Grails creates Hibernate mappings by looking at the domain classes, and so if I add a new property, Grails will automatically add a column for that property in the database. Does the reverse also hold true? If I remove a property, is that column removed? I'm not seeing that behavior and so I'm wondering if it is a

Grails Gorm : Object references an unsaved transient instance

北战南征 提交于 2019-12-04 03:31:48
问题 I get the following Exception when saving an instance of Trip in Grails: 2011-01-26 22:37:42,801 [http-8090-5] ERROR errors.GrailsExceptionResolver - object references an unsaved transient instance - save the transient instance before flushing: Rower org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Rower The concept is simple: For a boattrip you need some rowers, a coxwain (is also a rower) and a boat: Trip

Grails GORM: could not initialize proxy - no Session

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 02:53:00
I have a method with the following structure: public void run(){ ... for (...) { //this part works correct User.withTransaction { User user = User.findByUsername(...); Position pos = Position.findByName(...) if(pos){ ... } else { ... try{ pos.save(flush:true); user.position = pos; } catch (Exception e){ ... } } ... try{ user.save(flush:true, failOnError: true); } catch (Exception e){ ... } } } //this part doesn't work User.findAll().each { ... if (...){ User.withTransaction{ ... //here the operation fails with //org.hibernate.LazyInitializationException: //could not initialize proxy - no

Why grails throwing null pointer exception while accessing hasMany relationship first time?

不羁岁月 提交于 2019-12-04 00:00:28
I have a strange problem. I have two domain classes User and Post with fields: class User { String name static hasMany = [posts: Post] static constraints = { } } and class Post { String content long date = System.getTimeInMillis() static constraints = { } static belongsTo = [user: User] static mapping = { version: 'false' } } and controller code is: class UserController { def addUser = { def user if (User.count() == 0) { user = new User() user.name = "Manish Zedwal" user.save(flush: true) } else { user = User.get(1) } println "Posts count: " + user.posts.size() render "post count: " + user

How do I sort by a property on a nullable association in Grails?

被刻印的时光 ゝ 提交于 2019-12-03 20:18:37
I'm trying to sort a table of data. I have the following domain (paraphrased and example-ified): class Car { Engine engine static constraints = { engine nullable: true // poor example, I know } } class Engine { String name } Here's the controller action that's handling the sort: def myAction = { def list = Car.findAll(params) render(view: 'list', model: [list: list]) } I provision some data such that there are several Cars, some with null engines and others with engines that are not null. I attempt the following query: http://www.example.com/myController/myAction?sort=engine.name&order=asc The

Grails Enum Mapping

两盒软妹~` 提交于 2019-12-03 18:46:13
问题 in Grails, Is there a way to limit the size of the column to which the enum is mapped. In the following example, i would like the column type to be char(2) enum FooStatus { BAR('br'), TAR('tr') final static String id } class Foo { FooStatus status static constraints = { status(inList:FooStatus.values()*.id,size:2..2) } } both inList and size do not have any effect when exporting the schema, the column type keeps its default value (varch(255)) Maybe i could do that if i define a new UserType.

Grails request parameters encoding issue in Tomcat

有些话、适合烂在心里 提交于 2019-12-03 18:05:20
问题 My grails app will not decode request parameters correctly. In config.groovy: grails.views.gsp.encoding = "UTF-8" grails.converters.encoding = "UTF-8" All my gsp's use contentType="text/html; charset=UTF-8" on the page directive as well as <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> in the head element. However, when i receive the posted parameters from the param object in my controller, the app just prints garbage... I'm using Grails 1.3.7 version deployed over