如何使用spring JPA存储库保存@lob数据

问题描述 投票:1回答:2

我想使用JPA存储库将@lob(blob)数据保存到DB中。

我正在尝试类似下面的内容

User user = new user();
user.setProfile(<<BLOB DATA>>);

我想将用户配置文件设置为blob数据并保存到db

spring jpa spring-data-jpa
2个回答
0
投票

我们使用@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


0
投票

为什么不使用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)来处理用户的内容。

© www.soinside.com 2019 - 2024. All rights reserved.