1

I am trying to read an byte[] image from database.

This is the first of my two entities:

@Entity
public class ReceivingInspection implements Serializable {

    /**
     * The id.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="RECEIVING_INSPECTION_ID")
    private int id;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "receivingInspection")
    private List<ReceivingInspectionImage> receivingInspectionImages;

    // Constructors, getters, setters
}

And this is my second entity:

@Entity
public class ReceivingInspectionImage implements Serializable {

    @EJB
    private ImageService imageService;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="RECEIVING_INSPECTION_IMAGE_ID")
    private int id;

    @Lob @Basic(fetch = FetchType.LAZY)
    @Column(length=16777000)
    private byte[] image;

    @ManyToOne(fetch = FetchType.LAZY)
    private ReceivingInspection receivingInspection;

    public byte[] getImage() {
        return imageService.getImage(id);
    }

    // Constructors, getters, setters
}

This is my DAO:

@PersistenceContext
private EntityManager em;

public byte[] getImage(int id) {
    Query query = em
            .createNativeQuery("SELECT * FROM RECEIVINGINSPECTIONIMAGE WHERE RECEIVING_INSPECTION_IMAGE_ID = ?");
    query.setParameter(1, id);
    ReceivingInspectionImage image = (ReceivingInspectionImage) query.getSingleResult();
    return image.getImage();
}

When I am trying to deploy my application, I get a:

org.hibernate.MappingException: Could not determine type for: my.application.util.ImageService, at table: ReceivingInspectionImage, for columns: [org.hibernate.mapping.Column(imageService)]"}}

What could cause the problem? Is there a mistake in the way I am using Hibernate mapping?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
John
  • 735
  • 2
  • 14
  • 32
  • 2
    Exception literally says it doesn't recognize `imageService` field of the `ReceivingInspectionImage` entity as a valid database column. Why are you injecting an EJB in an Entity? – BalusC May 03 '16 at 11:35

1 Answers1

3

The problem lies here

@EJB
private ImageService imageService;

You should not inject service inside Entity, Hibernate will try to map it to a column.

DAO layer should not be called from Entity, create service to interact with DAO.

Ankur Singhal
  • 23,626
  • 10
  • 70
  • 108