我正在尝试从数据库中读取byte[]
图像。
这是我的两个实体中的第一个:
@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
}
这是我的第二个实体:
@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
}
这是我的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();
}
当我尝试部署我的应用程序时,我得到了一个:
org.hibernate.MappingException:无法确定:my.application.util.ImageService的类型,在表:ReceivingInspectionImage,对于列:[org.hibernate.mapping.Column(imageService)]“}}
什么可能导致问题?我使用Hibernate映射的方式有错吗?
答案 0 :(得分:3)
问题出在这里
@EJB
private ImageService imageService;
您不应在Entity
内注入服务,Hibernate
会尝试将其映射到列。
DAO
调用 Entity
图层,创建与DAO
互动的服务。