Table in sqlserver:
create table Imagetable(
Image_Name varchar(20) not null primary key,
Image_Data varbinary(max) not null
);
I have tried to do it like this:
EntityClass:
public class ImageImport {
private String name;
private FileInputStream ins;
//getter,setter
}
Mapping file:
<hibernate-mapping package="com.yp.entity">
<class name="ImageImport" table="Imagetable">
<id name="name" column="Image_Name" length="20">
<generator class="native"/>
</id>
<property name="ins" column="Image_Data"/>
</class>
</hibernate-mapping>
When app start, the error message:
org.hibernate.MappingException: Could not determine type for: java.io.FileInputStream, at table: Imagetable, for columns: [org.hibernate.mapping.Column(Image_Data)]
And I also tried to retrieve data from sql server.
Entity class:
public class ImageExport {
private String name;
private Blob image;
//getter,setter
}
Mapping file:
<hibernate-mapping package="com.yp.entity">
<class name="ImageExport" table="Imagetable">
<id name="name" column="Image_Name" length="20">
<generator class="native"/>
</id>
<property name="image" column="Image_Data"/>
</class>
The code of retrieving image:
1. ImageExport image= session.get(ImageExport.class, "first");
2. FileOutputStream outs = new FileOutputStream(new File(path));
3. outs.write(blob.getBytes(1, (int) blob.length()));
4. outs.close();
when execute to the third line, error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The TDS protocol stream is not valid
I have successfully done it by using pure jdbc methods with this tutorial