0

Problem: I have a Purchase Order table and Country table which primary key is a foreign key in my PO table. On the front end I have a form (angularJS) used for creation of a new PO with a bunch of fields and about 9 drop-down list fields form where user can select whatever info is required for the PO. For the sake of the example I will use the Country drop-down. (All drop-downs are getting populated from the DB).

The problem I have is with the persistence. After submitting the form, if I use the debugger and I check the Purchase Order object that is being passed to the service all my drop downs objects are null, except the other normal input fields which belong to the PO table.

I think the problem is Angular, If I check the network tab in my chrome browser under developer tools I see the country being passed as countryData:2. Thing is I am new to Angular so I am bit confused at this stage on how I should handle this as data is coming from the drop-down list and how I should return the country object. Here http://tinypic.com/r/2rd9pxl/8 I put a screenshot of my network tab from the browser which show the from Data that is being posted.

Populate the country drop-down list

<div class="control-group">
                      <label class="control-label">*Country</label>
                      <div class="input-append">
                          <div data-ng-init="getCountryDataFromServer()">
                              <b>Person Data:</b> <select id="countryData" ng-model="contact.countryData">
                              <option value="">-- Select Countries --</option>
                              <option data-ng-repeat="country in countries" value="{{country.countryId}}">{{country.countryname}}</option>
                          </select><br>
                          </div>
                      </div>
                      <div class="input-append">
                          <label>
                            <span class="alert alert-error"
                                  ng-show="displayValidationError && newContactForm.countryData.$error.required">
                                <spring:message code="required"/>
                            </span>
                          </label>
                      </div>
                  </div>

Model

@Entity
@Table(name = "PURCHASEORDER",schema = "POTOOL")
@Audited
public class PurchaseOrder {

@Id
@GeneratedValue
private int id;

private String tower;
private Double revenue;
//other properties of the PO

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "country_id")
private Country country;

//Other ManyToOne relationships
//Getter and setters

Controller

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> create(@ModelAttribute("contact") PurchaseOrder purchaseOrder,
                                @RequestParam(required = false) String searchFor,
                                @RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
                                Locale locale) {
    purchaseOrderServiceImpl.save(purchaseOrder);

    if (isSearchActivated(searchFor)) {
        return search(searchFor, page, locale, "message.create.success");
    }

    return createListAllResponse(page, locale, "message.create.success");
}

Service

public void save(PurchaseOrder purchaseOrder) {
    purchaseOrderRepository.save(purchaseOrder);
}

Repository

public interface PurchaseOrderRepository extends
        PagingAndSortingRepository<PurchaseOrder, Integer> {


}
user2342259
  • 345
  • 2
  • 9
  • 27

2 Answers2

1

I don't think there is any issue because of angular since you have defined ng-model="contact.countryData" for dropdown and option has value from value="{{country.countryId}}". When you submit value country.countryId is going to server which is absolutely fine. I think you should have conversion mechanism which will convert data from UI to Hibernate Entity(i.e. PurchaseOrder). Generally DTO(Data transfer Object) is used to get data from UI and using setter and getter we set to Hibernate entity and latter you will save that entity. This question might help you.

DTO to Entity And Entity to DTO

Community
  • 1
  • 1
0

When you are working with HTML(not with JSP), it better to use @RequestBody at place of @ModelAttribute to get complete body of data. Please refer the following links: Link 1 Link 2

Community
  • 1
  • 1
Vish
  • 832
  • 7
  • 21