Link to home
Start Free TrialLog in
Avatar of Too Smart
Too Smart

asked on

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.
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

please repost the code here. We should not refer to questions posted on another site.
Avatar of Too Smart
Too Smart

ASKER

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.
The original question talks about owners and cars. The code talks about invoices.
Have you posted the right code?
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.
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
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);
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.
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.
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
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
You're welcome!