gorm

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

﹥>﹥吖頭↗ 提交于 2019-12-01 03:38:50
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 already been retrieved) and do not want to load the whole instance from the database. Is there a way to get

How to log sql in grails 1.3.7

倾然丶 夕夏残阳落幕 提交于 2019-12-01 03:23:19
问题 I try to configure logs for sql in grails with logSql=true in datasource (test env) but nothing is displayed in test output. I read this post but It's not working. How to log SQL statements in Grails Thanks 回答1: We did it in Config.groovy, log4j = { // ... whatever debug 'org.hibernate.SQL', 'org.hibernate.transaction' // optionally } Log4j is configured differently since Grails 1.1. 来源: https://stackoverflow.com/questions/5193539/how-to-log-sql-in-grails-1-3-7

Grails / GORM criteria query with hasmany String

随声附和 提交于 2019-12-01 03:22:48
I have a domain object (Cat) like this: class Cat { String name static hasMany = [ nicknames: String ] } (A cat has a name, and also has many nicknames (which are Strings)) And I am trying to query all the cats with certain nicknames. I've tried this: PagedResultList getCatsByNickname(String nickname, Map params) { PagedResultList results = Cat.createCriteria().list(params) { 'ilike'('nicknames','%'+nickname+'%') } return results } But it never returns any results. (If I change the query to just use the simple name attribute, it works finding all cats with that name, but I want to query

How do you disconnect an object from it's hibernate session in grails?

偶尔善良 提交于 2019-12-01 01:46:02
问题 I'm trying to do this, but I get the error. "a different object with the same identifier value was already associated with the session" It looks like I need to remove dbObject from the hibernate session. def object = messageParserService.parseMessage(messageType, messageText) def dbObject = object.getClass().findByIdentifier(object.identifier) if(dbObject != null){ object.id = dbObject.id object.dateCreated = dbObject.dateCreated } if(!object.save()) { object.errors.each {println it} } 回答1:

Grails hasOne vs. belongsTo

允我心安 提交于 2019-12-01 01:27:51
问题 To create one-to-one relationships in Grails I can do: class Person { static hasOne = [address: Address] } In this case the Address table has the key to its person. I could also do: class Address { static belongsTo = [person: Person] } This gives the same result. What is the difference between my two samples using hasOne and belongsTo ? 回答1: hasOne indicates that there is a bi-directional one-to-one relationship where the child table has the parent's foreign key, as in your example. belongsTo

GORM: mapping large text fields database agnostically

↘锁芯ラ 提交于 2019-12-01 00:35:22
I have a Grails application that will run against either a SQL Server or Oracle backend. I am using GORM as an ORM. I want to map a large text field in a way that supports both database types. In my Grails domain class I have something like: class Note { String content static constraints = { content nullable: false, blank: false } } I then declare database tables that look like this: -- oracle CREATE TABLE NOTE ( id NUMBER(19, 0) NOT NULL, version NUMBER(19, 0) NOT NULL, content CLOB NOT NULL ); -- SQL Server CREATE TABLE NOTE ( id NUMERIC(19, 0) NOT NULL, version NUMERIC(19, 0) NOT NULL,

Grails: Create dynamic SQL-Connection

 ̄綄美尐妖づ 提交于 2019-11-30 23:41:08
For my application I need dynamic database connections at runtime. I know, there are ways to create multiple datasources but they are not that dynamically I think. Scenario: A user can enter database credentials and connect to a remote database to import single rows and tables to an other database. For this purpose I need to connect to the remote database dynamically. I've tried to do that in a service like they've said in If I use groovy sql class in grails, does it use the grails connection pooling? Note: GORM is dispensable in this case, I can use plain SQL instead. Any ideas? Thank you..

Tree structure in GORM (grails)

瘦欲@ 提交于 2019-11-30 23:34:13
I'm trying to define a tree structure in GORM. Here is my model: class Tree { String name Level rootLevel static hasOne = [rootLevel: Level] static hasMany = [levels: Level] static mappedBy = [levels:"parentTree"] } class Level { String name Tree parentTree Level parentLevel Set<Level> subLevels static belongsTo = [parentTree: Tree] static hasMany = [subLevels: Level] } Insertion seems to work fine, but when I can't load a Tree with many levels and sublevels. I guess I missed something in the relations: - the Tree should have a reference to the rootLevel (and optionally to all sublevels) - a

grails limited table creation

廉价感情. 提交于 2019-11-30 23:27:40
I'd like to use the Grails feature for creating/updating database tables on a limited basis. Specifically, I'd like Grails to manage some tables, but not all. Is there a way to limit the tables managed by Grails or is it an all or nothing proposition? In general it's all or nothing since Grails uses Hibernate's HBM2DDL functionality. But you can intercept the process using a custom configuration subclass. Here's an example that extends GrailsAnnotationConfiguration and overrides all three SQL generation methods: package com.yourcompany.yourapp; import java.util.ArrayList; import java.util.List

How to design domain classes in Grails?

独自空忆成欢 提交于 2019-11-30 23:13:18
Given these functional requirements: User Management Administrator Librarian Borrower *The users have the option of logging-in via OpenID. Property Management Book Memorandum Circular License Normally, I would implement these in Java as: interface User {} class Librarian implements User {} class Administrator implements User {} class Borrower implements User {} class OpenID {} //all Users HAS AN OpenID attribute (NULL if non-openId login) interface Property{} class Book implements Property{} class Memorandum implements Property{} class Circular implements Property{} class License implements