Lokeshwer
Lokeshwer

Reputation: 1139

How do I create association by joining non primary key column

class Contact {
String name
String number
}

class Message {
String text
String number   
Contact contactInfo //If any
}

I need to join on Message.number = Contact.number. Any thoughts on creating association in Grails/GORM with non primary key column?

Upvotes: 7

Views: 633

Answers (2)

sudhir
sudhir

Reputation: 381

Burt, this is possible with hibernate using the property-ref attribute

Upvotes: 4

Burt Beckwith
Burt Beckwith

Reputation: 75681

I'm pretty sure this isn't possible in GORM, and I don't know if it's even possible in regular Hibernate. But you can fake it:

class Message {
   String text
   String number

   static transients = ['contactInfo']

   Contact getContactInfo() {
      Contact.findByNumber(number)
   }
   void setContactInfo(Contact contact) {
      number = contact.number
   }
}

Upvotes: 5

Related Questions