views:

29

answers:

2

Hello all,

I am trying to set up a few domain classes. I will explain it in english, and I am wondering how the domain would be set up in grails. Capitalized words are my domains

An Employee has an Education. An Employee has many Employer (past and present). An Employee had one or many Project for each Employer. Project have a Role, Client...etc

Now my question is, when for example, I define Employer, will I put

 hasMany = [projects:Project]

and ALSO in Project put

 belongsTo = [employer:Employer, employee:Employee, client:Client]

Mind you - many employees may have worked on the same project, so I might want to figure out a way to define that?

Would I also put in Employer:

  ArrayList<Project> project = new ArrayList();
  static hasMany = [projects:Project]

Or is that redundant?

+1  A: 

Variable declaration is not the same as defining a belongsTo relationship. belongsTo mostly comes into play with cascading of persistence actions, notably deletes. For example, if you have two classes:

// Employee.groovy
Project project

// Project.groovy
static belongsTo = Employee

If a specific Project belongs to an Employee, and that Employee is deleted, the Project will also be deleted. Here's another SO question with a good answer.

For your second question, yes, defining the List is redundant. If you do:

static hasMany = [projects: Project]

The Collection is implicitly defined for the domain. However, there are certain cases where you may need to initialize the Collection for use within constraints. See this issue for more details.

Rob Hruska
You don't have to define it as long as you have used the map notation. You can simply have the hasMany = [foos:Foo] and then use that declaration for your constraint.
Gregg
Is that new as of 1.3? I know that I wasn't able to do it in 1.2.0, at least.
Rob Hruska
It may be. It's been a while since I was down on the 1.2.x branch of the code.
Gregg
I've removed my statement about that for now. I'm running a few tests in 1.3 to see if I still have the problem there.
Rob Hruska
I just tested this in Grails 1.3.4 and had the same behavior. If I don't explicitly define the Set, I get `null`-related errors when I try and perform constraint validation on it (per my example in my answer).
Rob Hruska
I updated my answer with a link to the relevant JIRA issue. We're both right - you can use constraints with or without defining the collection, depending on which workaround you use (from the JIRA).
Rob Hruska
+1  A: 

It is redundant but your example isn't entirely accurate to what you are describing. By default, when you define a hasMany, Grails will create a Set. What your code will do is use an ArrayList instead of a Set but the relationship is exactly the same. I assume you meant for your project ArrayList to actuually be plural (project*s*).

Also, just a side note, you should always use the Interface to declar your typed variables rather than an implementation:

List<Project> projects = new ArrayList<Project>()
Gregg
+1, particularly for the note about the Interface type
Rob Hruska
Just to be clear on this - the relationship will be exactly the same, but since I define projects (yes, you were right, meant for it to be "projects") as an ArrayList, then I will be able to use those special ArrayList methods on the objects, right?
Derek