db4o - unique constraints on a group of fields - db4o

I would like to have unique constraints operate on more than 1 field for several entities. How can I achieve this with db4o?
Thanks,
Walter

Currently db4o doesn't support the UniqueConstrain on multiple fields. You can set unique-constrains only field by field, but not combine them.
#Ladlestein Well the intention is good, but it doesn't work. db4o manageds objects by it referencial identity. When you apply the unique-constrain on the Foo.name, you ensure that the reference is unique. So no other object can have the same reference to a name object. But you're not interested that the reference is unique, but you want to have a unique content of the names.

Sounds like a composite key. Can you create a new class that contains the key-constituting fields, and use a member variable with that type in place of the key-constituting fields in your original class?
i.e. where you had
class Foo {
String given_name;
String family_name;
}
instead use
class Name {
String given_name;
String family_name;
}
class Foo {
Name name;
}
and make Foo.name the unique field?

Related

Is it possible to refresh a property in Grails?

In order to refresh a domain object i.e to re-read the data from database we do refresh().
def b = Book.get(1)
…
b.refresh()
I am wondering whether we can refresh a property of the domain.
Suppose i have bound params to Book object and suppose i want to unbind the author property from the book object then is it possible to achieve that?
Let's consider the Book is defined as
class Book {
String title
String author
String category
}
Suppose I do bindData(bookInstance, params). This will bind to all properties. I want to unbind the author after bindData. Is this possible?
It sounds like you just want to exclude binding a particular property.
bindData(bookInstance, params, [exclude: 'author'])
will bind all of the Book properties except for those listed.
You can conversely use include to explicitly list which properties to bind from params.
bindData(bookInstance, params, [include: 'title', 'category'])
I solved this by using bookInstance.author = bookInstance.getPersistentValue('author').

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.

GORM mapping: make an index unique

I'm feeling a little slow today. I'm trying to do something that I think is very simple. I have a Domain class with a property called 'name'. I want 'name' to have an index, and I want the index to require that the 'name' is unique. I've set the unique constraint and tried creating an index. I can't make sense out of the Gorm docs as to how I add the unique attribute to the index. Here's some code:
class Project {
String name
static hasMany = [things:Things]
static mapping = {
name index:'name_idx'
}
static constraints = {
name(unique:true)
}
}
All is well with the above, except when do "show indexes from project" in mysql it shows my name key as not unique. I know the problem is that I am not specifying unique in the mapping, but quite frankly the docs for gorm are making my head hurt. I see all kinds of stuff about columns, but I can't find a single example anywhere on the web that shows what I want to do. I don't need complex mappings or compound keys, I just want to know the syntax to add the unique attribute to the mapping declaration above. Any advice welcome.
I also did a grails export-schema and see the following:
create index name_idx on project (name);
Nothing in that to indicate this index requires unique values
A related followup question would be once I succeed in making that index unique, what type of error should I expect when I go to save a Project instance and the name is not unique? Is there a specific exception thrown? I realize that even if I check that a given 'name' is unique there's still a possibility that by the time I save it there may be a row with that name.
I'm quite sure the syntax to do what I want is simple but I just can't find a simple example to educate myself with. I've been to this page but it doesn't explain HOW the uniqueness is enforced. I'd like to enforce it at the name index level.
The indexColumn allows additional options to be configured. This may be what you're looking for.
static mapping = {
name indexColumn:[name:'name_idx', unique:true]
}
Grails Documentation for indexColumn
If you put only the unique constraint the GORM send DDL to create an unique index on database.
static constraints = {
name nullable: false, unique: true
}

Mapping hasMany = [field:String] to a database field TEXT

Is it possible to set the database column type of a hasMany association to a primitive type (in my case string) to a specific value? I know how to set a field (see this question), but this is different.
List textRecords
static hasMany = [
textRecords:String,
]
I want to make sure that my textRecords are mapped to a TEXT or LONGTEXT database type.
I could create a separate domain class that contains just one string field and map that field, but that seems like a kluge.
The Grails User Guide has an example listed in 5.2.1.4 Basic Collection Types section that is very similar to what you want to accomplish.
Keep in mind that every time you want to add a textRecord to that object it will have to load the entire List of textRecords in order to save it again. This may not be the behaviour that you want for performance reasons.
You'll end up having a mappings block like this:
static mapping = {
hasMany joinTable:[name:'bunch_o_text_records', key:'domain_id', column:'text_record', type:"text"]
}

Map of other types than Strings in Grails

I created simple domain class with map within it.
class Foo {
Map bar
}
Bar mapping will be created as sth like:
create table foo_bar (bar bigint, bar_idx varchar(255),
bar_elt varchar(255) not null);
...as stated in http://www.grails.org/GORM+-+Collection+Types:
The static hasMany property defines
the type of the elements within the
Map. The keys for the map MUST be
strings.
Now my question is - is it possible to create map of values other than Strings? I can achieve that using pure Hibernate (element mapping) - any ideas how to port this to Grails?
I think you meant if it's possible to create map of KEYS other than Strings.
It is not possible: all keys must be Strings, while values can be whatever type you want.
A way to achieve what you want is using some unique identifier for the type of class you want as key of your map.
Say you want a Map persisted in your database and say you have two instances: objectA and objectB you want to persist in your map, which name is "relationship":
relationship."objectA.toString()" = objectB
That should work. Changet toString() with hashCode(), getId() or whatever thing that gives you a unique String that identifies that object and only that, and you got it.

Resources