org.springframework.dao.InvalidDataAccessApiUsageException when using grails dynamic finder - grails

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?

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
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

i faced the same issue today..i found the solution...there is a foreign key relation which is getting set as NULL.....so when it tries to save null it throws this exception..so make sure all the values are not null...else u may need to change the cascade definition in ur mapping xml..

Related

Grails, GORM, relationship. Optional child records

For example, I've parent class Cafee:
class Cafee {
String name
static hasMany = [
admin: Person
]
}
and a child class Person:
class Person {
String name
static belongsTo = [cafee: Cafee]
}
I've done some records to Cafee using:
def user = new Person(name: "Andrew")
def a = new Cafee(name: "Tarelka")
.addToAdmin(user)
.save()
Adding child to parent works fine, but when I trying to create Person-instance separately, for example:
def visitor = new Person(username: 'testerUser', password:'password', firstName:'Иван', lastName:'Иванов', email:'ivanov#gmail.com', isAdminCafee: false)
visitor.save(flush:true)
I get an error:
ERROR context.GrailsContextLoaderListener - Error initializing the application: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person
Message: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person
How to fix it?
There are two things going on here that aren't entirely obvious. One is that this form of belongsTo
static belongsTo = [cafee: Cafee]
is bidirectional, as compared to
static belongsTo = [Cafee]
which is undirectional; in both variants a Cafee can access its associated Person instances via its admin collection, but using the second syntax there's no direct way for a Person to know which Cafee it's associated with.
Declaring a hasMany like you did creates a Set of Persons in the class, and its name is the key you used in the hasMany map, in this case admin; it's as if you had added
Set<Person> admin
but you shouldn't because it's redundant - an AST transform adds that property to the bytecode.
Similarly, when declaring
static belongsTo = [cafee: Cafee]
a field of type Cafee with the name cafee is added to the class. It's as if you added
Cafee cafee
but again, please don't manually add either, just be aware that they're there.
So the problem is that persistent properties are not-null by default unless you override with nullable: true, so if you printed out the errors for that Person instance you'd see at least one complaining that the non-nullable cafee property is null. This works with addToAdmin because that method does a lot. If instantiates the set to a new Set interface implementation (probably just a HashSet) if it's null (which should only ever occurr when the domain class instance is new; a persistent set will never be null, just empty or containing items), then adds the Person to the collection, and finally if the relationship is bidirectional, it sets the backreference on the Person to the owning Cafee.
So all you're missing is setting the Cafee manually, either as part of the map constructor
user = new Person(cafee: a, username: 'testerUser', ...)
or later in the workflow (but before validation or saving)
user = new Person(username: 'testerUser', ...)
...
user.cafee = a
...
user.save()

How to load any attributes without using the Mapping ' lazy: false' in Grails

Constantly faced with a problem when I need to compare and manipulate objects that reference other objects. For example:
Class Student {
...
String Name
Integer Age
...
}
Class Stuff {
...
Student student
...
}
When I invoke an instance of Stuff (Stuff.get (id)/load(id)) and will access the Name, Age and other attribute I see in debug mode (stuff .name = null, they're like 'null' although they are not null. It
command when analyzing values ​​of these attributes (stuff
.name == "pen") error occurs.
I need to invoke the instances and compare their values ​​to execute business rules, but do not know how to resolve this issue.
I read something about the inclusion in the configuration Stuff Mapping 'student lazy: false' for all the time you need to load the instance ofstuff , also charge the Student, but that in addition to overload the memory (since stuff is a Domain Great) would solve this case being the only solution to put all references as 'lazy: false' which would slow the application just to make a simple comparison.
Does anyone know how to invoke instances (Stuff), automatically invoking the attribute to be working (student) just to make the comparison of data, without using the 'student lazy: false' that invokes the data at all times?...
Using Grails 2.2.0 e o Groovy 2
Stuff don't have a property called name so you should get MissingPropertyException calling stuff.name. This has nothing to do with the lazy or eager relationship.
You can check the definition of a lazy relationship in the documentation and also the difference between the types of fetch.
To access the name property you need to access the student property before:
Stuff instance = Stuff.get(id)
println instance.student.name //this, if lazy, will trigger a new database query.
If you know that your code will access the Student instance by the relation with Stuff you could fetch both in one database access (eager and not lazy):
Stuff instance = Stuff.withCriteria {
eq('id', id)
fetchMode("student", FetchMode.JOIN)
}

How to dynamically add a property / field to a domain class in Grails?

For a project I'm currently working on I need to dynamically add properties to a domain class and persist them later in the database. In general, I need a key/value store attached to a "normal" domain class. Sadly I cannot use a NoSQL database (e.g. Redis).
My approach would be to handle the additional properties on a save() by identifying them within afterInsert or afterUpdate and writing them to another table - I would prefer not to use a map property within the domain class but an additional "Field" table (to better support searches).
I tried to add properties using the metaClass approach:
person.metaClass.middlename = "Biterius"
assert person.middlename == "Biterius" // OK
This works and I can identify the additional properties in the afterInsert/afterUpdate methods but it seems that I cannot change the value thereafter - i.e., the following does not work:
person.middlename = "Tiberius"
assert person.middlename == "Tiberius" // FAIL
Then I tried an Expando approach by extending the Person class by the Expando class (directly ("Person extends Expando") and via an abstract intermediate class ("Person extends AbstractPerson" and "AbstractPerson extends Expando")).
def person = new Person()
assert person in Person // OK
assert person in AbstractPerson // OK
assert person in Expando // OK
Both variants did not work - I could assign values to arbitrary "properties" but the values were not stored!
person.mynewproperty = "Tiberius" // no MissingPropertyException is thrown
println person.mynewproperty // returns null
So how can I add properties to a domain class programmatically during runtime, change them and retrieve them during afterInsert or afterUpdate in order to "manually" store them in a "Fields" table?
Or am I doing something completely wrong? Are there other / simpler ways to do this?
What about turning your DB into a "NoSQL" one?
In one of my projects, I just used a String-property to store a map as JSON-Object.
For Groovy it's not a big problem to convert between a map and a JSON-Object. And since you can access a map just like an object with properties, I found this solution very convenient.
Only drawback: you have to plan the size of your String-property in advance...
Update: sorry, just read that you want to support searches...
what about
class Person {
...
static hasMany = [extProperties:KeyValue]
...
def invokeMethod(String name, args) {
if (name.startsWith('get')) {
//an unknown properties's getter is called
}
//add same for setter
}
}
class KeyValue {
String key
String value
}
I guess such a schema would give you all freedom you need. Even without the hasMany, you can make use of invokeMethod to handle your external tables...
The getter and setter can save your values in a transient string propertie (static transients = ['myTransientProperty']). This property should be available in the afterInsert / `afterUpdate´ events.
Why don't you just create a map of strings on the domain object and store your extra data there manually? Unless you're storing complex data you should be able to cast anything you need to/from a string.

Overwriting Setter&Getter Domain class grails

I am trying to overwrite getter and setter for domain class. The rational for doing is so that i don't have to redo initialization of this domain class that is in so many places in code.
I have a domain class 'Bank' that used to have one 'address' and it was initialized as follows:
new Bank(address: Address)
Now, i need multiple addresses, so I have a mapping class BankAddress and Bank now have 'addresses' list of BankAddress
To keep initialization the same 'new Bank(address:Address)', i am rewriting 'getAddress' and 'setAddress' like the following:
Address getAddress(){
if(addresses?.size()>0){
addresses.asList().get(0).getAddress()
}else{
return null
}
}
void setAddress(Address instance){
if(addresses?.size()>0){
addresses*.delete()
addresses.clear()
}
def bankAddress = new BankAddress(address: instance, bank: this, isPrimary: true).save(flush: true)
addToAddresses(bankAddress)
}
Problem is that while my setter is called, the 'instance' parameter of the function is empty making to fail on save - new Bank(address:someAddress):
'AddressId', table 'someDB.Config.BankAddress'; column does not allow nulls. INSERT fails.
How come the setter has empty argument? ... I am newbee to groovy & its dynamic nature
Thank You for your help
In your setter you're creating a new BankAddress object and then doing a save(flush:true). One of the properties of your BankAddress object is the submitted Address object. If the submitted Address object hasn't been saved to the database already, the new BankAddress object will try to save a null id in its foreign key column in the database, which will fail because the foreign key column is not nullable.
Make sure your submitted Address object has already been persisted to the database so it has a primary key id, or delay persisting the new BankAddress object until later.

Grails : data binding

i'm trying to create some domain objects from xml.
class A {
String name
}
class B {
A a
int something
}
i first created an instance of A,and flushed. when creating B, first map the available attributes.
def b = new B(xml.attributes())
this would map 'something' correctly, but not the object type A. So, I retrieve the instance of A and add like
b.a = A.findByA("id of a")
I could see the object b is constructed (both fields filled in) in the debugger, but it doesn't persist on save(flush:true).
What is wrong in the above assignemt, or should use the id instead (b.a.id = ..)
How can I see what is going wrong in the log file? which trace needs to be enabled. I enabled there in config file
trace 'org.hibernate.SQL', 'org.hibernate.type' (which gives the sql trace for insert, select etc. But not for the above scenario, may be because it doesn't reach to hibernate).
Any pointer, highly appreciated.. thanks.
I would wager to guess that your save() is failing validation. You can add save(failOnError:true) to throw an exception when the validation fails, or add the following code to print each of the errors:
b.errors.allErrors.each {
println it
}
With the debugging tip from Rich, I could narrow down the problem... had to rename the attribute to prevent auto mapping. See a similar issue, and response at http://grails.1312388.n4.nabble.com/domain-controller-and-Failed-to-convert-property-value-of-type-problem-td1357947.html
To create association you must pass an object of A
new B(a:A.get(id))
or
B b = new B()
b.a = A.get(id)
Where id must be Integer or Long
Either I miss some context but class A doesn't have method findByA. There is no such A attribute for class A. Suggest you to use method get for strict findings.

Resources