41583

Grails - How to implement a foreign key relationship not using an id column?

Question:

Have been trying to look for an answer for hours, but have so far not managed to come up with an adequate solution, so I'm hoping someone here might have some more experience with Grails, and implementing custom relationships in it.

My problem is that I have two classes:

Company<br /> OrderConfig

OrderConfig contains two references to Company. 1 for a consignee, 1 for a shipper. I implemented the relationship (see below), and everything looks dandy, with my order_config table containing both a consignee_company_id column and a shipper_company_id column.

However, I do not want or need a company_id column at all. I would prefer using the CompanyName as the identifying column. How can I prevent Grails from automatically adding the id column to the company table, and instead use the companyName column as the primary key, thus ensuring my order_config table will be generated containing the companyName and not the company_id?

I tried embedding the company in the orderconfig class, and wrestled with all kinds of other options including the mappings, however I ran into trouble with each and every one of them.

Thanks in advance!

My code so far:

class OrderConfig { static hasMany = [consignee:Company, shipper:Company] String toString() { return "${consignee}" } static constraints = { consignee (blank:false, maxSize:10) shipper (blank:false, maxSize:10) } Company consignee Company shipper } class Company { String toString() { return "${companyName}" } static constraints = { companyName(blank:false, maxSize:10) companyAddress1(blank:false, maxSize:40) companyAddress2(blank:false, maxSize:40) companyAddress3(blank:false, maxSize:40) companyAddress4(blank:false, maxSize:40) companyZipCode(blank:false, maxSize:36) companyCountry(blank:false, maxSize:36) } String companyName String companyAddress1 String companyAddress2 String companyAddress3 String companyAddress4 String companyZipCode String companyCountry }

Answer1:

You need to look at the advanced GORM config options in the documentation, in section 5.5.2.1

There is an example

class Person { String firstName static hasMany = [addresses:Address] static mapping = { table 'people' firstName column:'First_Name' addresses joinTable:[name:'Person_Addresses', key:'Person_Id', column:'Address_Id'] } }

there you can see how they are specifying the key and column name on which to do the stuff.

Edit -- I reread your question: if you are asking how to change it so Company has an assigned id, you might be able to do something like this example

static mapping = { id generator:'assigned', column:'column_name',name:'propertyName' }

where more options are available here: <a href="http://www.grails.org/doc/1.3.x/ref/Database%20Mapping/id.html" rel="nofollow">http://www.grails.org/doc/1.3.x/ref/Database%20Mapping/id.html</a>

Recommend

  • Dual axes line vs column and zoom tooltip broke
  • Alternatives of spread syntax
  • Chronos does not run job
  • CDI object not proxyable with injected constructor
  • Use java class in Graal.js
  • Objective-C operator (?) and (:)
  • String array in C#
  • can an android application check the version of the phone
  • Unexpected end of Stream, the content may have already been read by another component. Microsoft.Asp
  • MVC controller.Execute with areas
  • Swift 4 add gesture: override vs @objc
  • Counting bigrams real fast (with or without multiprocessing) - python
  • Can you call FB.login inside a callback from other FB methods (like FB.getLoginStatus) without trigg
  • How to find out what makes poi corrupt a xlsx / xlsm file
  • tooltip.js popper.js usage example
  • How to do handle login-logoff
  • Catch SqlException when Attempting NHibernate Transaction
  • spring-data-redis Jackson serialization
  • FormsAuthentication.CookieDomain property
  • using JQuery autocomplete from database with PHP (CodeIgniter)
  • Jenkins - could not read Username for 'https://github.com': No such device or address
  • .htaccess redirect domain.com to www.domain.com
  • Simultaneous animation when entering editing mode of UITableViewCell
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Fortran function variable length string return
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • Autocomplete source from project settings
  • 'Edit' function for forum posts and such
  • Calculate time from document
  • Unable to create Access token grant type in wso2 API manager store to test API
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • matrix multiplication apache pig
  • Functions by reference or by variable, which to use when?
  • multiple button click in asp.net MVC 3
  • Running R's aov() mixed effects model from Python using rpy2