Spring+JPA, visible Transient field in View and not persistent?

Asked

Viewed 718 times

1

Good evening guys, I have a filter that I do manually with @query() that in it I make a total calculating Join of a sale ie,

SUM(valor * quantidade) as total

At the end I’ll put the filter for you... what happens is the following, this field "temporario" total that refers to the total of the sale, when I create it in my Entity jpa want to persist it in the bank, then I use @Transfer but it is not visible in the view, I would have to give a setTotal() in the controller so it can have some value...

I have tried it in several ways, below....

@NumberFormat(pattern = "#,##0.00")
transient BigDecimal total;

Thus above, it is not visible in the view, it does not bring back the value of the bank, only if I give a setTotal().

@Column(updatable = false, insertable = false) // até assim (não funciona)
@NumberFormat(pattern = "#,##0.00")
@Transient
private BigDecimal valortemporario;

after form above, it is visible in the view, but tries to persist in the database, logic that gives error, because the field does not exist in the table.

@Column(updatable = false, insertable = false) // até assim (não funciona)
@NumberFormat(pattern = "#,##0.00")
private BigDecimal valortemporario;

That way above, also does not work, it tries to record in tbm bank..

Below I leave my Repository only to see have general notion of the thing:

public interface AtendimentosRepository extends JpaRepository<Atendimentos, Long> {

String sqlPrincipal = "SELECT id_heados, pecastotal.total as total, "
        +" maoobra_heados,+"veiculo_km_heados,bsextras_heados,"
        +" tipoatendimento_heados,  "
        + " datasaida_heados FROM heados  "

        + " LEFT JOIN ( (SELECT pedos.controle_pedos,adicional_pedos,"
        + " sum( ( COALESCE(valor_pedos,'0') * COALESCE(quantidade_pedos,'0') ) - (  "
        + " ( COALESCE(valor_pedos,'0') * COALESCE(quantidade_pedos,'0') )  * COALESCE(desconto_pedos,'0') /100"
        + " ) ) as total FROM pedos as pedos  WHERE adicional_pedos  = 'N'"
        + " group by pedos.controle_pedos) ) pecastotal ON controle_pedos  = id_heados"

        + " WHERE id_heados = :controle ";

@Query(value = sqlPrincipal, nativeQuery = true)
public Atendimentos atendimentoByControle(@Param("controle") Long controle);

}

I tried to find a way to remove the referent field from my Entity class total, but found no function for it.

Has anyone ever been through this?? has another solution??

Thank you!!

  • The annotation @Transient that you are using is from the package javax.persistence.Transient? A very common error is using the package annotation org.springframework.data.annotation who does something quite different (see that answer in Soen)

  • Opa, thanks for answering, I’ve used the two packages no works!

  • org.springframework.data.annotation.Transient Here, let me visualize but try to persist in the database. @javax.persistence.Transient&#Xa and in that redirect I lose the mysides of Entity.

  • Renato now understood the problem better. Maybe bringing the result of the query to an external POJO instead of your Entity is the best solution in your case. Something like Sqlresultsetmapping maybe it’ll come in handy.

  • Opa @Anthonyaccioly , as I have not used yet the Sqlresultsetmappging went a look, but I still did not realize how can help me, but I will research even better on the same, but I keep thinking how can not have a simpler exit... I just need not persist this field, in fact he should already know that this field is creating at runtime, only at the time of the sale filter with JOIN->SUM().... I’m caught in it for days, qq way Obg!!

  • @Anthonyaccioly I did practically a gambiarra, but working, I uncoupled the Entity "daughter" of the Entity "father" or I commented on the Onetomany and Manytoone , and in the controller I instated the Entity "daughter" filtering by the number of the sale and after that I gave a for (Search obj : pecasAtend) totaling the sale and then a setValorTotal() sending to the view. at the end of the accounts almost q da no same, taking away the FOR and the setValorTotal() that I had to do manually, apart from that to the bank deducted qq way the JPA would do qndo using Manytoone. Obg

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.