简体   繁体   English

Spring Boot Data Rest JPA:@ManyToOne未填充数据库中的ForeignKey列

[英]Spring Boot Data Rest JPA: @ManyToOne not populating the ForeignKey column in DB

I am using Spring Boot Data Rest with JPA in TomCat to persist data in MySQL. 我在TomCat中使用带有JPA的Spring Boot Data Rest将数据持久化在MySQL中。

The problem: When a JSON to add a User is sent to the Rest endpoint, both MySQL tables are updated, BUT the values in the mapping column (phone.user_id) are set to 'NULL' (should be set to the corresponding User.uid)... What is missing from the code to work as intended ? 问题:当将要添加用户的JSON发送到Rest端点时,两个MySQL表都将更新,但映射列(phone.user_id)中的值设置为“ NULL”(应​​设置为相应的用户)。 uid)... 代码无法按预期工作吗?

The resulting SQL tables and values: 产生的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

The @OneToMany side: @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;
     }
 }

The @ManyToOne side: @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*/  

}

The UserRepository and PhoneRepository: UserRepository和PhoneRepository:

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

The Rest controller: 休息控制器:

@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";
    }
}

After saving user to database repository model returns user object with updated values such as id. 将用户保存到数据库存储库模型后,返回具有更新值(例如id)的用户对象。

So just do the following: 因此,请执行以下操作:

user = ur.save(user);

So user object will be replaced with the object that repository returns and id is not null anymore. 因此,用户对象将替换为存储库返回的对象,并且id不再为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM