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]
...
Related
I have these domain class:
class Product {
Manufacture manufacture
Model model
Category category
int price
String productCondition
String productDescription
}
class Manufacture {
String manufactureName
static hasMany = [products:Product, models:Model]
}
class Model {
Manufacture manufacture
String modelName
static hasMany = [products:Product]
}
class Category {
String categoryName
static hasMany = [products:Product];
}
I am wondering if I need a Manufacture class, Model class, and Category class or if I can just use a String manufacture, etc.. Is there any advantage to having those additional domain classes than just having String manufacture, etc when it comes to searching? Say I want all products that are manufactured by Ford, or all products that are category car. I am confused on when I should make a domain class vs just using a field.
Thanks
The choice between one solution or the other one depends on how you plan to query the application and adapt it to future changes.
You could use only a Product domain class and with the appropriate database indexes on manufacture, model and category String fields the queries would go fast. But with this approach you can not evolve easily your domain to add new fields to manufacture, model and category.
I always prefer the domain class solution because I don't know how the application is going to evolve.
And intermediate solution can be use embedded domain classes.
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.
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"
}
I have two classes in my Grails application. One is derived from the other. Is there any way to easily get the last instance of the second class created for the first class. Please see example:
class korridor {
String name
String description
static hasMany = [exceptions: Exception];
}
class Exception extends TerminKorridor{
String type;
static belongsTo = [terminKorridor: TerminKorridor]
}
I want to get the last exception created for a specific korridor.
Something like this: terminKorridor.exceptions.getLast()
There is no "last"
exceptions in your case is a Set. A Set has no order. Change your collection type to a List in the korridor class and then it will have an order. Mind you this requires a schema change to store the index.
Here is the link to the section of the user guide that explains this http://grails.org/doc/latest/guide/GORM.html#sets,ListsAndMaps
I need a way to be able to have a domain class to have many of itself. In other words, there is a parent and child relationship. The table I'm working on has data and then a column called "parent_id". If any item has the parent_id set, it is a child of that element.
Is there any way in Grails to tell hasMany which field to look at for a reference?
This is an example of what you are looking for (it's a snippet code I am running and it generates column parent_id). I don't think you need SortedSet:
class NavMenu implements Comparable {
String category
int rank = 0
String title
Boolean active = false
//NavMenu parent
SortedSet subItems
static hasMany = [subItems: NavMenu]
static belongsTo = [parent: NavMenu]
}
Furthermore, you can give name to the hasMany clause using the Mapping DSL, which is explained at http://grails.org/GORM+-+Mapping+DSL