I have a domain class in grails... How do I get gorm to leave out this entity when it create the db? Just leave it alone.
If i understood, u want not create table from domain class? If yes, use this code inside domain class:
static mapWith = "none" // disable persisting into database
Sounds like you don't need it to be a domain class then. You could just make it a POGO in the src/groovy file. If my assumptions here are wrong please explain further what you're trying to accomplish.
You could use Command Objects.
http://grails.org/doc/latest/guide/single.html#commandObjects
They provide the data binding and validation of domain classes, but do not map to the database.
Related
I need to make a model class which wouldn't require tables creation but I still want to be able to use tags associated with domain classes used in views and hence dont want to create those classes in src folder. Is there a way to do that without generating custom tags?
The mapWith static property adds the ability to control if a domain class is being persisted.
Example:
class Airport {
static mapWith = "none"
}
In above example the Airport class will not be persisted to the database.
Please check this for example and this for documentation.
Hope this will helps u.
Just put your domain inside src/main/groovy and tables will not be created.
Or in some situations you can use the 'embedded' declaration inside your domain object's nested object.
I would like to add a few additional ldap attributes (actually just one) to the userdetail object. It seems like the only way to do that is to override the usercontextmapper classes which then involves extending person class and essence class within it. It seems like a little too much work just to add some additional attributes. Before pursuing that route I wanted to make sure that there isnt another easier way to accomplish this.
Basically I have an attribute called "collections" in ldap which I would like to have available on the Principal object within my application.
Thanks
You don't have to extend the internal classes if you don't want to. The only thing that the UserDetailsContextMapper requires is that the object you return from mapUserFromContext implements UserDetails.
So you should be able to just read the attributes you want (including "collections") from the LDAP context object (the DirContextOperations) and use these to create your instance.
I was wondering how to persist data to a hsqldb. For example I am trying to save a simple name to a database but I cant seem to figure out how to persist it.
The recommended approach is to create a domain class with a String name property. Then you can save it, and you're done. First, create the domain class:
$ grails create-domain-class com.foo.Person
Then edit the grails-app/domain/com/foo/Person.groovy:
package com.foo
class Person {
String name
}
In controller actions or service methods you can create, save, and retrieve data:
def heMan = new Person(name: 'He Man')
if ( !heMan.save() ) {
// Handle problems saving (e.g. constraint violations)
}
def h = Person.findByName('He Man')
println h.name
An alternative approach is to work with JDBC directly. You can have dataSource bean automatically injected into your controller, then use the groovy.sql.Sql class to query that dataSource. Check out this stackoverflow.com question.
Two ways:
GORM
raw JDBC (via spring's JdbcTemplate)
I would suggest starting with a good Grails tutorial such as this one at IBM or one of these. Learn to use GORM. It will make your life better.
I like having separate classes, one class represents the entity, and that a separate DAO (database access object).
Is this possible with rails and active record?
Most of what you would put into a DAO is already hidden inside of ActiveRecord anyway, so there's not much of a need to split these up. But, if you insist you can split out whatever methods you want into a separate Module and then include it in your model.
I need to add a JNDI datasource from a legacy database to my Grails (1.2.2) application.
So far, the resource is added to my Tomcat (5.5) and DataSource.groovy contains:
development {
dataSource {
jndiName = "jdbc/lrc_legacy_db"
}
}
I also created some domain objects mapping the different tables to comfortably load and handle data from the DB with GORM. But I now want to assure, that every connection to this DB is really read-only. My biggest concern here is the dbCreate- property and the automatic database manipulation through GORM and the GORM classes.
Is it enough to just skip dbCreate?
How do I assure that the database will only be read and never ever manipulated in any way?
You should use the validate option for dbCreate.
EDIT: The documentation is quite a bit different than when I first posted this answer so the link doesn't quite get you to where the validate option is explained. A quick find will get you to the right spot.
According to the Grails documentation:
If your application needs to read but never modify instances of a persistent class, a read-only cache may be used
A read-only cache for a domain class can be configured by
1. Enable Caching
Add something like the following to DataSource.groovy
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='org.hibernate.cache.EhCacheProvider'
}
2. Make Cache Read-Only
For each domain class, you will need to add the following to the mapping closure:
static mapping = {
cache usage:'read-only', include:'non-lazy'
}