2
Guys, help me please, I’ve got a problem here and I’ve been racking my brain trying to figure this out for hours. I use Spring in the project and 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 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 I perform 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 return I receive is a line from a "random" unit and not the one I sought. Example: I searched for unit 001 and get unit 002.
And what I want is to fetch an item and the price of it, which has a Onetoone relationship if searched by internal code + unit.
If anyone can give me a light on how to resolve this I thank you very much from now on!
You’re confused about this. For example: you say that "My Item class has two main fields": Itemid and drive; however in your query you put as if Itemid were in Item (
i.itemId:=itemId
) and unit in an Item attribute calledpreco
(i.preco.unidade
), that wasn’t even shown in your Item class.– mari
The two classes have an Itemid attribute to be able to relate to each other. And I think you read wrong the part that said, "My Item class has two main fields," actually I wrote "My Itempreco class has two main fields," maybe that’s why I got confused
– GiovaniOliveira
Still confused. If Itempreco has Item "Mapped by preco" would not need an Itemid to map. I think it’s best to put the complete code of the Item and Itempreco classes.
– mari