Grails "duplicate field name" error in static mapping closure - grails

I am receiving the following errors in my "Class" class in Grails. For each field, it is telling me that I have a duplicate field. Which doesn't make any sense, because all I'm trying to do is map the fields with their associated table columns. The class fields and the fields in my mapping closure are all underlined. Here is my class so far:
package booklist
class Class {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static constraints = {
}
static mapping = {
//Uses the default datasource
table ''
columns {
id column: 'class_id'
name column: 'class_name'
description column: 'course_description'
instructor column: 'instructor_name'
courseNumber column: 'course_number'
lineNumber column: 'line_number'
bookTotalPrice column: 'book_total_price'
sequenceNumber column: 'sequence_number'
subjectCode column: 'subject_code'
}
}
}

You don't need to declared in static mapping the fields that you don't need to rename. Just write this:
package booklist
class MyClass {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static mapping = {
description column: 'course_description'
}
}
Grails works with the CoC (Convention Over Configuration) approach: if you don't need to change something, don't write it and a convention will be used.
For more details about the column mapping, take a look at the Grails documentation: http://grails.org/doc/latest/ref/Database%20Mapping/column.html

Related

Grails 3.2 - List field names in domain class that are not nullable

How can I get a list of field names for a domain class that are not nullable?
For instance, in the following domain:
class MyDomain {
String field1
String field2
String field3
String field4
static constraints = {
field2 nullable: true
field3 nullable: true
}
}
How can I get back the list ['field1','field4'] in a controller?
I'm validating rows in a CSV, and some of the row information is different from what is stored in the domain, so it would be preferable to get a List of String names rather than bind to a command object with exclusions.
You can use the constrainedProperties. It gives all the constraints of the particular domain class.
And now you want only the non-nullble constraints then filter out the result for it.
Example :
MyDomain.constrainedProperties.findResults { it.value.nullable ? null : it.key }
Output :
['field1','field4']
For grails 2.x users :
MyDomain.getConstraints().findResults { it.value.nullable ? null : it.key }
You need to use the PersistentEntity API
Set<String> propertyNames = [] as Set
for (PersistentProperty prop: MyDomain.gormPersistentEntity.persistentProperties) {
if (!prop.mapping.mappedForm.nullable) {
propertyNames.add(prop.name)
}
}
You may have to exclude things like version or timestamp properties depending on what you want.

ElasticSearch Grails plugin

it is my first time to use elasticsearch Grails plugin in my Grails2.5.1application , when i'm trying to search for age=35 using elasticSearchService.search("${age:35}").searchResults or using domainName.search("${age:35}").searchResults the searchresults is empty although there is a record in the DB age is equal to 35. And is there any useful tutorial for using ElasticSearch with Grails.
here is my domain:
class EmploymentSeeker {
String empType
String email
String fullName
String expYears
String socialStatus
Integer nubOfKids =0
String computerKnowledge
String militaryStatus
String haveDrivingLic
String gender
String eduQualification
String hasVehicle
String placeOfStudying
String courses
String currentTitle
String currentEmployerName
Integer age
Date dateCreated
static searchable = {
age boost:2.0
root true
except = ['email', 'fullName', 'placeOfStudying', 'currentTitle', 'currentEmployerName', 'dateCreated']
}
static constraints = {
}
static mapping={
}
}
Looks like you have a rogue '$' in your query string. It probably should be:
elasticSearchService.search("age:35")
${..} is needed only if you are passing in a query and want Groovy to replace the expression before invoking the ElasticSearchService.

Grails map domain class custom column name

In my Grails project I'm connecting to an existing DB and I'm having the following structure
class Country {
Integer country
String countryCode
String label
}
class Address {
String countryCode
....
}
DB tables:
country table
id, version, country, country_code, label
address table
id, version, country_code, ...
I would like to have something like:
class Address {
Country country
}
But it seems it is automatically looking for a column called country_id in the address table, I've tried using
static mapping = {
country column: 'country'
}
But I'm still having the same result.
I know it would better to link it to the country.id but the DB is existing and I cannot modify it.
Anybody can help on this?
If you use one-to-many relationship, you do not need the property declared.
Under the class Country you can add
static hasMany = [addresses: Address]
And add
static belongsTo = [country: Country]
You don't need to add Country country field.
Then you can use addressInstance.country.countryCode in your view to display the data.
Check out this and add to domain class Country:
static mapping = {
id name: 'country'
}

Customizing the jointable column type of string lists in Grails domain classes

I've a domain class with a list of strings as one of many attributes.
Sometimes, the strings that are in the list have more than 255 chars. So,
how can I increase this limit in the database? Or change the column type for a CLOB or TEXT
type ??
If I understood you correctly, you want to change the type o the column in the join table of your domain class. You can do this in the mapping of your hasMany, through the type option.
class Person {
static hasMany = [nicknames: String]
static mapping = {
hasMany joinTable: [
name: 'bunch_o_nicknames',
key: 'person_id',
column: 'nickname',
type: "text"
]
}
}

Grails Scaffolding - define possible values for this property of a domain class

I am new to Grails. I have a Person domain class as :
class Person {
String firstName
String lastName
String gender
Date dateOfBirth
}
And wondering if I can define possible values for a property - say gender as {M, F, U} so that these three values will be listed in combo box when using dynamic scaffolding for Person controller.
Here I just wanted to know if there is such feature in Grails framework? If such feature exists , then how can I use it?
From the documentation http://grails.org/doc/latest/guide/scaffolding.html, you should be able to use an inList constraint:
class Person {
String firstName
String lastName
String gender
Date dateOfBirth
def constraints = {
gender( inList: ["M", "F", "U"])
}
}
This should scaffold to a select list for the gender field, depending on the version of Grails you're using. 2.0+ definitely does this.
Here is an alternative solution
class Person {
String firstName
String lastName
enum Gender {
M(1),
F(2),
U(3)
private Gender(int val) { this.id = val }
final int id
}
Gender gender = Gender.U
Date dateOfBirth
def constraints = {
gender()
}
}
This will store gender in the database as an integer (1,2,3) and default the gender to U. The benefit here is you can rename what F, M, and U mean without handling a data migration.

Resources