I am using JPA with Hibernate. I have an @Entity with a field whose data type I have set to Long. The corresponding field in the mysql database is int(11). When I try to retrieve this field, it comes up as Null. Am I missing something?
@Entity
@EqualsAndHashCode(of = {"test_id", "other_test_id"})
@Table(name = "test_table")
@Data
class Dummy{
@Id
@Column(name = "test_id", nullable = false)
private Long testId;
@Id
@Column(name = "other_test_id", nullable = false)
private Long otherTestId;
}
class DummyDao{
public Dummy findDummyByOtherTestId(Long otherTestId){
StringBuffer query = new StringBuffer();
query.append("SELECT * ");
query.append("FROM test_table tt WHERE ");
List<String> criteria = new ArrayList<>();
criteria.add("tt.other_test_id = :otherTestId");
query.append(criteria.get(0));
List<Dummy> results = em.createNativeQuery(query.toString(), Dummy.class).setParameter("otherTestId", otherTestId).getResultList();
return results.isEmpty() ? null : results.get(0);
}
}