外键不以一对多关系保存在子表中

时间:2019-03-23 10:53:44

标签: java spring hibernate spring-boot

我正在使用Spring Boot开发REST API应用程序,并且在使用mappingBy属性时出现了“一对多”映射。 我有User类和Usermeata类,一个用户可以有多个usermeta。

虽然程序在使用JoinColumn时成功保存了外部变量,但我想知道在使用mapbyby时犯了什么错误。

这是我的代码:

用户实体类:


@Entity
@Table(name="User")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long userId;

    @Column(unique=true)
    private String username;

    @Column(unique=true)
    private String emailId; 

    private String password;
    private String firstName;
    private String lastName;
    private String phoneNo;
    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="user")
//  @JoinColumn(name="user_id")
    private List<UserMeta> userMetaList=new ArrayList();




    public List<UserMeta> getUserMetaList() {
        return userMetaList;
    }

    public void setUserMetaList(List<UserMeta> userMetaList) {
        this.userMetaList = userMetaList;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getPassword() {
        return password;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }


}

UserMeta类:


@Entity
public class UserMeta {

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

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

    private String _key;

    private String _value;

    public Long getUserMetaId() {
        return userMetaId;
    }

    public void setUserMetaId(Long userMetaId) {
        this.userMetaId = userMetaId;
    }

    public User getUser() {
        return user;
    }

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

    public String get_key() {
        return _key;
    }

    public void set_key(String _key) {
        this._key = _key;
    }

    public String get_value() {
        return _value;
    }

    public void set_value(String _value) {
        this._value = _value;
    }

}

UserService.class

@Service
public class UserService {

    @Autowired
    private UserRepository  userRepository;

    public User getUser(Long  userId) {
        return userRepository.getOne(userId);
    }

    public List<User> getAllUser() {
        return userRepository.findAll();
    }

    public User addUser(User user) {
        return userRepository.save(user);
    }

    public void removeUser(User user) {
        userRepository.delete(user);
    }

    public void updateUser(User user) {
        userRepository.save(user);
    }
}


POST请求:

{
    "username":"myusername",
    "emailId":"myemailid@gmail.com1",
    "password":"2323@123",
    "firstName":"myfname",
    "lastName":"mlname",
    "phoneNo":"000000",
    "userMetaList":[
        {
            "_key":"api_key",
            "_value":"api_key_value"
        },
        {
            "_key":"prop1",
            "_value":"val1"
        }
        ]
}

现在将创建表并使用值更新表,但是表usermeta中的user_id列始终为空。.

0 个答案:

没有答案