gorm

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

北城以北 提交于 2019-12-05 10:00:37
问题 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) }

Grails - findAll Posts Created Today by User

有些话、适合烂在心里 提交于 2019-12-05 09:22:37
I have a domain object. class Post { User user String subject String body Date dateCreated } How do I go about this? Is straight GORM, HQL, or criteria the better way? I didn't know what kind of date manipulation ("today eq") would work in criteria. I would probably make a named query class Post { User user String subject String body Date dateCreated static namedQueries = { todaysPosts { def now = new Date().clearTime() between('dateCreated', now, now+1) } } } Then you can use it like: Post.todaysPosts.count() or Post.todaysPosts.list() Post.todaysPosts.list(max: 10, offset: 5) you can even do

What's the best way to define custom id generation as default in Grails?

强颜欢笑 提交于 2019-12-05 07:54:41
I want to switch my domain classes to use a variable length UUID for their ids. I don't want to simply display sequential ids on the URL for people to try and mess with. I've written a custom version of the Java UUID method to allow for variable length so I can have shorter ids for models that won't grow large. I found this thread that explained how to modify the default mapping so I can change to 'assigned'. Modify Id generation for a Grails Plugin What's the best way to also configure a default beforeInsert (to generate the custom UUID) and tell Grails I want to use strings for ids instead

Grails Domain Class : hasOne, hasMany without belongsTo

放肆的年华 提交于 2019-12-05 06:50:34
I am new to Grails. Can I use "hasOne" or "hasMany" without using "belongsTo" to another domain-class? Thanks in advance. Yes, you can. See examples in Grails doc: http://grails.org/doc/2.3.8/guide/GORM.html#manyToOneAndOneToOne hasMany (without belongsTo) example from the doc: A one-to-many relationship is when one class, example Author, has many instances of another class, example Book. With Grails you define such a relationship with the hasMany setting: class Author { static hasMany = [books: Book] String name } class Book { String title } In this case we have a unidirectional one-to-many.

Issues posting nested resource in Grails

孤街浪徒 提交于 2019-12-05 06:48:52
I'm having issues understanding how Grails Restful controllers work. I'm attempting to make a post request (see below) to a nested resource. I'm not sure I understand what I need to change to make this work, as it seems that GET requests build the Bid's association with it's parent resource Item, but when I attempt to POST I am warned that the Item cannot be blank. Any help is appreciated! Item.groovy class Item { static hasMany = [bids:Bid] } Bid.groovy class Bid { Integer ownerId Double amount static belongsTo = [item:Item] static constraints = { ownerId nullable: false amount nullable:

How to fetch records in grails by max date and group at same time

雨燕双飞 提交于 2019-12-05 06:04:44
I have a table that looks like this: id name shade date_created ---- ----- ------- --------------- 1 Red bright 10-28-2012 2 Orange light 10-28-2012 3 Red <null> 10-24-2013 4 Orange light 10-24-2013 Desired Result: id name value date_created ---- ----- ------ --------- 3 Red <null> 10-24-2013 4 Orange light 10-24-2013 What can I do with GORM to get this result? In pure sql this is the query that gets me the desired result: SELECT t.name, t.shade, r.MaxTime FROM (SELECT name, MAX(date_created) as MaxTime FROM colorable GROUP BY name) r INNER JOIN colortable t ON t.name = r.name AND t.date

GORM in Grails and StaleObjectStateException

痴心易碎 提交于 2019-12-05 05:45:33
I'm writing a small Grails app, and I keep on getting StaleObjectStateException:s for about 1/10:th of the calls to "createfoo" when running the following rather simple code. Most probably I'm missing out on the best way to use GORM. This is the code: def viewfoo = { session.user.refresh() // ... } def createfoo = { session.user.refresh() var user = session.user if (param["name"]) { var newFoo = new Foo() newFoo.name = param["name"] if (newFoo.validate()) { newFoo.save() if (user.validate()) { user.addToFoos(newFoo) } else { user.discard() } } else { newFoo.discard() } } } My questions

Maintaining both sides of self-referential many-to-many relationship in Grails domain object

試著忘記壹切 提交于 2019-12-05 02:46:05
问题 I'm having some problems getting a many-to-many relationship working in grails. Is there anything obviously wrong with the following: class Person { static hasMany = [friends: Person] static mappedBy = [friends: 'friends'] String name List friends = [] String toString() { return this.name } } class BootStrap { def init = { servletContext -> Person bob = new Person(name: 'bob').save() Person jaq = new Person(name: 'jaq').save() jaq.friends << bob println "Bob's friends: ${bob.friends}" println

Grails: multiple relationships between two domain objects

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:32:45
问题 I am trying to implement two different types of relationships between two domain classes in Grails. Consider the following; I have two domain classes, an Author and Book class with an Author having many Books. class Author{ String name } class Book{ String title static belongsTo = [author:Author] } The above depicts a very basic one to many relationship between an Author and a Book. But I also want an Author to have a concept of a list of favourite books. This would ideally be represented as

How do I create a composite primary key using GORM?

落爺英雄遲暮 提交于 2019-12-04 23:47:34
问题 I have three domain classes: Beer, Review, and Reviewer. I want the Review table to create a many to many relationship between Beer and Reviewer, so I want the primary key of Review to be a composite of the id fields from Beer and Reviewer. I'm following this Grails documentation. http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.5.2.5%20Composite%20Primary%20Keys Here are my domain classes. class Beer { String name String type Brewery breweryId static