Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate object with same id, despite ordered by id and added @Key annotation. #757

Open
GlenGGG opened this issue Mar 15, 2021 · 0 comments

Comments

@GlenGGG
Copy link

GlenGGG commented Mar 15, 2021

I am using Sql2o 1.6.0 with sql2o-postgres:1.6.0 with sfm-sql2o-jre9:8.2.3.

I have two POJOs, they are:

public class Post {
  @Key
  private String id;  // This is generated from application side using UUID.randomUUID.toString
  private List<Image> images;
}

public class Image {
  @Key
  private String id;
  private String postId;
  private String url;
}

When I try to read post from a PostgreSQL database using sql2o and sfm, I get duplicate posts with the same id using the following code:

    try(Connection conn = sql2o.open())
    {
      Query query = conn.createQuery(
          "SELECT post.*," +
          "image.id as images_id," +
          "image.post_id as images_post_id," +
          "image.url as images_url " +
          "FROM post " +
          "LEFT JOIN image on image.post_id=post.id " +
          "ORDER BY post.id;");
      query.setAutoDeriveColumnNames(true);
      query.setResultSetHandlerFactoryBuilder(new SfmResultSetHandlerFactoryBuilder(JdbcMapperFactory.newInstance().addKeys("id", "images_id")));
      List<Post> posts = query.executeAndFetch(Post.class);
}

From the resulting posts I can see SimpleFlatMapper seems to recognize each row of the resulting set as a separate object. Each post in the resulting list has only one image in images, which corresponds exactly to each row in the resulting joint table. I have ordered the post id and added @key to keys and added keys to the factory, I don't know what went wrong. Is it something to do with my ids, because they are strings.

Sorry if this is a duplicate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant