domain class in grails - grails

please help me
I have this class
Class Person
{
String name
String id
Date date
}
This class is connected with mysql
my question is where put this:
Person.count() or
Person.findByAllName("Carlos")
Class Person
{
String name
String id
Date date
def person = Person.count() //here???
}
I want to create a field person that show how many register have the table.
is possible with scaffold only, because have error the moment of run app?
Thanks and sorry by my bad english¡¡¡

The correct place would be outside the domain class. Also, you need not define the Id on the domain class, as it is autogenerated

Related

Grails: How to assign domain field = someValue fetching from database

Suppose I have two domains:
class Country {
String name
String Desc
static hasMany = [organizations: Organization]
}
class Organization {
String name
Country country
static belongsTo = [country:Country]
}
Note: In case of create Organization, countries will be listed in a combobox.
Suppose I have 4 row in my database which I have fetched from bootstrap.
**Name** **Desc**
US USA
CA CAN
NP NEP
IN IND
I want to assign USA as a default value while create a Organization. Is there a way to do it from domain level not gsp?
I think the load method is your best bet, although it requires that you know the id of the instance that you want to be the default. It's not a good idea to hard-code this because it could change in the future. But you can look it up when the app starts and save the id, e.g.
class BootStrap {
def grailsApplication
def init = {
def usa = Country.findByName('US')
grailsApplication.config.COUNTRY_US_ID = usa.id
}
}
Use whatever Config key you want of course.
Then change the Organization class to call load in its constructor:
import grails.util.Holders
class Organization {
Organization() {
if (Holders.config.COUNTRY_US_ID) {
country = Country.load(Holders.config.COUNTRY_US_ID)
}
}
String name
static belongsTo = [country:Country]
}
Note that I removed the Country country property because it's redundant; adding that belongsTo creates a property with the same name as the map key ("country") and with the same type as the map value (Country). It doesn't hurt to have it, but it's not needed.
The reason load is appropriate here is that it doesn't hit the database until you access a persistent property other than id. So you're configuring it to lazily retrieve the real instance if necessary, but if you overwrite the value with a different instance, you won't have wasted a database lookup for the initial population. Turn on SQL logging to verify this.

Creating one-to-many & many-to-many for same domain class in grails

I want to create a domain class as like , One user can post many orders [Bidirectional] and one order can be liked by many users [unidirectional].
I have written a domain class as shown below ,
Class User {
String userName;
List orders
static hasMany = [Order]
}
Class Order {
String orderId
String orderName
//Indicates this order belongs to only one user
static belongsTo =[owner : User ] // Bidirectional
//Indicates order can be liked by many users
static hasMany = [likedUser : User] //Unidirectional
}
But I am getting am error saying invalid schema . Any body please help...
This post looks similar to my question but I am not getting , Please help.
First, order is a reserved word in SQL. Since GORM by default creates a table with the same name as your class, you'll need to either rename your class or provide a different name to use when mapping to SQL tables.
For example:
class Order {
static mapping = {
table 'user_order'
}
// ...
}
Another problem is that Order contains two associations to User. You need to tell GORM which one of these that is the bi-directional association from User to Order. That can be achieved using mappedBy, like this:
class User {
String userName
static hasMany = [orders: Order]
static mappedBy = [orders: 'owner']
}
Hope this helps.

Gorm mapping for inheritance

I'm facing a issue regarding inheritance in Grails.
I have a domain class Person.grooy:
class Person{
String name
String contactNumber
Address address
}
Now I'm extending Person.groovy for Employee and Customer like:
class Employee extends Person{
String designation
}
class Customer extends Person{
String interest
}
Now I want separate table in my database for Employee and Customer having columns of Person i.e name,contactNumber and associate address key.
How could I achieve this. I searched every where but there is nothing for this aspect.
Is this one approach is not possible in GORM.
Please answer.
Thanks Guys
Finally I managed to get what I want just by placing a grails.persistence.Entity annotation to my child domain classes. I also make my parent i.e. Person.groovy abstract and place in src/groovy.
Now I have database hierarchy as I expected but some scaffold issues in controller still persist that will also sorted out with your help.
You need to disable table-per-hierarchy, which is by default enabled in Grails
class Employee extends Person{
String designation
static mapping = {
tablePerHierarchy false
}
}
table-per-hierarchy Ref
If you put your Person class in src/java or src/groovy it won't be mapped to the db.
Remember to import it into your Employee and Customer classes
import com.yourPackage.Person
class Employee extends Person{
}
It looks like inheritance is not the approach we need to follow here. You should create composition with Person class and it will store the properties of Person class in Employee.
class Employee {
Person person
String designation
static embedded = ['person']
}
Gorm Composition
you can put it inside src/java, but that solution will not be standard, as it really will not be treated as a grails domain example once you get deeper into the application.
For example, if you want to create a controller or a test script on the extended domain as per the previous answer, it will be complicated.
As of grails 2.2.x I believe, grails provides you with mapWith. You can use that for a more maintainable solution
class Employee{
static mapWith = "none"
}

Grails domain List field not being persisted

I have a domain class
class Something {
String name
String email
List<String> friends = []
}
In the domain class itself I have a method for Something objects which populates the friends list. But for some reason I am not able to persist this list. and it gets made null on browsing away from the gsp relevant to the domain modifying action.
Any suggestions as to why this is happening?
I think you instead need to write your domain as:
class Something {
String name
String email
List friends
static hasMany = [ friends: String ]
}

Retrieve collection in domain class from Database

We use dynamic scaffolding in our project and hence place maximum coding in Domain itself.
I have a requirement where I want to retrieve a collection for a Domain class property from the same domain class.
Example :
class Person{
String name
String school
}
school property should be a dropdown containing list of all schools so far available in the Person table. If no value available, it can be empty dropdown.
Any suggestions to achieve this in domain class itself?
That is what
static hasMany is for: http://grails.org/doc/latest/ref/Domain%20Classes/hasMany.html
in your case, something like below will work , once you create a School Domain object:
class Person{
...
static hasMany = [schools: School]
...

Resources