Я использую Spring JPA и хочу установить значение столбца внешнего ключа. Вот мои сущности и репозиторий.
@Entity
public class Device {
@NotEmpty
@Id
private String deviceId;
@ManyToOne
@JoinColumn(name="userId", referencedColumnName="userId", insertable=false, updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
private User user;
//Getters and setters
}
@Entity
public class User(){
@Id
private String userId;
private String userName;
//Getters and setters
}
public interface DeviceRepository extends PagingAndSortingRepository {
}
public class DeviceServiceImpl implements DeviceService {
@Autowired
private DeviceRepository devRepos;
@Autowired
private UserRepository userRepos;
@Override
public void saveDevice(Device device, String userId) {
User user = null;
if (userId!=null) {
user = userRepos.findOne(userid);
device.setUser(user);
}
deviceRepos.save(device);
}
}
Пользователь существует в таблице устройств, но столбец userId в таблице не устанавливает значение. Пожалуйста, помогите мне решить проблему.
РЕДАКТИРОВАТЬ: я удалил вставляемый и обновляемый из аннотации, и теперь он работает. @JoinColumn (name = "userId", referencedColumnName = "userId")
Тогда это означает, что я должен получать пользователя устройства из таблицы User всякий раз, когда я сохраняю устройство?