我有两个与OneToMany关系绑定的DAO类,如下面的代码片段所示。
@Entity
@Table ( name = “Parent_Result”)
public class Parent {
@Id
@GeneratedValue
@Column
private Long Id;
@Column
private String value;
@OneToMany(fetch = FetchType.EAGER, mappedBy = “parentResult”, targetEntity = Child.class,
cascade = Cascadetype.ALL)
private set<Child> childSet;
}
@Entity
@Table ( name = “Child_Result”)
public class Child {
@Id
@Column
@GeneratedValue
private Long id;
@Column
private String result;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinColumn(name = “parent_id”, referencedColumnName = “id”)
private Parent parentResult;
}
public class ParentDal {
@Autowired
ParentRepository _parentRepo;
@Autowired
ChildRepository _childRepo;
@transactional ( readOnly =false, propogation = Propogation.REQUIRED)
public void saveResults( Parent parentResult) {
_parentRepo.saveAndFlush(parentResult);
for(Child childResult : parentResult.getChildSet() ) {
// Save parent PK in child. A requirement.
_childRepo.saveOrUpdateParentIdInChild(parentResult.getId(),
childResult.getId());
}
}
}
ChildRepository.java如下 -
public interface ChildRepository extends JpaRepository<Child, Long> {
@Modifying
@Transactional
@Query(value = "update Child_Result set parent_id = ?1 where
id = ?2", nativeQuery = true)
// id = (CAST( (?2) AS bigint))", nativeQuery = true) //-------> (2)
// id = (?2::bigint)", nativeQuery = true) //--------> (3)
void saveOrUpdateParentIdInChild(Long parent_id, Long id);
}
当我们第一次尝试插入孩子时,每件事都很好,我们确实在DB表中输入了父和子。子条目也会填充父母的PK。
当我们尝试更新或删除已存在的条目时出现问题。调用'saveOrUpdateParentIdInChild'
后,它不会更新现有结果并抛出异常,如下所示 -
[WARN] [30 Sep 21:55:18] - org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 42883
[ERROR] [30 Sep 21:55:18] - org.hibernate.util.JDBCExceptionReporter - ERROR: operator does not exist: bigint = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
似乎在&#39;其中&#39;存储库类中的子句被视为字节数组,而它假设为bigint。正如提示中所建议的那样,我试图将?2转换为bigint(如(2)和(3)注释中所示)但是我抛出了另一个异常,即bytearray无法转换为bigint。
我被困在这个问题中相当一天半。仍然不知道如何解决这个问题。请让我知道我在这里做错了什么。为什么更新/删除失败。
编辑:
在发布我的问题之前,我还经历了以下SO帖子: