我有两个通过OneToMany关系关联的模型。
@Entity
@Getter @Setter @NoArgsConstructor
@Table(name = "GroupChat")
public class GroupChat extends RepresentationModel<GroupChat> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "uniqueId", nullable = false, unique=true)
private UUID uniqueId;
@Column(name = "expirationDate")
private Date expirationDate;
@Column(name = "analysedDate")
private Date analysedDate;
@Column(name = "creationDate")
private Date creationDate;
@Column(name = "totalParticipants", nullable = false)
private int totalParticipants;
@Column(name = "totalCurrentParticipants", nullable = false)
private int totalCurrentParticipants;
@Column(name = "totalMessages", nullable = false)
private int totalMessages;
@Column(name = "totalSentVideos", nullable = false)
private int totalSentVideos;
@Column(name = "totalSentPhotos", nullable = false)
private int totalSentPhotos;
@Column(name = "totalSentGifs", nullable = false)
private int totalSentGifs;
@Column(name = "totalSentAudioFiles", nullable = false)
private int totalSentAudioFiles;
@Column(name = "totalSentReactions", nullable = false)
private int totalSentReactions;
@Column(name = "groupChatName")
private String groupChatName;
@Column(name = "participants")
@OneToMany(mappedBy="groupChatId", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
private List<MessengerUser> participants;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "MessengerUser")
public class MessengerUser extends RepresentationModel<MessengerUser> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "profilePic")
private int profilePic;
@Column(name = "numberOfMessagesSent")
private int numberOfMessagesSent;
@Column(name = "numberOfPhotosSent")
private int numberOfPhotosSent;
@Column(name = "numberOfVideosSent")
private int numberOfVideosSent;
@Column(name = "numberOfGifsSent")
private int numberOfGifsSent;
@Column(name = "numberOfAudioFilesSent")
private int numberOfAudioFilesSent;
@Column(name = "numberOfReceivedReactions")
private int numberOfReceivedReactions;
@Column(name = "numberOfSentReactions")
private int numberOfSentReactions;
@Column(name = "userReactionsReceived", columnDefinition="TEXT")
@Lob
private String userReactionsReceived;
@Column(name = "userReactionsSent", columnDefinition="TEXT")
@Lob
private String userReactionsSent;
@Column(name = "addedToChat", columnDefinition="TEXT")
@Lob
private String addedToChat;
@Column(name = "removedFromChat", columnDefinition="TEXT")
@Lob
private String removedFromChat;
@Column(name = "firstRecordOfActivity")
private Date firstRecordOfActivity;
@Column(name = "lastRecordOfActivity")
private Date lastRecordOfActivity;
@Column(name = "userActiveStatus")
private Boolean userActiveStatus;
@Column(name = "firstMessage")
private String firstMessage;
@Column(name = "firstMessageDate")
private Date firstMessageDate;
@Column(name = "message")
private int message;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="group_chat_id", nullable=false)
private GroupChat groupChatId;
}
当我调用控制器方法从postgres数据库中检索组聊天数据时,我收到这个错误:
Hibernate: select g1_0.id,g1_0.analysed_date,g1_0.creation_date,g1_0.expiration_date,g1_0.group_chat_name,g1_0.total_current_participants,g1_0.total_messages,g1_0.total_participants,g1_0.total_sent_audio_files,g1_0.total_sent_gifs,g1_0.total_sent_photos,g1_0.total_sent_reactions,g1_0.total_sent_videos,g1_0.unique_id from group_chat g1_0 where g1_0.unique_id=?
2022-12-04T14:06:41.751Z ERROR 16704 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: Argument [3] of type [java.lang.Long] did not match parameter type [com.example.demo.models.GroupChat (n/a)]] with root cause
java.lang.IllegalArgumentException: Argument [3] of type [java.lang.Long] did not match parameter type [com.example.demo.models.GroupChat (n/a)]
数据成功添加到DB中,正如我在数据库中看到的那样。我注意到它成功地从数据库检索组聊天数据,但我不知道为什么它不返回对象?
控制器:
@GetMapping("/{uniqueId}/chat")
public GroupChat getGroupChatByUniqueId(@PathVariable(value = "uniqueId") UUID uniqueId) {
GroupChat g = groupChatRepository.findByUniqueId(uniqueId);
Link selfLink = linkTo(methodOn(GroupChatController.class)
.getGroupChatByUniqueId(uniqueId)).withSelfRel();
g.add(selfLink);
if (messengerUserRepository.findByGroupChatId(g.getId()).size() > 0) {
Link ordersLink = linkTo(methodOn(MessengerUserController.class)
.getMessengerUsersByGroupChatId(g.getId(),uniqueId))
.withRel("allMessengerUsers");
g.add(ordersLink);
}
return g;
}
GroupChat回购:
public interface GroupChatRepository extends JpaRepository<GroupChat, Long> {
GroupChat findByUniqueId(UUID uniqueId);
}
您使用什么类型的列uniqueId
如果您使用的是UUID类型->https://www.postgresql.org/docs/current/datatype-uuid.html
您应该查看一下hypersistence-utils-hibernate-52库,将它包含在您的项目中(如果还没有包含它),并尝试将@Type(type="pg-uuid")
添加到实体类中的列属性中。
也有一篇很好的文章和一个类似的帖子描述了这种问题的解决方案