1

I work on a spring boot, data jpa web application and have a problem with an entity that has a @transient field and I want to bind result of my native query to this entity (all fields even @transient field)

suppose Chat entity class in brief :

    @entity
public class Chat  implements  Serializable{

@Id
@GeneratedValue(strategy =  GenerationType.IDENTITY)
private long id;

@ManyToOne
@JoinColumn(name="user_from")
private User userFrom;

@ManyToOne
@JoinColumn(name="user_to")
private User userTo;

@Transient
private int unread;

//getter and setter }

I have a native query that get chat list of user by left joining of chat table with chat_message table, in my select statement I have something like this:

select c.*,count(m.id) as unread from chat c left join chat_message m where ...

I need to bind result of this query to my chat entity, and it is important to value of unread field of above query set to my @transient unread field of chat class,

but this @transient field ignored by data jpa I tried more and more by using @SqlResultSetMapping and data jpa projection and ..., but there was no result for me. by using @SqlResultSetMapping, @transient filed still ignored, by using jpa projection, @transient filed is ok, but UserFrom and UserTo fields are null.

please guide me to overcome this problem if you had a similar experience or have any idea about this,

thanks

samaz
  • 11
  • 2
  • That's not possible. Transient fields are ignored by JPA – Simon Martinelli Jul 06 '22 at 12:31
  • @SimonMartinelli thanks for your response, oh...what should I do in this situation? how to get unread from my native query and map to my entity? select c.*,count(m.id) as unread from chat c left join chat_message m where.... – samaz Jul 06 '22 at 14:27
  • You can do it yourself with a JPQL query similar to "Select c, size(c.messages) from Chat c where ...". Assuming you have a OneToMany to chat_messages mapped as messages, this will return Chat instances with the count(m.id) value - you then have to loop through the returned list and set the transient with the count, or just use the value and keep your Entity as is. Alternatively, you can map it as a DTO https://smarterco.de/spring-data-jpa-query-result-to-dto/ – Chris Jul 06 '22 at 14:44
  • You shouldn't map it to an Entity but a DTO – Simon Martinelli Jul 06 '22 at 15:27
  • @Chris thanks, when map the result as a DTO, how to manage fileds of DTO that related to manyToOne fileds of entity ? I get null for userFrom and userTo of my DTO because there is no manyToOne annotation in DTO. can you explain me how to create query and DTO for my scenario? – samaz Jul 12 '22 at 07:25
  • Using SQL 'native' queries means JPA won't build mappings for you unless you are defining exactly how (an SQLResultSetMapping can define how to build and return multiple entities), but you are on your own to work out the joins.. Use a (JPQL) constructor query and pass in the Chat mappings to the constructor to let JPA do it all for you - you are likely better off with returning managed Chat instance and have the count data external though, as the Chat will have all references managed as well. – Chris Jul 12 '22 at 13:15

0 Answers0