how to bind result of native query in data jpa to an entity with a @transient field

797 Views Asked by At

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

0

There are 0 best solutions below