Я пытаюсь обновить объект отношением OneToOne, используя спокойную веб-службу.Пользовательский запрос Значение параметра [2] не соответствует ожидаемому типу
Я использую пользовательский запрос, но он не работает
@Modifying
@Query("UPDATE Activity a SET a.type = :type WHERE a.id = :uuid")
int setType(@Param("uuid") UUID uuid, @Param("type") long type);
Ошибка:
java.lang.IllegalArgumentException: значение параметра [2] не соответствует ожидаемому типа [com.mezoo .tdc.model.ActivityType (п/а)]
активность Бин
@Entity
@Table(name= "Activity")
public class Activity implements Serializable{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy = "uuid2")
@Column(name = "uuid", nullable = false, unique = true, columnDefinition = "BINARY(16)")
private UUID uuid;
@OneToOne(fetch = FetchType.EAGER, cascade={CascadeType.MERGE})
@JoinColumn(name="type", nullable = false)
private ActivityType type;
ActivityType Bean
@Entity
@Table(name= "ActivityType")
public class ActivityType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false, unique = true)
private String code;
@Column(nullable = false)
private String label;
можно обновить без пользовательских запросов? Я хотел бы запрос POST использовать, как показано ниже:
{ "UUID": "9d9fa946-ee6e-408E-9e8a-7a9786a1d362", "ярлык": "деятельность", "тип": { "ID": 2}}
Update
активность Repository:
@Transactional
public interface ActivityDao extends CrudRepository<Activity, UUID> {
@Modifying
@Query("UPDATE Activity a SET a.type = :type WHERE a.uuid = :uuid")
int updateType(@Param("uuid") UUID uuid, @Param("type") ActivityType type);
}
Service активность:
@Service
public class ActivityService implements EntityService<Activity,UUID> {
private static final Logger LOGGER = LoggerFactory.getLogger(ActivityService.class);
@Override
public Activity update(Activity activity) {
LOGGER.info("Update ",activity);
Activity updated = activityDao.findOne(activity.getUuid());
if(updated==null) throw new ResourceNotFoundException();
if (activity.getType().getId()!= updated.getType().getId()) {
LOGGER.info("Update ActivityType for Activity "+activity.getUuid(),activity);
activityDao.updateType(updated.getUuid(),activity.getType());
}
updated.setLabel(activity.getLabel());
updated.setDetails(activity.getDetails());
return activityDao.save(updated);
}
Благодаря
a.type явно относится к типу ActivityType, поэтому вам необходимо передать в ActivityType. Запросы SQLNative - это единственный способ обойти это, но тогда ваш код станет грязным взломом. –