Fork me on GitHub

29 Jun 2010

Acyclic relationship validation in Grails

A common domain class use-case is for a self-referential relationship that must not be circular (a directed acyclic graph). For example, a Person class may have a parent property that is a many-to-one relationship with another Person instance. However a given Person cannot be his own parent or ancestor.

We're using just such a relationship for trees of pages that inherit certain characteristics from their ancestors. In order to validate that our users aren't setting up circular references I implemented the following constraint:Then I just needed to register my new constraint by adding ConstrainedProperty.registerNewConstraint(AcyclicConstraint.NAME, AcyclicConstraint) to grails-app/conf/spring/resources.groovy. Using the constraint is as simple as this:
class Person {

    String name
    Person parent

    static constraints = {
        parent acyclic: true
    }
}
The constraint can be mixed with others such as nullable: true. The really nice thing is that the constraint implementation doesn't reference any of my domain classes directly, meaning it can be re-used in any other domain class.

When you're using similar validation logic in multiple classes defining a constraint like this is a much better option than using a validator closure and like many things in Grails it turns out to be pretty easy to implement.

25 comments:

johnrellis said...

awesome! this constraint should definitely be added to the grails source, i reckon it is one that is enforced in all wacky ways in other projects. Adding a constraint looks nice and easy!

dustmite said...

Nice work! Custom constraints in Grails are very powerful. Wonder if this could be extended to look beyond direct relationship and make sure that someone isn't say their own great great grand child or such.

Rob said...

@dustmite it's already doing that

dustmite said...

@Rob Sorry, I noticed that after posting my comment. Really great stuff, took it for a spin last night and works like a charm.

As @johnrellis said this should be included with the Grails source.

Rick Jensen said...

Please, as others have said, get this included in Grails core. It's a sufficiently complex validation that is easy to mess up and is also very common.

cerri bell said...

Jadwal Bola | Permainan Bola | Judi Bola | Taruhan Bola | Agen Bola | Agen Sbobet | Jasa SEO Profesional | Jasa SEO | Harga Jasa SEO Profesional | Jasa SEO Murah | Pakar SEO

Anonymous said...

338a | agen bola | agen sbobet | bola tangkas | casino sbobet | liga champion | prediksi bola | cool mobile gagdet

Anonymous said...

sbobet | ibcbet | agen bola | agen bola Terpercaya | Jasa SEO Profesional | Jasa SEO | Harga Jasa SEO Profesional | Jasa SEO Murah | Pakar SEO

Metamorfosis said...

Pelatihan SDM is network marketing and training information or training an employee who has worked with many consulting firms and training institutions.
pelatihan hrd,
pelatihan pengembangan diri,
pendidikan dan pelatihan sdm,
pelatihan akuntansi keuangan,
pelatihan sales marketing,
pelatihan kehumasan,
pelatihan sdm jogja,
lembaga pelatihan sdm
pelatihan manajemen bisnis,
pelatihan administrasi perkantoran,
lembaga training motivasi,
pelatihan sekretaris,
pelatihan karyawan,
lembaga training di jogja,
pelatihan in house training,
pelatihan public training,
jasa konsultan,
pelatihan pengembangan sdm,
pelatihan manajemen sdm,
program training karyawan,
diklat sdm,
pelatihan sdm untuk rumah sakit,
_____
id card murah,
harga id card,
cetak kartu santri,
produksi id card,
bikin id card,
cetak id card jogja,
id card murah jogja,
cetak id card,
percetakan id card,
pabrik id card,
cetak member card,
cetak kartu pelajar,
cetak kartu siswa,

ssamira Butik said...

I like your stuff, classic right, Now... everything with digital, perdita di peso thanks for remind me for this memories
water heater gainsborough
water heater ariston
water heater rinnai
water heater delizia
water heater wika

Metamorfosis said...

Kursus Arab Pare is a place for all of you who want to study and able to speak arabic. Come and join us..

Tempat Kursus Bahasa Arab

شركات كشف تسربات المياه وعزل اسطح وخزانات said...

شركة تنظيف شقق بجازان
شركة نقل اثاث بجازان
شركة كشف تسربات المياه بجازان
شركة مكافحة حشرات بحائل
تنظيف منازل بحائل
نقل اثاث بحائل
كشف تسربات المياه بحائل

abo-bder said...


شركة شحن عفش من الرياض الي الامارات-دبي
شركة نقل اثاث من الرياض الى الامارات

شركة نقل اثاث من الرياض الى الامارات


Assignment Help said...

I loved the article, keep updating interesting articles. I will be a regular reader… I am offering assignment help to students over the globe at a low price.
Course Mentor
Assignment help
Homework Help
Coursework Help
Become Mentors
how to be a mentor
how to become a mentor
Assignment help
find a mentor
how to get a mentor
how to find a mentor
Coursework Help
Coursework Writing Service
coursework writing help
Homework Help
Help With Homework
Homework Help Service
Assignment help NZ
Assignment help UK
Assignment help US
Assignment help Australia
Assignment help Canada
Assignment help AU

Assignment Help said...

Thanks for sharing this information. I have shared this link with other keep posting such information to provide best in class law assignment help online at very affordable prices.

Assignment help
Assignment help
Assignment help
Python Programming Assignment
Programming Assignment Experts
Assignment help tutors
Assignment help

Assignment Help said...

I loved the article, keep updating interesting articles.and we serve best food according to our customers' reviews we have a specious hall for marriages & special arrangement for kitty parties and other parties. You can visit us at any time we would love to serve you.

assignment help
assignment help
online assignment help
assignment help online
assignment help australia
do my assignment
college assignment help
university assignment help
statistics assignment help
finance assignment help

Assignment Help said...

I love the article keep updating article. And we serve best food according to our customer’s review we have a specious hall for marriage & special arrangement for kitty parties and other parties. You can visit us at any time we would love to serve you.
Assignment help Australia
Australian Assignment Help
Assignment writing service Australia
Online assignment help australia
Assignment Help Brisbane
Assignment help Sydney
Assignment Help Adelaide
Assignment Help Perth
Assignment Help Melbourne
Assignment Help Canbera
Essay Writing Service australia
Nursing Assignment Help Australia
University Assignment Help Australia
Write My Essay Australia
Cheap Essay Writing Service Australia
Accounting Assignment Help Australia

Assignment Help said...

I found this one pretty fascinating and it should go into my collection. Very good work! I am Impressed. We appreciate that please keep going to write more content. We are the assignment helper, we provide services all over the globe. We are best in these:- services
dm beginners
dm beginner
digital marketing for website
digital marketing for beginner
digital marketing beginner
learn digital marketing
learn digital marketing free online
learn digital marketing free
learn seo
how to learn affiliate marketing
dm beginners
dm beginner
digital marketing for website
digital marketing for beginner
digital marketing beginner
learn digital marketing
learn digital marketing free online
learn digital marketing free
learn seo
how to learn affiliate marketing

Assignment Help said...


Many of the people are depressed about the problems of essay writing. Well, don’t worry about that because we are providing this service at a very reasonable price.

Statistics Assignment Help
Statistics Homework Help
statistics help
statistics homework
statistics homework helper
SPSS Help
SPSS Tutors
SPSS Homework Help
matlab homework help
matlab assignment help
help with matlab
stata homework help
probability assignment
probability assignment help
R Programming homework help
Bio Statistics Assignment Help
R Programming Help
R Programming homework help
business statistics assignment help
business statistics homework help

Assignment Help said...

I loved the article, keep updating interesting articles.and we serve best food according to our customers' reviews we have a specious hall for marriages & special arrangement for kitty parties and other parties. You can visit us at any time we would love to serve you.

Article Assignment Help
Essay Assignment Helper
Essay writing
Essay writing service
Dissertation help
Thesis writing help
Write My Essay
Do My Essay
Hire Cheap Essay Writer
College Essay Help

Assignment Help said...

I love the article keep updating article. And we serve best food according to our customer’s review we have a specious hall for marriage & special arrangement for kitty parties and other parties. You can visit us at any time we would love to serve you.
" Essay writing
"
Essay Writer
seo writing service
Essay writing service
Essay writing help
Write My Essay
hire seo writer
hire writer
Write my essay cheap
hire article writer

Assignment Help said...

I love the article keep updating article. And we serve best food according to our customer’s review we have a specious hall for marriage & special arrangement for kitty parties and other parties. You can visit us at any time we would love to serve you.
Assignment Help
Assignment Helper
Essay writing
Essay writing service
Dissertation help
Thesis writing help
Write My Essay
Computer Science Assignment Help
Assignment Help South Africa
Assignment Writing Service

Assignment Help said...

I loved the article, keep updating interesting articles. I will be a regular reader… I am offering assignment help to students over the globe at a low price.
verdant marriage palace
verdant Resort
marriage palace in Pehowa
Wedding Venue in Pehowa
Banquet Hall in Pehowa
Resort in Pehowa
Best Resorts near Pehowa
Best marriage palace in Pehowa
Best Family Restaurant in Pehowa
Best Veg Restaurant in Pehowa
Best Non-Veg Restaurant in Pehowa
Chawlas 2

james cook said...

It is nice to read such high-quality content. It is a good article that discusses the topic at hand quite well. I am looking forward to read more articles from your site. Keep up the good work.
assignment help uk

Hailie Hills said...

At what type of exam questions I need to pay attention more? I'm sure that at least once every student asks him/herself this question. I can recommend you to pay attention to the facts, people names, differences, and similarities. Those things will help you to recover information which you prepared before.