I have two DAO classes tied with OneToMany relationship as represented in code fragment below.
@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 is as below --
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);
}
When we try to insert the child for the very first time, every things works fine and we do get entry of parent and child in our DB tables. Child entries also get populated with parent's PK.
Problem arises when we try to update or delete already existed entries. It is not updating existing results and throwing exception after calling 'saveOrUpdateParentIdInChild'
, which is as below -
[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.
It seems like in 'where' clause in repository class has been taken as byte array while it suppose to be bigint. As suggested in hint, i have tried to cast ?2 to bigint ( as shown in (2) and (3) comment) but i was thrown another exception that bytearray can not be cast to bigint.
I am stuck in this problem for fairly a day and half. Still no idea how to way around to this problem. Please let me know what i am doing wrong in here. Why update/delete is failing.
Edit:
I have also gone through following SO posts before posting my question: