Reputation: 2216
I have table with two columns:
@Entity
@Table(name = "documents")
public class Document {
@Id
@Column(name = "key")
public String key;
@Lob
@Column(name = "value")
public byte[] value;
....some more data...
}
And I have another table, with some field where I have a reference to the Document table, via key column reference:
@Entity
@Table(name = "table")
public class Table {
...some values and id...
@Nullable
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Document.class)
@JoinColumn(name = "some_document_id", referencedColumnName = "key",
foreignKey = @ForeignKey(name = "document_id_fk"))
@Convert(converter = ByteArrayToStringConverter.class)
public String documentValue; //join documents by key column and return value column
}
What I want, is to fetch not the whole Document
with all fields when joining it from this table, but to fetch only one column named value
, and then convert it from byte[]
to String
.
I understand that I can just call getter from java code, but I am curious if that is possible by Jpa/Hibernate API?
Upvotes: 2
Views: 2245
Reputation: 376
You can create a method in your repository with @Query
annotation like below code
@Repository
public interface TableRepository extends JpaRepository<User, Serializable>{
@Query("select d.value from Table t LEFT JOIN t.documentValue d where d.key=:key")
byte[] findValue(@Param("key") String key);
}
This method will return you a byte[] array that you can easily convert in String using
String s = new String(byteArray)
Upvotes: 1