Pinaki Poddar wrote:
> Item refers to Product via field 'produto' then why do you need a separate
> Integer field 'cdProduto'?
Sorry, I should have mentioned that the produto field is used only as
lookup! So, the idea is that on crud operations, we only use primitive
types.
> The item constructor is taking p.getCdProduto() as last argument, so I am
> assuming that the constructor is setting
> item.cdProduto field but not item.produto.
Correct, it is a decision's design!
>
> And nobody else is either so item.getProduto() is returning null.
No! As you could see in the Item i2 = em.find(Item.class, cdItem);
statement, shouldn't openjpa returns the item.produto object populate
through a new query (or eager) on db, when I try to access it?
>
> What you can do is:
> a) remove cdProduto field from Item
> b) pass Produto to Item constructor
> c) In Item constructor set this.producto = produto;
>
>> @Column(name = "cd_produto")
>> private Integer cdProduto;
>>
>> @ManyToOne
>> @JoinColumn(name = "cd_produto", referencedColumnName="cd_produto",
>> insertable = false, updatable = false)
>> private Produto produto;//read only
>
>
>
> Gilberto C Andrade wrote:
>> Hi,
>>
>> openjpa 1.0.1, jdk 1.5
>> Produto:
>>> @Entity
>>> @Table(name = "produto")
>>> public class Produto implements Serializable {
>>> @Id
>>> @GeneratedValue(strategy=GenerationType.TABLE,
>>> generator="ProdutoGerador")
>>> @TableGenerator(name="ProdutoGerador", table="ID_GERADOR",
>>> pkColumnName="PK",
>>> valueColumnName="AID", pkColumnValue="produto_id_gerador",
>>> allocationSize=1, initialValue=1)
>>> @Column(name="cd_produto", columnDefinition="INTEGER")
>>> private Integer cdProduto;
>> Item:
>>> @Entity
>>> @Table(name = "item")
>>> public class Item implements Serializable {
>>> @Transient
>>> protected final Log log = LogFactory.getLog(getClass());
>>> @Id
>>> @GeneratedValue(strategy=GenerationType.TABLE,
>>> generator="ItemGerador")
>>> @TableGenerator(name="ItemGerador", table="ID_GERADOR",
>>> pkColumnName="PK",
>>> valueColumnName="AID", pkColumnValue="item_id_gerador",
>>> allocationSize=1, initialValue=1)
>>> @Column(name="cd_item", columnDefinition="INTEGER")
>>> private Integer cdItem;
>> .
>> .
>> .
>>>
>>> @Column(name = "cd_produto")
>>> private Integer cdProduto;
>>>
>>> @ManyToOne
>>> @JoinColumn(name = "cd_produto", referencedColumnName="cd_produto",
>>> insertable = false, updatable = false)
>>> private Produto produto;//read only
>> OBS.: Originally this was mapped the following way, using hibernate's tag:
>>> <many-to-one
>>> name="produto"
>>> class="org.appfuse.model.estoque.Produto"
>>> cascade="none"
>>> outer-join="true"
>>> update="false"
>>> insert="false"
>>> access="property"
>>> column="cd_produto"
>>> />
>>
>>
>> Test:
>>> @Test
>>> public void persistRemoveItemTest() {
>>> log.debug("\npersistRemoveItemTest - Criação de uma instância da
>>> classe Item\n");
>>> String nomeItem = "Calça";
>>> Integer cdItem = null;
>>> BigDecimal precoVenda = new BigDecimal(0.0F);
>>> BigDecimal precoCusto = new BigDecimal(0.0F);
>>> UnidadeMedida uM = em.find(UnidadeMedida.class, "MT");
>>> Float estoqueAtual = 0.0F;
>>> Float estoqueMinimo = 0.0F;
>>> Float nivelDeReposicao = 0.0F;
>>> Boolean flDescontinuado = false;
>>> Produto p = em.find(Produto.class, 1);
>>>
>>> Item i = new Item(nomeItem,precoVenda, precoCusto,
>>> uM.getCdUnidadeMedida(), estoqueAtual, estoqueMinimo, nivelDeReposicao,
>>> flDescontinuado, p.getCdProduto());
>>> assertNull("cdItem antes do método persist:",i.getCdItem());
>>> em.getTransaction().begin();
>>> em.persist(i);
>>> em.getTransaction().commit();
>>> log.debug("Objeto pós gravação: \n"+i);
>>> cdItem = i.getCdItem();
>>> i = null;
>>>
>>> assertNotNull("cdItem pós persist:",cdItem);
>>>
>>> Item i2 = em.find(Item.class, cdItem);
>>> log.debug("Objeto carregado: \n"+i2);
>>> assertNotNull("Não pode ser
>>> nulo:",i2.getProduto());<==================== Here is the problem!
>>> }
>> As you can see I'm testing the lazy load of the Produto class. But i2
>> instance comes with null produto.
>>
>> Is there anything wrong with my mapping?
>>
>> Thanks,
>>
>> Gilberto
>>
>>
>