# How to write mapping file and java entity class to store and retrieve images, binary files in SQL Server

**URL:** <https://discourse.hibernate.org/t/how-to-write-mapping-file-and-java-entity-class-to-store-and-retrieve-images-binary-files-in-sql-server/611>\
**Category:** Hibernate ORM\
**Created:** [April 26, 2018, 3:30am UTC](https://discourse.hibernate.org/t/how-to-write-mapping-file-and-java-entity-class-to-store-and-retrieve-images-binary-files-in-sql-server/611 "2018-04-26T03:30:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Letter375](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@Letter375](https://discourse.hibernate.org/u/Letter375)\
**Post date:** [April 26, 2018, 3:30am UTC](https://discourse.hibernate.org/t/how-to-write-mapping-file-and-java-entity-class-to-store-and-retrieve-images-binary-files-in-sql-server/611/1 "2018-04-26T03:30:22Z")

</div>

Table in sqlserver:

```auto
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:

```auto
public class ImageImport {
	private String name;
	private FileInputStream ins;	
	//getter,setter
}

```

Mapping file:

```auto
<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:

```auto
public class ImageExport {
	private String name;
	private Blob image;
	//getter,setter
}

```

Mapping file:

```auto
<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:

```auto
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](https://www.thecrazyprogrammer.com/2016/01/save-and-retrieve-image-from-mysql-database-using-java.html)

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [April 26, 2018, 8:01am UTC](https://discourse.hibernate.org/t/how-to-write-mapping-file-and-java-entity-class-to-store-and-retrieve-images-binary-files-in-sql-server/611/2 "2018-04-26T08:01:29Z")

</div>

Mapping the Blob property as `FileInputStream` is not a good idea since that field would only be consumed once and requires to be closed after opening it.

Therefore, as we explained in the [User Guide](http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#basic-lob), you need to use either `Blob` or `byte[]`.

```
@Lob
private byte[] image;

```

or with XML mappings:

```
<property name="image" column="Image_Data" type="blob"/>

```

or

```
<property name="image" column="Image_Data" type="binary"/>
```

---

<div class="post-metadata">

**Author:** ![Letter375](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@Letter375](https://discourse.hibernate.org/u/Letter375)\
**Post date:** [April 26, 2018, 2:04pm UTC](https://discourse.hibernate.org/t/how-to-write-mapping-file-and-java-entity-class-to-store-and-retrieve-images-binary-files-in-sql-server/611/4 "2018-04-26T14:04:06Z")

</div>

My problem solved, thanks a lot.
