views:

23

answers:

2

Using Grails i'm trying a dynamic finder like this one

Policy.findAllByResourceAndUser(resource,user)

But When i call this, grails raise this exception

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: object references
an unsaved transient instance - save the transient instance before flushing: gmedia.User;
nested exception is org.hibernate.TransientObjectException: object references an unsaved
transient instance - save the transient instance before flushing: gmedia.User

Do we need to save the parameter of the finder? Where i'm wrong?

A: 

http://www.grails.org/DomainClass+Dynamic+Methods#findAllBy*

Policy.findAllByResourceAndUser(resource,user)

capital "B" in "By" is the first thing I see wrong? Is that a type on the question?

 def res = new Resource(name:"resource name").save()
 def user = new User(name:"My Name").save()
 def policy = new Policy( user:user, resource:res, right: "right string").save()

 println Policy.findAllByResourceAndUser(res,user)

not elegant, but you get the idea, there must be a problem in the way you are saving your objects

your user object will "never" get saved with that code... you have to specify values for all of you properties or define your constraints appropriately. I think you should review the documentation for Domain Objects in Grails because it appears there are fundamental problems in your approach see http://www.grails.org/GORM+-+Creating+a+domain+class

Aaron Saunders
sorry, just a typing error. The actual code was correctly typed. i'll edit the question text.
BenZen
looks like ou have not saved/flushed the user object.. I wrote a test case and the code runs fine
Aaron Saunders
In fact i try to do a findAll... at a point where i should find no Policy (for testing purpose)
BenZen
@BenZen dont really understand your comment
Aaron Saunders
i did a test case too. The test that raise this exception, the resource is saved, the user is saved, but the policy doesn't exist. Plus there is no ressource. Is that a probleme?
BenZen
i tryed to modify my finder like this "Policy.findAllByResource", and it work, except that it is the the beheaviour i want
BenZen
@BenZen what are you saying? There is no resource?? there has to be a resource unless you say nullable is true. I would suggest you post the code from you test case because whatever it is you are saying is not coming thru very well
Aaron Saunders
I wont give my complete test case cause it's a bit complicated, but it end to be like this def res = new Resource(name:"resource name").save() def user = new User(name:"My Name").save() println Policy.findAllByResourceAndUser(res,user)
BenZen
your user object will "never" get saved with that code... you have to specify values for all of you properties or define your constraints appropriately. I think you should review the documentation for Domain Objects in Grails because it appears there are fundamental problems in your approach http://www.grails.org/GORM+-+Creating+a+domain+class
Aaron Saunders
ohh, you're totally right, can you put this on the answer. This way i ll be able to make my question as answered. Bonus question, why grom doesn't show a message to warn that i was trying to save an "uncomplite" entity?
BenZen
see grails validation for your last question http://www.grails.org/Validation
Aaron Saunders
A: 

@Aaron Saunders these are two domain class

class Resource{
static contraints={}
}

class User extends Resource{
  String name
  String password
  String email
  Date creationDate
  Date lastModicationDate
}

class Policy{
Resource resource
User user
String right
static mapping={
  user cascade:'all-delete-orpahn'
  resource cascade:'all-delete-orpahn'
}

Maybe it a consequence of the inheritance between User and Resource

BenZen
where is the Policy Domain Object?
Aaron Saunders
Completely unrelated, but your constraints block has to be static like the mapping block, otherwise it'll be ignored.
Burt Beckwith