【问题标题】:Custom query Parameter value [2] did not match expected type自定义查询参数值 [2] 与预期类型不匹配
【发布时间】:2015-11-19 06:22:41
【问题描述】:

我尝试使用 Restful Web 服务更新具有 OneToOne 关系的实体。

我正在使用自定义查询,但它不起作用

@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 (n/a)]

活动豆

@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","label":"ACTIVITY", "type":{"id":2}}

更新

活动存储库:

@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
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 查询是解决这个问题的唯一方法,但是你的代码变成了非 OO 的脏黑客。

标签: spring jpa


【解决方案1】:

您的参数@Param("type") long type 错误! 应该是@Param("type") ActivityType type

【讨论】:

  • 对于你的第二个问题:我认为这是可能的。您使用什么库将 json 转换为 java ?杰克逊? ...在转换您的 json 时,您会得到一个 Activity 对象,只需保存它,就这么简单...没有?
  • 感谢您的回答普拉斯。我做了,但现在出现了一个新错误 > javax.persistence.TransactionRequiredException: Executing an update/delete query
  • 欢迎您 Mezoo,您可以发布您访问存储库的服务吗?和存储库本身......你在某处缺少@Transactional
  • 完成了,我已经更新了帖子。之前存在事务注释
  • 好的......那么你的配置中有 对吗? ...现在确保您使用:org.springframework.transaction.annotation.Transactional 注释而不是 javax.transaction.Transactional
猜你喜欢
  • 2016-04-22
  • 2021-08-05
  • 2016-07-13
  • 1970-01-01
  • 2018-02-20
  • 2018-09-02
  • 2020-11-29
  • 2015-12-13
  • 2016-09-18
相关资源
最近更新 更多