3
public static List<Image> getList(int userId) {
      Session session = HibernateUtil.openSession();
      Transaction tx = null;
      List<Image> results = null;
      try {
      tx = session.beginTransaction();

      Criteria crit = session.createCriteria(Image.class);
      crit.add(Restrictions.eq("userID",new Integer(userId)));
      results = crit.list();

      tx.commit();
      }
      catch (HibernateException e) {
          System.out.println("Problem in retrieving data from database");
             if (tx!=null) tx.rollback();
             e.printStackTrace(); 
          } finally {
             session.close(); 
          }

    return results;

  }

Image class

@Entity
@Table(name = "image")
public class Image {
    @Id
    @Column(name = "userId")
    int userID;
    @Id
    @Column(name = "name")
    String name;
    @Column(name = "size")
    double size;
    @Column(name = "preview")
    byte[] preview;

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getName() {
        return name;
    }

    public Image() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Image(int userID, String name, double size, byte[] preview) {
        super();
        this.userID = userID;
        this.name = name;
        this.size = size;
        this.preview = preview;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
    }

    public byte[] getPreview() {
        return preview;
    }

    public void setPreview(byte[] preview) {
        this.preview = preview;
    }

}

This is my database enter image description here

Image is an entity which has the following attributes : userId , name , size ,preview.

userId + Image name is the composite primary key I have fetched all rows which have userId =1 When I iterate over the list obtained . I get AAdhar (which is the first entry in the database)displayed 6 times instead of different names each time. I am not able to figure out the problem .

Solution : Edited Correct Image Class code create ImagePK class as mentioned

 @Entity
    @Table(name = "image")
    @IdClass(ImagePK.class)
    public class Image {
        @Id
        @Column(name = "userId")
        int userID;
        @Id
        @Column(name = "name")
        String name;
        @Column(name = "size")
        double size;
        @Column(name = "preview")
        byte[] preview;
    ……}
HMT
  • 2,093
  • 1
  • 19
  • 51
  • I am surprised that `Image` seems to have no primary key. Can you show the code for this entity? – Arnaud Denoyelle Aug 29 '18 at 10:24
  • user Id combined with image name is the primary key for this table – HMT Aug 29 '18 at 10:24
  • 1
    I suspect that the primary key is not well defined in the entity. It is possible that keys are well defined in the table (in SQL) but not in the entity (with Java annotations). That + some hibernate cache would explain this behavior. – Arnaud Denoyelle Aug 29 '18 at 10:27
  • @ArnaudDenoyelle can you suggest how can I specify primary key in this particular situation when my primary key is UserId + image name – HMT Aug 29 '18 at 10:31
  • I have added the Image class code . please review @ArnaudDenoyelle . Thanks – HMT Aug 29 '18 at 10:31

1 Answers1

3

I think the way you are creating composite primary key is incorrect. You can create composite primary key in hibernate like below -

public class ImagePK implements Serializable {
    protected Integer userID;
    protected String name;

    public ImagePK() {}

    public ImagePK(Integer userID, String name) {
        this.userID = userID;
        this.name = name;
    }
    // equals, hashCode
}

Use @IdClass annotation in your Image entity class

@Entity
@Table(name = "image")
@IdClass(ImagePK.class)
public class Image {
}
Derrick
  • 3,669
  • 5
  • 35
  • 50
  • When I create the session factory object , I need to create it for Image or ImagePK ? – HMT Aug 29 '18 at 10:53
  • Can you elaborate your query @heena – Derrick Aug 29 '18 at 10:55
  • SessionFactory = new Configuration().configure().addAnnotatedClass(Image.class).buildSessionFactory(); – HMT Aug 29 '18 at 10:55
  • In this statement , in the parenthesis of addAnnotatedClass , we need to specify ImagePK or Image – HMT Aug 29 '18 at 10:57
  • One more question . After I updated my code with the help of this code. I get this error "org.hibernate.AnnotationException: No identifier specified for entity: entities.Image" So I wanted to know how to specify Id in the Image class – HMT Aug 29 '18 at 11:01
  • Yes you have to add ImagePK.class – Derrick Aug 29 '18 at 11:02
  • 1
    do not remove @Id annotation form Image class – Derrick Aug 29 '18 at 11:04
  • I have edited the Image class code . You can view it above, Please review it amd tell me how to update the image class code so that I don't get org.hibernate.AnnotationException: No identifier specified for entity: entities.Image – HMT Aug 29 '18 at 11:05