Property [] of class [] can not be null error in Grails - grails

I am new to Grails and getting this error on two out of my three domains when I run the project on all the domains content. The domain the works is
class Location {
def scaffold = true
String company
String name
String address
static belongsTo=[company:Company]
static constraints = {
}
}
The domain that does not work is
class Report {
def scaffold = true
String title
String location
Date published
static belongsTo=[location:Location]
static constraints = {
}
}
I can not see the structural or syntax issue that is trowing the errors. I have been trying a variety of adds and subtracts and can not seem to find anything that address this error at a basic level. Again I have just started using Grails and Groovy

After doing the changes told by #araxn1d, you should also check the constraints. By default all properties are not nullable (that's why you're getting for example the error for the title property)
If you already have data in your database you have two options:
Update your database and set the correct values in each table or row
Set each property in the domain as nullable. For example
static constraints = {
title nullable:true
}

Are you creating a Report with no properties set? By default, Grails will check that all properties are not null. If you want to allow the user to leave a certain field undefined (null), then you have to explicitly tell Grails in the constraints map:
static constraints = {
propertyName nullable: true
}

Seems like error is in this line:
String location
location is String type, but should be Locationtype. The same as String company in Location domain should be Company company.

I think the problem is that you've changed the structure of the domain class after you generated the automatically created views/controller for the domain.
I fixed it by just deleting the offending domain along with the controller/views file and recreating them. I'm sure you could root through and find the offending code though.

Related

Is any package name restrictions applied in grails?

I have created a Domain class called Action under package promotion
package promotion
class Action {
String name
String code
String address
static constraints = {
name blank:false
code maxSize:5
address nullable:false
}
}
Issue: Here my problem is I can not generate controller&views. And also I can not change default error messages from message property for my constraints
example: promotion.Action.name.nullable=Please enter the action name
Note: But if I change my package name "promotion" to something like "com.app.act" it works fine.
Try to make package name little brief. So it can be a good coding practice also

Unique key parts not showing in Create Form Grails

I have following Domain Class -
class xyz {
String name
String version
String vclass
Date dateCreated
static constraints = {
version(blank:false)
name (unique: ['version'])
}
static hasMany = [ yz: Yz ]
}"
when i create a record for it, i don't see an option to enter version, only name and vclass. Why?
In Grails, version is a special property of Domain classes used for optimistic locking. Rename your property to something other than version. You can read more about this in the user guide.

constraints in grails

Hi I am having some trouble getting my constraints to work in my grails project. I am trying to just make sure that the field for Site_ID is not left blank but it still accepts an blank input. Also I am trying to set the order in which the fields appear but even that is not reflecting on the page when I try it out. Here is the code:
package translation
class J2_Translations {
String Site_ID
String I18NKey
static constraints = {
Site_ID(blank:false)
I18NKey()
}
}
and here is my code for the controller, I am not doing anything special I just want the constraints to work
package translation
class J2_TranslationsController {
def scaffold = J2_Translations
}
thanks,
Ameya
Grails is a convention-over-configuration framework. Make sure you follow the standard Java naming conventions. Properties should be named with camel-case identifiers with the leading character in lowercase. For example:
String siteId
String i18nKey

String size in Grails too big for database field

I have Message domain class that is used to send an email to site administrators
class Message {
String name
String subject
String body
}
I thought String would not have a maximum size but when people put in messages that are too big then I see exceptions in my log files. For now I put a constraint in to limit the size of the message to 250 but cannot make it bigger or else the save fails. I am using PostgreSQL 9.1.1 and Grails 1.3.7. Does anyone know a way to fix this?
You can specify the data type using a constraint:
static constraints = {
body type:'text'
}
Add this to your domain class:
static constraints = {
body(maxSize:1000)
}
Setting type in constraints block doesn't work!
Put in the mapping block instead.
static mapping = {
body type: 'text'
}
The max size of a VARCHAR is set in the database, Grails doesn't have any say in the matter. What you can do is tell Grails what you need, and it will generate DDL that uses a clob instead of a string.
Change the mapping for the column to use type: "text", while adding the maxSize you want in your constraints. Grails will figure out from this that it should set the column data type to clob.
(Generally a clob field will hold up to 4GB, though some databases allow changing that.)

Grails domain ID column validation issue

I have a simple domain object
class MyDomain
{
String id
String name
static constraints =
{
id unique:true
name nullable:true
}
static mapping =
{
table 'schema.MyDomain'
id column:'MY_ID', type:'string', generator:'assigned'
}
}
The issue I am having is when I call validate on the object, it returns true even when the id field is null. I had thought that all columns were nullable:false unless explicitly stated otherwise. If I change the line
id unique:true
to
id unique:true, nullable:false
then it seems to work fine. My main question is, why do I have to explicitly set nullable for the ID column? It is just a small line of code, but I don't like just adding in the tag of code without understanding why in case it is a symptom of a bigger problem.
The id column is auto generated and auto populated(when versioning is turned on[true by default]) and you shouldn't have to declare a new one.
This default id column is nullable:false by default and you can still set the mapping properties and id generation strategies like you have done above against it.
However if you want to define the default constraints for all domain in you app, you can do it globally by setting thwe following in your config.groovy file.
grails.gorm.default.constraints = {
myShared(nullable:true, size:1..20)
}
For more on constraints see the Grails documentation.

Resources