OneToOne returning wrong value

2

Please help me please, I have a problem here and I'm banging my head to resolve this for hours. I use Spring in the project and I have the following relationship in one of my models:

@OneToOne()
@JoinColumn(name = "ITE_COD_INTERNO")
@NotFound(action = NotFoundAction.IGNORE)
@Getter @Setter
private ItemPreco preco;

In the ItemPreco I have this:

@OneToOne(mappedBy = "preco")
@Setter
private Item item;

My ItemPreco class has two main fields:

@Id
@Column(name = "TPC_COD_INTERNO")
@Getter @Setter
private String itemId;

@Column(name = "TPC_UNIDADE")
@Getter @Setter
private String unidade;

My repository that performs the query is as follows:

@Query("SELECT i FROM Item i "
        + "WHERE i.grupo.visivel = :visivel "
        + "AND i.preco.unidade = :unidade "
        + "AND i.itemId = :itemId "
        + "AND i.preco.preco <> 0 "
        + "ORDER BY i.descricao")
Item findByItemIdAndVisivelAndUnidade(@Param("itemId") String itemId, @Param("visivel") boolean visivel, @Param("unidade") String unidade);

The feedback I receive is a line from a "random" drive rather than the one I've been looking for. Example: I searched for drive 001 and I get drive 002.

And what I want is to fetch an item and its price, which has a OneToOne relationship if searched for by internal code + drive.

If someone can give me a light on how to solve this, I thank you, thank you in advance!

    
asked by anonymous 24.11.2017 / 13:36

1 answer

1

Solution found: I created a constructor with attributes for the Item class and instead of using the relationship with the ItemPreco table, I just used the value returned from the select.

Builder:

    public Item(String itemId, String codBarra, CbAlt codBarraAlterado, Long estado, String complemento, String descricao, String unidade, String pesado, Long opcional, Integer opcionaisGratis,
        String impressoras, String tempoPreparo, GrupoItem grupo, String cardapio, Double precoSelecionado) {
    super();
    this.itemId = itemId;
    this.codBarra = codBarra;
    this.codBarraAlterado = codBarraAlterado;
    this.estado = estado;
    this.complemento = complemento;
    this.descricao = descricao;
    this.unidade = unidade;
    this.pesado = pesado;
    this.opcional = opcional;
    this.opcionaisGratis = opcionaisGratis;
    this.impressoras = impressoras;
    this.tempoPreparo = tempoPreparo;
    this.grupo = grupo;
    this.cardapio = cardapio;
    this.precoSelecionado = precoSelecionado;
}

Then I created an attribute with @Transient in the Model Item:

@Transient
@Getter @Setter
private Double precoSelecionado;

And my select in the ItemRepository looks like this:

@Query("SELECT new br.com.rp.restaurante.model.Item(i.itemId, i.codBarra, i.codBarraAlterado, i.estado, i.complemento, i.descricao, i.unidade, i.pesado, i.opcional, i.opcionaisGratis, i.impressoras, i.tempoPreparo, i.grupo, i.cardapio, i.preco.preco) FROM Item i "
        + "WHERE ((i.codBarra LIKE %:codBarra%) OR (i.itemId LIKE %:itemId%) OR (lower(i.descricao) LIKE concat('%', lower(:descricao), '%') )) "
        + "AND i.grupo.visivel = :visivel "
        + "AND i.preco.unidade = :unidade "
        + "AND i.preco.preco <> 0 "
        + "ORDER BY i.descricao")
List<Item> findByItemIdContainingOrCodBarraContainingOrDescricaoContainingAllIgnoreCaseAndGrupo_VisivelTrueOrderByDescricao(@Param("itemId") String itemId, @Param("codBarra") String codBarra, @Param("descricao") String descricao, @Param("visivel") boolean visivel, @Param("unidade") String unidade);

In this way, I get the correct value returned from the select and step into the @Transient attribute of the Item class and use it.

What happened before was that I was not calling that after selecting, regardless of the parameterization, the relationship that was considered and not what I used in select, then it created an object with the proper relationships. And since the ItemPreco table had two attributes that should be considered at the time of the relationship (unit / product id), which did not happen, because the relationship considered only the product id (obvious) and the unit was left out, this entailed in a return of the first line of select, that could be the price of any unit.

    
27.11.2017 / 01:51