当 Spring Data JPA 从数据库返回数据时,我遇到了问题。我在这里再试一次。下面是设置。
Spring V 1.5.15.RELEASE
PGV 9.5.4
查询语句:
CREATE TABLE dmg.gps_user_to_data_set (
group_id int8 NOT NULL,
data_set int4 NOT NULL,
agency_id int8 NOT NULL,
FOREIGN KEY (agency_id) REFERENCES funding_agency(agency_id),
FOREIGN KEY (group_id, data_set) REFERENCES data_set(group_id, data_set)
)
WITH (
OIDS=FALSE
) ;
回购:
public interface GpsUserToDataSetTestRepository extends JpaRepository<GpsUserToDataSetTest, DataSetPK> {
@Query(value = "select group_id,data_set,agency_id from gps_user_to_data_set where group_id=?1 and data_set=?2", nativeQuery = true)
List<GpsUserToDataSetTest> test(Long groupId, Long dataSet);
@Query(value = "select group_id,data_set,agency_id from gps_user_to_data_set where group_id=?1 and data_set=?2", nativeQuery = true)
List<Object> test2(Long groupId, Long dataSet);
}
实体:
@Entity
@Data
@Table(name = "GPS_USER_TO_DATA_SET")
public class GpsUserToDataSetTest implements Serializable {
@EmbeddedId
private DataSetPK primaryKey;
@Column(name = "AGENCY_ID")
private Long agencyId;
}
测试:
System.out.println("ID: "+dataSetPK);
System.out.println("YYY : ");
gpsUserToDataSetTestRepo.test(dataSetPK.getGroupId(), dataSetPK.getDataSetId()).stream().forEach(e -> {
try {
System.out.println(new ObjectMapper().writeValueAsString(e));
} catch(JsonProcessingException jpe) {
}
});
System.out.println("ZZZ: ");
gpsUserToDataSetTestRepo.test2(dataSetPK.getGroupId(), dataSetPK.getDataSetId()).stream().forEach(e -> {
try {
System.out.println(new ObjectMapper().writeValueAsString(e));
} catch(JsonProcessingException jpe) {
}
});
所以我运行它并得到两个不同的结果,我希望当接口 GpsUSerToDataSetTest 时封装的信息应该与我使用 java.lang.Object 运行相同的查询时完全相同,但是你可以看到 ids 不一样,下面是数据库和标准输出转储中的数据。
我什至不知道在这里说什么,这对我来说似乎很奇怪,并且可能是一个错误?
D B:
group_id |data_set |agency_id |
---------|---------|----------|
1356 |1 |2 |
1356 |1 |2 |
1356 |1 |19 |
白板的微信
相关分类