Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:27:07+00:00 2026-06-05T11:27:07+00:00

i create two domain, Author and Book, the Author has many Book, and the

  • 0

i create two domain, Author and Book, the Author has many Book, and the Book is belongsTo Author.

class Author {
     hasMany = [ books : Book ]
     String name
}

class Book {
    String title
    Author author
}

this “savebook” function in BookController.groovy

   def savebook={

        def json = request.JSON
        def bookInstance = new Book()
        bookInstance.properties = json

        bookInstance.author_id = json.author_id
        bookInstance.title = json.title         

        if (bookInstance.validate()) {
            bookInstance.save();
            def rep = [ respence: "1" ] // save
            render rep as JSON
        }

        else {
            def rep = [ respence: "0" ] // not save
            render rep as JSON
        }

    }

I have not found a solution for the line : bookInstance.author_id = json.author_id in “savebook” to create a book by an author.
My second question is how should be the structure of the JSON object to make a REST POST ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:27:08+00:00Added an answer on June 5, 2026 at 11:27 am

    For the first part of your question you need to look the author up by their id and assign that instance to the author variable in Book:

    def author = Author.get(json.author_id)
    bookInstance.author = author
    bookInstance.title = json.title
    author.addToBooks(bookInstance)
    

    The structure of the JSON coming into the rest method can be any valid JSON. You could simply do

    {
        "author_id": 12345,
        "title" : "See Jane Run"
    }
    

    or you could get as complicated as you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two domain classes, Alpha and Beta. class Beta{ String betaName int age
I have two grails domain classes Class MultipleChoiceQuestion { String question static constraints =
I have two domain classes: class Domain1 { String val11 String val12 Domain2 domain2
I have a domain class which has two dates in it and I want
i create two input text fields , one for title and another for permanent
I am trying to create two different sets of settings for datepicker( http://keith-wood.name/datepick.html )
I'm currently trying to create a Friendship domain object to link two User objects
My problem is this: I want to create a grails domain instance, defining the
Say I have two domain objects and a mapper interface. class Person { int
If my domain object should contain string properties in 2 languages, should I create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.