I am having this problem with a model entity mapped to an OpenEdge Progress database that I can't modify. It has 3 different ID columns that basically make up a primary key.
I came up with a workaround so I could at least get the data, even if it's not very pretty in my opinion.
In my Repository class, which extends JpaRepository<MyEntity, Long>
, I added a new method which returns List<Object[]>
. So for example:
@Query(
value="SELECT * FROM pub.\"mytable\" WHERE \"id-one\" = ?1 AND \"id-two\" = ?2 AND \"id-three\" is null",
nativeQuery=true
)
List<Object[]> getRecordsAsObject(Long aIdOne, Long aIdTwo);
Then in my service class I manually create the model record by extracting the data from the object array.
getRecordsAsObject(myIdOne, myIdTwo).forEach(aRecord -> {
MyEntity myEntity = new MyEntity();
myEntity.setIdOne((Long) aRecord[0]);
myEntity.setIdTwo((Long) aRecord[1]);
myEntity.setOtherValue((String) aRecord[2]);
process(myEntity);
});
This at least helps me with extracting data from the table. I don't know that it would do anything in terms of further persisting/saving/updating the record in the table.