0
votes

Can't figure out how to retreive a 'blob' type from my DB. Can't figure out how to do in JPA.

public interface ActeRepository extends JpaRepository<byte[], String> {
    @Query(value = "select doc from t_doc_content", nativeQuery = true)
    public List<byte[]> findActeByBordereau(String id);
}

Error:

Caused by: java.lang.IllegalArgumentException: Not an managed type: class [B at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.(JpaMetamodelEntityInformation.java:68) at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ... 29 common frames omitted

Any idea ?

2

2 Answers

0
votes

The definition of your interface is not correct

Maybe change to

public interface ActeRepository extends JpaRepository< Acte, String> {

Is the primary key a String, if so the above is OK, else change String to whatever it is. Long ?

0
votes

This is not the way JpaRepository works, when extending it you need to remember that it is of type JpaRepository<T, ID extends Serializable>, where T is a POJO that you mapped as a table in the database maybe using @Entity and the ID is you primary key of that table, Long or Integer, I doubt you have a String as your primary key as integers are faster to index. I dont't know the rest of your code but to give you an example:

@Entity
public class File {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@Lob
private byte[] bytes;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

public File() {
}

public File(String name, byte[] bytes, User user) {
    this.name = name;
    this.bytes = bytes;
    this.user = user;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public byte[] getBytes() {
    return bytes;
}

public void setBytes(byte[] bytes) {
    this.bytes = bytes;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}
}

@Repository
public interface FileRepository extends JpaRepository<File, Long>{

List<File> findByName(String name);
}

And after you get a list of objects, you get that bytes variable from each object.