gorm

grails 3.3 gorm where query with projection count() different than list().size()

与世无争的帅哥 提交于 2019-12-10 18:26:42
问题 According to the Gorm 6 documentation section 7.4 the Where Query returns a DetachedCriteria , which provides a method count() that is supposed to return the number of records returned by the query. In fact, as far as I can tell, if dc is an instance of DetachedCriteria , then dc.count() == dc.list().size() must hold true. In the case of a Where Query containing a projection, count() seems to return something else. In this simple example: query = Runner.where { projections { groupProperty

How to avoid loss of DB when changing Grails domain class in development

坚强是说给别人听的谎言 提交于 2019-12-10 18:17:27
问题 One of the advantages of Grails 2.0 is that you can change domain classes in development without needing to restart the application server. This works, however when I change domain classes I lose all my bootstrap data, which basically defeats the purpose. I'm using the default h2 database. What is the best way to get around this? Do I have to go to an external DB like Postgres? 回答1: The default DataSource.groovy in a newly-created Grails 2 app has environments { development { dataSource {

How do I configure a grails domain class attribute to be stored as (postgres 9.4) jsonb?

时光毁灭记忆、已成空白 提交于 2019-12-10 17:38:07
问题 I've tried to configure a domain class like this: class Test { String data static constraints = { } static mapping = { data type: 'jsonb' } } This throws an exception (the reason, in the end, being Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: jsonb, at table: test, for columns: [org.hibernate.mapping.Column(data)] ). I also tried column: 'data', sqlType: 'jsonb' , which creates a text column named data . How do I correctly

Table creation fails

爱⌒轻易说出口 提交于 2019-12-10 17:11:47
问题 I'm using an H2 database for testing my Grails app. I have some simple domain classes like: package mypackage class UserSession { User user String sessionTokenHash // last seen info String lastSeenIP Date lastSeenTime String lastSeenUserAgent String lastSeenURL } however, the table doesn't seem to get created correctly. hbm2ddl.SchemaExport Unsuccessful: create table user_session (id bigint not null auto_increment, version bigint not null, last_seenip varchar(255) not null, last_seen_time

Sort based on column in child table using GORM?

我们两清 提交于 2019-12-10 16:48:21
问题 I have a table called employee and child table address. Now I want to get a list of employees sort by address1 in address table using GORM. Employee.findAllByName(name, [max: maxRecords, offset: 100,sort: Address.address1, order: desc]) the above statement is not working, any suggestions would be appreciated. Thanks 回答1: Try using a criteria query like so... def c = Employee.createCriteria() def results = c.list (max: maxRecords, offset: 100) { eq("name", name) address { order("addres1",

GORM: List all the domain instances belonging to User (root object)

隐身守侯 提交于 2019-12-10 15:29:59
问题 I'm working with grails since a while. There is something I still don't know how to implement correctly. I have a domain class (let's say User) which contains a List which can be potentially any domain class (Item, User, etc, etc). Is there a way to make this out of the box? At the moment I'm doing it the following way: I have a UserLink which contains following properties: class UserLink{ User user String className Long refId } Then I have a service which loads all links for a given user and

Getting the SessionFactory for a particular Datasource in Grails

走远了吗. 提交于 2019-12-10 15:11:38
问题 So if I want to do a direct SQL query using the session that Grails is using prior to supporting multiple datasources I could do: def conn = new Sql(sessionFactory.currentSession.connection()) Now the question is that I have multiple datasources and want to grab a connection to a specific one. How do I do that? TIA 回答1: Given a datasource defined in DataSource.groovy as "dataSource_foo", you'll have a SessionFactory called sessionFactory_foo . So you can dependency-inject it like any other

Setting default maxLength for Grails GORM Strings?

我只是一个虾纸丫 提交于 2019-12-10 13:56:20
问题 I know you can set default constraints via the grails.gorm.default.constraints config property by name by: grails.gorm.default.constraints = { '*'(nullable:true) } but is there a way to set it by type? I want to default all my strings to default to maxSize:2000 (primarily to force the default db mapping to not be to varchar(255)) 回答1: I don't think there's any way to do this easily in Config.groovy . You can create a custom dialect for hibernate without too much trouble though. For example

Persisting Maps and Lists of properties as JSON in Grails

穿精又带淫゛_ 提交于 2019-12-10 12:31:48
问题 EDIT: onload() method changed to afterLoad(): Otherwise objects might not be passed properly to the map. I am currently using some domain classes with a lot of dynamic, complex properties, that I need to persist and update regularly. I keep these in a Map structure for each class since this makes it easy for referencing in my controllers etc. However, since Grails does not seem to be able to persist complex property types like List and Map in the DB I am using the following approach to

Mapping two domain classes in Grails

随声附和 提交于 2019-12-10 11:49:20
问题 I have two tables in our database that need mapped. The first is a Student table. It looks something like this: id first_name last_name major_code_1 major_code_2 And the Major table is like this: id description I need to map the major codes of the student, where major_code_1 and major_code_2 point to an id in the Major table. How could I do this? Thanks! 回答1: Here is a simple model which maps to your schema: class Student { String firstName String lastName Major firstMajor Major secondMajor