我想使用JPA存储库将@lob(blob)数据保存到DB中。
我正在尝试类似下面的内容
User user = new user();
user.setProfile(<<BLOB DATA>>);
我想将用户配置文件设置为blob数据并保存到db
我们使用@Lob将数据保存在BLOB或CLOB中
实体:
@Entity
@Table(name="USER_LOB_TABLE")
public class User {
@Id
private Long userId;
@Lob
@Column(name="PROFILE")
private byte[] profile;
//getters and setters
}
JpaRepository:
public interface UserRepository extends JpaRepository<User,Long>{}
服务层:
userRepository.save(new User(1L,"hellodgasdgasdgasdgadsgas".getBytes()));
输出: USER_ID,轮廓 1,BLOB
为什么不使用Spring Content JPA?
pom.hml
<!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa-boot-starter</artifactId>
<version>0.0.11</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.11</version>
</dependency>
user.Java
@Entity
public class User {
@Id
private Long userId;
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
// if you have rest endpoints
@MimeType
private String mimeType = "text/plain";
JpaRepository
public interface UserRepository extends JpaRepository<User,Long>{}
users content store.Java
@StoreRestResource(path="usersContent")
public interface UsersContentStore extends ContentStore<User, String> {
}
这还将为您提供REST端点(@ / usersContent)来处理用户的内容。