0

I have a request handler method "listAll" which returns all articles. When I invoke this method it returns Article list but When I access this return in angularjs side it does not contain User info forexample article.user.name does not return any info, but I can acces the other article fields. My entities, angularjs script and controller is below. Thanks for your helps

@Entity
public class Article {

    @Id
    @GeneratedValue
    private int id;
    private String title;
    private String summary;
    private String description;
    private String createdDate;

    @ManyToOne
    private User user;

    public Article(){

    }

    public Article(String title, String summary, String description, String createdDate, int id, User user) {
        super();
        this.title= title;
        this.summary= summary;
        this.description = description;
        this.createdDate = createdDate;
        this.user = user;
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }
    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public String getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    @JsonBackReference
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object object) {
        if (object instanceof Article){
            Article article = (Article) object;
            return article.id == id;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return id;
    }
}


@Entity
@Table(name = "system_user")
public class User {

    @Id
    @GeneratedValue
    private int id;

    private String email;
    private String name;
    private String enabled;
    private String password;

    @Enumerated(EnumType.STRING)
    @Column(name = "user_role")
    private Role role;

    public int getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEnabled() {
        return enabled;
    }

    public void setEnabled(String enabled) {
        this.enabled = enabled;
    }
}

Request Handler method:

  @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<?> listAll(@RequestParam int page, Locale locale) {
        return createListAllResponse(page, locale);
    }

Angularjs script:

it returns "data.articles[i].user" is undefined in following script:

$scope.populateTable = function (data) {
        if (data.pagesCount > 0) {
            $scope.state = 'list';
            $scope.page = {source: data.articles, currentPage: $scope.pageToGet, pagesCount: data.pagesCount, totalArticles : data.totalArticles};
            alert("data.articles.length: " + data.articles.length)
            for (var i = 0; i < data.articles.length; i++) {
                alert("data.articles ["+i+"]: "+ data.articles[i].user.name);
            }

            if($scope.page.pagesCount <= $scope.page.currentPage){
                $scope.pageToGet = $scope.page.pagesCount - 1;
                $scope.page.currentPage = $scope.page.pagesCount - 1;
            }

            $scope.displayCreateArticleButton = true;
            $scope.displaySearchButton = true;
        } else {
            $scope.state = 'noresult';
            $scope.displayCreateArticleButton = true;

            if(!$scope.searchFor){
                $scope.displaySearchButton = false;
            }
        }

        if (data.actionMessage || data.searchMessage) {
            $scope.displayMessageToUser = $scope.lastAction != 'search';

            $scope.page.actionMessage = data.actionMessage;
            $scope.page.searchMessage = data.searchMessage;
        } else {
            $scope.displayMessageToUser = false;
        }
    }

2 Answers 2

1

As you not provided us with DB schema, I have two assumptions:

  1. First of all, make sure that the Article.User field is not null at the server side. Set the breakpoint at the following line:

    return createListAllResponse(page, locale);

    Once the breakpoint is reached, press Alt+F8 (in IDEA) in order to see the value of the response.

  2. Have you tried adding @JoinColumn?

@ManyToOne
@JoinColumn(name="USER_ID")
private User user;
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Artem Larin,

I can access user informations in server side but when Response entity sent to angularjs side, object does not contains user information.

I solved this situation with ArticleDTO object which contains String userName aganist User object. Thanks for your helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.