Goddamn you, grails, #2


Right. So now it has decided – for whatever godawful reason – that if I create a UserTree object with some users, then it simply won’t save the users.

The code (stripping out unrelated properties) is:

class UserTree {
  Collection users = [];
  static hasMany = [users: UserTreeUser];
  static mappedBy = [users: "tree"];

  static mapping = {
    users cascade: 'all-delete-orphan', fetch: 'join', sort: 'userId', lazy: false
  }
}

class UserTreeUser {
  UserTree tree;
  static belongsTo = [tree: UserTree];
  static constraints = {
    tree nullable: false
  }
  static mapping = {
    tree column: 'USER_TREE_ID'
  }
}

[UserTreeService.groovy]

  def create(String workspaceName) {
    TreeNode n = new TreeNode();
    n.nodeType = NodeTypeEnum.U;

    n.utree = new UserTree();
    n.utree.ownerId = ((IbisUserDetails)springSecurityService.principal).name;
    n.utree.label = workspaceName;

    UserTreeUser owner = new UserTreeUser();
    owner.userId = n.utree.ownerId;
    owner.permissions = UserTreePermissions.A;

    n.utree.users.add(owner);

    n.save();

    return n;
  }

Doesn’t work. Don’t know why. The creation of the Node and USerTree object works (a user tree belongs to a node. I tried and tried to get table-per-class inheritance, but it just wouldn’t fscking do it, so I gave up).

Remove the explicit declaration and assignment of users? No – the collection is instantiated to null, and you cant add UserTreeUser objects to it. Ok, maybe it needs to be instantiated to some weird hibernate object. So make the UserTree, save and refresh, then add the UserTreeUser. Nope, can’t refresh – the object doesn’t exist yet. Save the UserTreeUser separately? Possibly, but that is wrong – UserTreeUser is a belongsTo object, it should be saved as part of its owning object.

How about: fuck these hibernate collections and just save everything by itself, use finders to traverse relationships? Yeeeees, that could work. But it shouldn’t be necessary. GORM and hibernate is such an agile, fast way to build and use a data structure! Why, you just c/p some cookbook code and away it goes!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: