0

I have two models related by a OneToMany Relationship.

@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;

}

When I call the controller method to retrieve the group chat data from the postgres DB I receive this error:

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)]

The data is successfully added to the DB as I can see it in the database. I notice that it retrieves the group chat data from the DB successfully but I'm not sure why it doesn't return the object?

Controller:

@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 repo:

public interface GroupChatRepository extends JpaRepository<GroupChat, Long> {

    GroupChat findByUniqueId(UUID uniqueId);
}
MSmith
  • 39
  • 1
  • 7

1 Answers1

0

What type are you using for the column uniqueId in the db ?

Provided you are using the UUID type -> https://www.postgresql.org/docs/current/datatype-uuid.html

you should take a look at the hypersistence-utils-hibernate-52 library, include it in your project (if it's not included already) and try to add the @Type(type="pg-uuid") to your column attribute in the entity class.

There is also a good article and also a similar post describing the solution to this kind of a problem

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
m1k1
  • 3
  • 1
  • 2
  • Thank you for your reply. That is the type I am using. I have tried using ```@Type(type="pg-uuid")``` but it appears to be depreacted. Solutions in this post (https://stackoverflow.com/questions/4495233/postgresql-uuid-supported-by-hibernate) haven't worked. I don't think the UUID is the problem as I am able to print the object after calling it with the controller and I can see the uuid value present. – MSmith Dec 04 '22 at 15:49