This has already been answered but to help with the wider question;
I have an entity;
@Entity
public class BlobEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "NAME")
private String name;
@Lob
@Column(name="DATA", length=100000)
private byte[] data;
A JPA repository
@Repository
public interface BlobEntityRepository extends CrudRepository<BlobEntity, Long> {
}
And a test that reads a word doc and retrieves it from the database
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class BlobEntitytRepositoryTest extends AbstractTest {
@Autowired
private BlobEntityRepository repository;
@Test
@Transactional
public void test1() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("/HelloGreg.docx");
byte[] byteArray = IOUtils.toByteArray(inputStream);
BlobEntity blobEntity = new BlobEntity();
blobEntity.setName("test");
blobEntity.setData(byteArray);
repository.save(blobEntity);
assertEquals(1, repository.count());
BlobEntity entity = repository.findOne(1l);
assertNotNull(entity);
FileOutputStream outputStream = new FileOutputStream(new File("testOut.docx"));
IOUtils.write(entity.getData(), outputStream);
}
}
Config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.greg" />
<tx:annotation-driven />
<jpa:repositories base-package="com.greg" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:file:~/data/jpa-test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.greg" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>