2017-12-21 13 views
0

希望している特定の問題を解決するための助けを得ることができます。私は基本的に私のユーザ& UserProfileクラスと私の1対1の関係を表示する残りのAPIエンドポイントを作成しようとしています。注釈を使用してこれを実行しようとしていますが、私は運がありません。ユーザー情報と関連するプロファイル情報の両方を返すようには見えません。ここに私のクラスここ Java Spring Link 1対1のリレーションを関連付けます。

package com.account.service.model; 

import com.fasterxml.jackson.annotation.JsonBackReference; 
import com.fasterxml.jackson.annotation.JsonFormat; 
import com.fasterxml.jackson.annotation.JsonIgnore; 

import javax.persistence.*; 
import java.util.Date; 

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

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Integer id; 

    private String email; 

    private Boolean active; 

    @JsonIgnore 
    private String password; 

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") 
    private Date created_at; 

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") 
    private Date updated_at; 

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "user") 
    @JoinColumn(name = "user_id") 
    @JsonBackReference 
    private UserProfile userProfile; 

    public User(){} 

    public User(String email, Boolean active, String password, Date created_at, Date updated_at) { 
     this.email = email; 
     this.active = active; 
     this.password = password; 
     this.created_at = created_at; 
     this.updated_at = updated_at; 
    } 

    public Integer getId() { 
     return id; 
    } 

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

    public String getEmail() { 
     return email; 
    } 

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

    public Boolean getActive() { 
     return active; 
    } 

    public void setActive(Boolean active) { 
     this.active = active; 
    } 

    public String getPassword() { 
     return password; 
    } 

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

    public Date getCreated_at() { 
     return created_at; 
    } 

    public void setCreated_at(Date created_at) { 
     this.created_at = created_at; 
    } 

    public Date getUpdated_at() { 
     return updated_at; 
    } 

    public void setUpdated_at(Date updated_at) { 
     this.updated_at = updated_at; 
    } 

    public UserProfile getUserProfile() { 
     return userProfile; 
    } 

    public void setUserProfile(UserProfile userProfile) { 
     this.userProfile = userProfile; 
    } 
} 

とは、私が得る応答は、プロファイルの関連付けを不足している次のとおりであるユーザープロファイル

package com.account.service.model; 

import com.fasterxml.jackson.annotation.JsonManagedReference; 
import javax.persistence.*; 

@Entity 
@Table(name = "user_profiles") 
public class UserProfile { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Integer id; 

    private String first_name; 

    private String last_name; 

    @OneToOne(cascade = CascadeType.ALL, optional = false) 
    @JsonManagedReference 
    private User user; 

    public UserProfile(){} 

    public UserProfile(String first_name, String last_name, User user) { 
     this.first_name = first_name; 
     this.last_name = last_name; 
     this.user = user; 
    } 

    public Integer getId() { 
     return id; 
    } 

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

    public String getFirst_name() { 
     return first_name; 
    } 

    public void setFirst_name(String first_name) { 
     this.first_name = first_name; 
    } 

    public String getLast_name() { 
     return last_name; 
    } 

    public void setLast_name(String last_name) { 
     this.last_name = last_name; 
    } 

    public User getUser() { 
     return user; 
    } 

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

されています。

[ 
    { 
     "id": 1, 
     "email": "[email protected]", 
     "active": true, 
     "created_at": "2017-12-21", 
     "updated_at": "2017-12-21" 
    } 
] 

問題の可能性についての考えはありますか?

意図は、次のように表示することです:誰がこのような問題に貼付されている場合

[ 
    { 
     "id": 1, 
     "email": "[email protected]", 
     "active": true, 
     "created_at": "2017-12-21", 
     "updated_at": "2017-12-21", 
     "profile": { 
      "id": 1, 
      "first_name": "master", 
      "last_name": "splinter" 
     } 
    } 
] 
+0

Userクラスでは、 '@JoinColumn(name =" user_id ")'を使用します。ここで、UserProfileクラスでこの列を定義していますか? –

+0

ユーザープロファイルテーブルには、対応するuser_idがあります。これは、ユーザーテーブルのfkです。私はこれをプロファイルクラス内にリストアップしていません。 – prola

+0

FETCH EAGERパラメータが不足している可能性がありますか? –

答えて

0

@JsonBackReferenceで見てください、これがないあなたにあなたのUserクラスに使用されるべきである、それを考え出しましたプロファイルクラス。

関連する問題