I have written a domain class
class ReportCallByUser {
Integer userId;
String userName;
String reportName;
Date timeOfReportCall;
static constraints = {
}
static mapping = {
timeOfReportCall index: 'time_of_report_call_index'
}
The last line creates an index in database but in ascending order. How can I create index on 'timeOfReportCall' with descending order?
Thanks in advance.
There's no such thing as order in regards to index creation. It's the query's ability to specify the ordering:
ReportCallByUser.list( [ sort:'timeOfReportCall', order:'[desc|asc]' ] )
I am assuming that you want the results in descending order by default when you query the database using GORM. If that is the case, you can specify sort order in your mapping as such.
static mapping = {
sort timeOfReportCall:"desc"
}
Hope that helps.
Related
I have a few domain classes similar to the following:
class Position {
String code
String title
static hasMany = [relations: Relation]
}
class Unit {
String code
String title
static hasMany = [relations: Relation]
}
class Relation {
Position position
Unit unit
static belongsTo = [
position: Position,
unit: Unit
]
}
I am attempting to use criteria to find all positions that do not have any relations. I know this can be solved using HQL but I find the criteria to be cleaner when building dynamic criteria versus building a dynamic HQL string.
Is there a way to use criteria to do something like:
Position.withCriteria { isNull('relations') }
I have tried the above but I always get a list containing 0 elements even though I know there are unrelated positions in the table.
For collections you need to use isEmpty() instead of isNull().
Position.withCriteria { isEmpty('relations') }
My domain class is
class RoomWantedAd{
Set<MateAgeRange> mateAgeRanges
static hasMany=[mateAgeRanges :MateAgeRange]
}
Her MateAgeRange is :
enum MateAgeRange {
TWENTIES('18-29')
,THIRTIES('30-39')
,FOURTIES("40-49")
,FIFTIES("50-59")
,SIXTIES("60+")
final String value
private MateAgeRange(String value) {
this.value = value
}
String toString() { value }
String getKey() { name() }
static belongsTo=[roomWanted:RoomWanted]
}
My problem is searching. In the search page, a person can select 0 or more values in [18-29, 30-39, 40-49, 50-59, 60+]. In the db, 0 or more values among [18-29, 30-39, 40-49, 50-59, 60+] are stored in field 'mateAgeRanges'.
Let db contains [30-39, 50-59] in 'mateAgeRange' field. Let in the search page, the user selects [18-29, 50-59, 60+]. Then the Ad corresponding to the above list must be returned. This is because at least one value in user's selection is present in the db list. How is it possible. Is it possible using an SQL query or grails GORM query.
you have to use exactly the same <g:select/> tag as you are using for create/update. Thus, you will see human readable values like THIRTIES in browser, but in the background the '30-39' values will be used.
Using MVC 4 with EF code First.
When doing linq to EF selects statements, the collections are being populated with what seems like the last record's data. And what's weirder, only some of the properties are repeated where others are not. It is best to show you by example:
Using this query returns the proper data:
var orders = db.Orders.ToList();
OrderID OrderTotal Name
1 215.00 Bob
2 415.00 Mark
3 315.50 Ralph
When I filter the Orders entity by a SubscriberID, which is a foreign key, like so:
var orders = db.Orders.Where(s => s.SubscriberId == 2).ToList();
The data end up looking like this:
1 315.50 Bob
2 315.50 Mark
3 315.50 Ralph
Notice how the OrderTotal is repeated but the names stay the same. Please note that this is not a view issue. When I look at the data in the collection while debugging in the controller, this is what I see. This does not appear to be the only place this is happening. I am seeing something similar with more complicated models - but I thought I'd start with the simplest sample. Thanks!
The problem was caused when I was setting default values on my models. Apparently using static was incorrect. So:
[ReadOnly(true)]
[Display(Name = "Sub total")]
private static decimal _OrderSubTotal = 0;
public decimal OrderSubTotal { get { return _OrderSubTotal; } set { _OrderSubTotal = value; } }
Should be written as
[ReadOnly(true)]
[Display(Name = "Sub total")]
private decimal _OrderSubTotal = 0;
public decimal OrderSubTotal { get { return _OrderSubTotal; } set { _OrderSubTotal = value; } }
How can I create named unique constraint for multiple columns?
I've three classes:
class Descriptor {
// some columns
}
class Protein {
// some columns
}
class DescriptorValue {
// some columns
static belongsTo = [protein: Protein, descriptor: Descriptor]
static constraints = {
protein(unique:['descriptor'])
}
}
GORM creates an index with an auto generated name that is different for different environments. How can I specify its name?
Try to do it like:
static mapping = {
protein unique:['descriptor'], index: 'protein_idx' //or whatever name you like
}
If you need to use multi-column indices, then you can specify for every property the same index name.
String field1
String field2
Integer field3
SomeObject object
static constraints = {
object unique: ['field1','field2', 'field3']
}
I have the following piece of code in ItemController.groovy
def list = {
params.max = 60
def storeYYId = params.id
[itemInstanceList: Item.list(params), itemInstanceTotal: Item.count()]
}
I have the following in Item.groovy:
class Item {
String itemName
static belongsTo = [store:Store]
static constraints = {
itemName(blank:false)
storeId()
}
}
This gives me an error since it tells me that there is no storeId property, but there is, since store_id is a foreign key to the Store table in the corresponding database.
Question1. How do I tell grails to let me access the properties of domains that are autogenerated by GORM, like the id and storeId in this case?
Question2. What code should I write in my ItemController.groovy in my list action, in order to retrieve only a list of items where the storeId == storeYYId ?
Question1. How do I tell grails to let me access the properties of
domains that are autogenerated by GORM, like the id and storeId in
this case?
You should be able to access autogenerated properties in exactly the same way as you access properties that you define. The reason you're getting an error is because Grails does not automatically generate a storeId property for the Item class, the only properties it will autogenerate are version and id (for both Item and Store).
Question2. What code should I write in my ItemController.groovy in my
list action, in order to retrieve only a list of items where the
storeId == storeYYId ?
You'll need to write either a HQL or criteria query to retrieve these items. The criteria query would look something like this (untested)
// Get all items that have storeId = 6
def storeId = 6
def items = Item.withCriteria {
store {
eq('id', storeId)
}
}