Spring Boot Data Rest JPA:@ManyToOne没有填充DB中的ForeignKey列

时间:2017-10-08 23:49:13

标签: java spring rest spring-data spring-data-jpa

我在TomCat中使用带有JPA的Spring Boot Data Rest来保存MySQL中的数据。

  

问题:当添加用户的JSON发送到Rest端点时,两个MySQL表都会更新,但映射列(phone.user_id)中的值设置为'NULL '(应该设置为相应的User.uid)... 代码中缺少什么可以按预期工作?

生成的SQL表和值:

// "user" table                  // "phone" table
uid | first_name                 pid | number   | user_id
----------------                 ------------------------
1   | John                       1   | 03432453 | NULL    <--- should show 1
                                 2   | 02451254 | NULL    <--- should show 1

@OneToMany方面:

@Entity    
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long uid;
    private String firstName;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="user", orphanRemoval=true)  
    private List<Phone> phones;

    /* getters and setters */

    public void addPhone(Phone phone) {
        this.phones.add(phone);
        phone.setUser(this);
    }

    /* UPDATED - method added */
    public User updatePhoneUserId(User user) {
        List<Phone> phs = user.getPhones();
        for (Phone ph : phs ) {
            ph.setUser(user);
        }
        return user;
     }
 }

@ManyToOne方面:

@Entity
public class Phone {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long pid;

    private String number;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    private User user;

    /* getters and setters*/  

}

UserRepository和PhoneRepository:

public interface UserRepository extends JpaRepository<User, Long> { }
public interface PhoneRepository extends JpaRepository<Phone, Long> { }

休息控制器:

@RestController  
@RequestMapping(path="/api")
public class UserController {

    @Autowired 
    private UserRepository ur;

    @PostMapping(path="/add")
    public @ResponseBody String addNewUserPost(@RequestBody User user) {
        // ur.save(user);                       /* UPDATED - line removed */ 
        ur.save(user.updatePhoneUserId(user));  /* UPDATED - line added */

        return "Saved";
    }
}

1 个答案:

答案 0 :(得分:2)

将用户保存到数据库存储库模型后,返回具有更新值(如id。

)的用户对象

所以,请执行以下操作:

<div ng-repeat="question in questions | limitTo:10" ng-show="questions.length > 0">
        <div class="question-list">
            <h2><a href="{{question.link}}" title="{{question.title}}" target="_blank">{{question.title}}</a></h2>  
            <div class="details"><div class="owner"><div class="image"><a href="{{question.owner.profile_image}}" title="View {{question.owner.display_name}}" target="_blank"><img src=""/></a></div>
            <a class="name" href="{{question.owner.link}}" title="View {{question.owner.display_name}}" target="_blank">{{question.owner.display_name}}</a><span class="rep" title="Reputation Score"><span class="fa fa-star-o"></span> {{question.owner.reputation}}</span></div>
            <div class="created" title="Date asked"><span class="fa fa-calendar"></span>{{question.creation_date}}</div></div><div class="is-answered">{{question.answer_count}} answers<span class="triangle"></span></div>
        </div>     
</div>

因此,用户对象将被存储库返回的对象替换,并且id不再为null。