gorm

Many-to-Many link tables in grails (GORM) / hibernate

天大地大妈咪最大 提交于 2019-11-28 01:28:39
问题 I'm playing aroud with Grails and am finding the ORM stuff tedious because I don't fully understand what I'm doing when it comes to domain classes. I'm hoping someone can put me back on track Consider the following Test Job One:Many Hardware Used on Job Many:One Physical Hardware ...this is analogous to the classic Order, OrderLine, Product scenario seen in university DB examples I've created the following domain classes class Job { String jobName String jobDescription } class HardwareOnJob {

GORM createCriteria and list do not return the same results : what can I do?

断了今生、忘了曾经 提交于 2019-11-27 18:23:37
问题 I am using Nimble and Shiro for my security frameworks and I've just come accross a GORM bug. Indeed : User.createCriteria().list { maxResults 10 } returns 10 users whereas User.list(max: 10) returns 9 users ! After further investigations, I found out that createCriteria returns twice the same user (admin) because admin has 2 roles !!! (I am not joking). It appears that any user with more than 1 role will be returned twice in the createCriteria call and User.list will return max-1 instances

Grails/GORM: The meaning of belongsTo in 1:N relationships

南楼画角 提交于 2019-11-27 18:22:20
问题 In an ordinary one-to-many mapping the "one"-side is the owner of the association. Why would anyone use the belongsTo-mapping for such a mapping? Am I missing some side-effect of specifying belongsTo? In other words: what are the effects of specifying a belongsTo-mapping in GORM vs. not specifying it? 回答1: Whether to specify belongsTo depends upon the type of referential action you want. If you want Grails to do On Delete, CASCADE referential action, then DO specify belongsTo . If you want

Do I ever need to explicitly flush GORM save calls in grails?

≯℡__Kan透↙ 提交于 2019-11-27 17:31:27
I have a strange situation which appears to indicate a GORM cacheing problem //begin with all book.status's as UNREAD Book.list().each { book.status = Status.READ ; book.save() } println (Book.findAllByStatus (Status.READ)) //will print an empty list println (Book.list().findAll (it.status == Status.READ)) // will print all books I cannot understand why the last two queries could return different results. However if I make the following modification of book.save(flush:true) . Both of the println statements will return all books. I was under the impression that this was not necessary within a

Is it possible to map a table name for a domain object dynamically in grails?

霸气de小男生 提交于 2019-11-27 15:36:45
问题 I have a domain that looks something like class Foo { String name static mapping = { table 'foo' } } but I want to make is more like : static mapping = { table "foo_${dynamicVarThatComesFromRequest}" } What I want to know is whether this is even possible? Thanks! 回答1: It is possible. You can add a Hibernate interceptor to process all SQL statements and parse/replace some token in the table name you enter in the mapping with the actual table name you want to use. src/groovy

Grails GORM domain association across two datasources

不打扰是莪最后的温柔 提交于 2019-11-27 15:04:43
问题 Is it possible to have an association between two domain classes (i.e. belongsTo ) if the other domain class uses a different datasource? The two datasources are different database drivers too. I suspect this may not be possible, but I wanted to reach out to the community here to see if it was possible. Right now I'm trying to do it and I am getting the usual suspected Hibernate error: Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from

How to order by more than one field in Grails?

99封情书 提交于 2019-11-27 10:44:08
问题 Is there a way to get a list ordered by two fields, say last and first names? I know .listOrderByLastAndFirst and .list(sort:'last, first') won't work. 回答1: This old solution no longer works. Please see mattlary's answer below You may have to write a custom finder in HQL or use the Criteria Builder. MyDomain.find("from Domain as d order by last,first desc") Or def c = MyDomain.createCriteria() def results = c.list { order("last,first", "desc") } 回答2: Hates_ criteria answer didn't seem to work

How to change primary key column in grails?

我们两清 提交于 2019-11-27 06:45:17
问题 I have a domain class having an Integer variable 'code'. my requirement is to make 'code', primary key column for that domain and also auto increment and to remove the default 'id' column from the table created for that doamin.thnks 回答1: use this : static mapping = { id name: 'code' } more informations here : http://grails.org/doc/2.0.x/ref/Database%20Mapping/id.html 回答2: Domain classes in Grails by default dictate the way they are mapped to the database using sensible defaults. You can

Using groupProperty and countDistinct in Grails Criteria

喜欢而已 提交于 2019-11-27 06:15:59
问题 I'm using Grails 1.2.4. I would like to know on how can I sort by "countDistinct" (descending) and with groupProperty inside a projections. Here are my domains: class Transaction { static belongsTo = [ customer : Customer, product : Product ] Date transactionDate = new Date() static constraints = { transactionDate(blank:false) } } class Product { String productCode static constraints = { productCode(blank:false) } } In MySQL terms, this is what I want: select product_id, count(product_id)

How to configure Hibernate to put quotes around table names

回眸只為那壹抹淺笑 提交于 2019-11-27 04:43:45
问题 I've got a situation where I'm trying to create a table called 'user' in Postgres, which throws an error due to Hibernate not putting table names in quotes: | Error 2012-02-27 23:06:58,782 [Thread-10] ERROR hbm2ddl.SchemaExport - Unsuccessful: create table user (id int8 not null, version int8 not null, account_expired bool not null, account_locked bool not null, email_address varchar(255) not null, enabled bool not null, first_name varchar(255) not null, last_name varchar(255) not null,