users@glassfish.java.net

JPA ManyToOne relation problem

From: <glassfish_at_javadesktop.org>
Date: Mon, 25 Feb 2008 04:56:40 PST

I have following problem with ManyToOne relation. I have two entities

public class Params implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "paramId", nullable = false)
    private Long paramId;

    @Column(name = "paramName")
    private String paramName;

    @Column(name = "paramValue")
    private String paramValue;

    @JoinColumn(name = "productId", referencedColumnName = "productId")
    @ManyToOne
    private Product productId;


and another entity

public class Product implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "productDescriptorId", nullable = false)
    private Long productDescriptorId;

    @Column(name = "priceDescriptor")
    private String priceDescriptor;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productId")
    private Collection<Params> ParamsCollection;

I access them through generated with NetBeans facade, but when I retrieve "Product" from facade and then set "Param" collection in the database old values from Products doesn't delete and still exist. Here is the code

.......
productDescriptor = productDescriptorFacade.find(productDescriptorId);

List<Params> paramList = new ArrayList<Params>();
                for(int i = 0; i < 5; i++){
                        Params param = new Params();
                        param.setParamName(name);
                        param.setParamValue(value);

                        paramList.add(param);
                }
                                                                                
productDescriptorFacade.edit(productDescriptor);

..................

Here is Product Facade

@Stateless
public class ProductFacade implements ProductFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    /** Creates a new instance of ProductFacade */
    public ProductFacade() {

    }

    public void edit(Product product) {

        em.merge(product);
    }

    public Productr find(Object pk) {

        return (Product) em.find(Product.class, pk);
    }

....................
When I set new Param collection for Product the old values still exist in database.
Where I wrong ?
[Message sent by forum member 'petka' (petka)]

http://forums.java.net/jive/thread.jspa?messageID=260707