web application with one to many relationship using spring data jpa, spring data rest, angular js, ng resource and ui router

I am facing problem in implementing one to many relationship in my web application using Spring Data JPA, Spring Data REST, Angular JS, ng-Resource and ui-router. Using Jhipster I have generated the application with all these frame works and is perfectly working fine for all basic crud operations for single entity. Now I am trying to form One to Many relationships (Owner having Multiple Cars), when I click a link in  owner page, I need to get all the cars related to that Owner.  Can any one help me this regard. For detail sample code reference, you can refer my code posted at  http://stackoverflow.com/questions/32830569/unable-to-get-one-to-many-owner-cars-link-from-jhipster-generated-entities. I am looking answer for this question.
Too SmartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
please repost the code here. We should not refer to questions posted on another site.
Too SmartAuthor Commented:
Hi, after lot of trails I cam e this so far and describing the problem where I stuck up and the code for that one to many relationship (invoice -> invoice details)

Angularjs code :

In InvoiceDetails.js   (ui router)

.state('invoiceDetails.byInvoiceId', {
                parent: 'entity',
                url: '/invoiceDetailss/byInvoiceId/:invoiceId',
                data: {
                    roles: ['ROLE_USER'],
                    pageTitle: 'Tracking Numbers'
                },
                views: {
                    'content@': {
                        templateUrl: 'scripts/app/entities/invoiceDetails/invoiceDetailss.html',
                        controller: 'InvoiceDetailsController'
                    }
                },
                resolve: {                  
                    entity: ['$stateParams', 'InvoiceDetailsGeneralService', function($stateParams, InvoiceDetailsGeneralService) {
                        return InvoiceDetailsGeneralService.findByInvoiceId({id : $stateParams.invoiceId});
                    }
                    
                    
                    ]
                }
            })

Open in new window


And Angularjs Service :

.factory('InvoiceDetailsGeneralService', function ($http) {
        return {           
            findByInvoiceId: function (invoice) {  
            	console.log("Inv " + invoice.id);  
                return $http.get('/api/invoiceDetailss/byInvoiceId', {params: {invoiceId: invoice.id }}).then(function (response) {
                    return response.data;
                });
            }
        };
    });

Open in new window


At this point my console.log show
Inv 1

Open in new window


Spring Data Jpa and REST is below :

InvoiceDetailsResource.java

 // Get Invoice Details (tracking numbers) by Invoice Id
    @RequestMapping(value = "/invoiceDetailss/byInvoiceId",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<InvoiceDetails> findByInvoiceId(@RequestParam(value="invoiceId") Long invoiceId) {
        log.debug("REST request to get Invoice Details By Invoice Id" + invoiceId);
        return invoiceDetailsService.findByInvoiceId(invoiceId);
    }

Open in new window


In InvoiceDetailsService.java

 
    @Inject
    private InvoiceDetailsRepository invoiceDetailsRepository;
    
    public List<InvoiceDetails> findByInvoiceId(Long invoiceId) {     

        return invoiceDetailsRepository.findByInvoiceId(invoiceId);
    }    

Open in new window


In InvoiceDetailsRepository.java

public interface InvoiceDetailsRepository extends JpaRepository<InvoiceDetails,Long> {

	List<InvoiceDetails> findByInvoiceId(Long invoiceId);

}

Open in new window


In InvoiceDetails.java,

  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    
    @Column(name = "service")
    private String service;
    
    @Column(name = "tracking_number")
    private String trackingNumber;
    
    @Column(name = "total_charges")
    private Double totalCharges;

    @ManyToOne
    private Invoice invoice;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getTrackingNumber() {
        return trackingNumber;
    }

    public void setTrackingNumber(String trackingNumber) {
        this.trackingNumber = trackingNumber;
    }

    public Double getTotalCharges() {
        return totalCharges;
    }

    public void setTotalCharges(Double totalCharges) {
        this.totalCharges = totalCharges;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

Open in new window


With this code, I am getting the following Error, in the response:
{"timestamp":1443978975829,"status":400,"error":"Bad Request","exception":"org.springframework.beans
.TypeMismatchException","message":"Failed to convert value of type 'java.lang.String' to required type
 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: \"byInvoiceId
\"","path":"/api/invoiceDetailss/byInvoiceId"}

Open in new window


Could you please help in fixing this, I could not able to figure our where I am going wrong, why response is not coming properly.
gurpsbassiCommented:
The original question talks about owners and cars. The code talks about invoices.
Have you posted the right code?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Too SmartAuthor Commented:
Yes. Essentially my problem is with one to many relationships. Earlier I tried with Owner -> Cars later on I deleted that code from system and tried with invoice to invoiceDetails. Problem is same, the only thing is I provided  another example.
gurpsbassiCommented:
The exception you are getting is not with one to many relationships.
It's way before your persistence layer. The problem is in your application / REST layer.

Can you supply the exact URL you are invoking. You can do this by bringing up a web debugger such as firebug / chrome console etc
gurpsbassiCommented:
are you sure its http://localhost:8080/api/invoiceDetailss/byInvoiceId?cacheBuster=1444014377012&invoiceId=2?

Your previous code sample indicates you are getting 'Inv 1' when doing :

console.log("Inv " + invoice.id);
Too SmartAuthor Commented:
Sorry this time, I tried for 2, when I click for 1, it gives 1 only in place of 2. Whatever request I have sent is correct only.
gurpsbassiCommented:
Sorry this time, I tried for 2, when I click for 1, it gives 1 only in place of 2. Whatever request I have sent is correct only.


Makes no sense to me.

Confirm what console.log is printing.
Too SmartAuthor Commented:
Sure, as I am in office I don't have access to my home system now. Evening after returning home around 8pm, I will post those details from the application result
mccarlIT Business Systems Analyst / Software DeveloperCommented:
In your InvoiceDetailsResource.java class what OTHER RequestMappings do you have defined? My guess is that the issue is due to a different mapping being defined as /invoiceDetailss/{invoiceId} and Spring is picking that one to process your request instead of the one that you are expecting.

If you change the first part of the path for the RequestMapping that you are trying to get working (and obviously the JavaScript code that calls the path) then I think it would just work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Too SmartAuthor Commented:
Yes you are right. In my trail and errors I end up with another same request mapping code with '/' at the end and it seems my request is going for that, after deleting it, now it is working fine. Thank you very much.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
REST

From novice to tech pro — start learning today.