0

This is what we have in the interface:

enter image description here

I pasted the parts of the code what I thought that are relevant, but maybe something more is required.

How it works

When the button is pushed, the userController.js save method is invoked. In the controller there is a $resource and the $save method is "connected" with the create method in UserController.java, and there is persisted.

The problem

In the interface I have three inputs (dd, mm, yy) and what I want to persist is a User with a java.time.LocalDate. How and where should I do the map/transformation of these three inputs to convert then in a LocalDate? Because obviously, the way the User is defined in the .js and the way is defined in .java are differents.

In the frontend

user.html

<div class="form-group">
                <label class="col-sm-2 control-label">Date of Birth</label>
                <div class="col-sm-10">
                    <div class="form-inline">
                        <div class="form-group">
                            <label class="sr-only" for="txt_day">Enter Day</label>
                            <div class="col-sm-2">
                                <input type="text" id="txt_day" ng-model="user.birthdate.day" class="form-control" placeholder="DD" required maxlength="2"
                                    data-validation-required-message="Day is required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="txt_month">Enter Month</label>
                            <div class="col-sm-2">
                                <input type="text" id="txt_month" ng-model="user.birthdate.month" class="form-control" placeholder="MM" required
                                    maxlength="2" data-validation-required-message="Month is required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="txt_year">Enter Year</label>
                            <div class="col-sm-2 ">
                                <input type="text" id="txt_year" ng-model="user.birthdate.year" class="form-control" placeholder="YY" required
                                    maxlength="4" data-validation-required-message="Year is required">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

userController.js

$scope.user = new UserService();

    $scope.save = function() {
        $scope.user.$save(function() {
            $location.path('/');
        });
    };

UserService.js

return $resource('rest/user/:action', {},....

In the backend

UserController.java

@POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public User create(User user) {
        LOGGER.info("create(): " + user);

        return this.userDao.save(user);;
    }

Entity

@Column(nullable = false)
    @Convert(converter = LocalDatePersistenceConverter.class)
    private LocalDate birthDate;
Iker Aguayo
  • 3,980
  • 4
  • 37
  • 49

1 Answers1

0

Well, honestly I see that your application is exposing your domain entities to outside through rest service. I wouldn't suggest to do in order to ensure separation of concern principle. This issue you are now having is because of that. If adding a services/dto layers is a bit cucumber in your application, one workaround could be:

@Entity
public class User{
    @Column(nullable = false)
    @Convert(converter = LocalDatePersistenceConverter.class)
    private LocalDate birthDate;

    @Transient
    private birthDay
    @Transient
    private birthMonth
    @Transient
    private birth birthYear
    ..
    @PrePersist
    protected void prePersist()
    {
        birthDate = new LocalDate(birthDay, birthMonth, birthYear)
    }
}

So your entity gets populated from your javascript component and the jpa provider makes the tweaking creating a jodatime object. Hope this works