users@glassfish.java.net

JPA in JSE updating entities

From: <glassfish_at_javadesktop.org>
Date: Wed, 16 Jan 2008 16:52:32 PST

I'm trying to update an entity's ID (aka primary key in relational model) in my JSE application. Is this possible? Or do I need to delete the old entity and creating a new one with the new ID?

I thought I had it working at one point, with a mere em.merge(), but it no longer propagates to the database. My entities are shown here. (FruitFamily think "apples, oranges, bananas, ...? and FruitVariety for apples think "golden delicious, fuji, gala, ...")

[code]
@Entity
@Table(name = "FRUITVARIETY")
public class FruitVariety implements Serializable {

   @Id
   @Column(name = "VARIETY_NAME", nullable = false)
   private String varietyName;

   @Column(name = "HARVEST_DATE", nullable = false)
   @Temporal(TemporalType.DATE)
   private Date harvestDate;

   @JoinColumn(name = "FRUIT_FAMILY", referencedColumnName = "FRUIT_FAMILY")
   @ManyToOne
   private FruitFamily fruitFamily;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "fruitVariety")
   private Collection<fruitFamily> fruitFamilyCollection;
}

@Entity
@Table(name = "FRUITFAMILY")
public class FruitFamily implements Serializable {

   @Id
   @Column(name = "FRUIT_FAMILY", nullable = false)
   private String fruitFamily;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "fruitVariety")
   private Collection<FruitVariety> fruitVarietyCollection;
}
[/code]

My update methods are located in a class that I suppose is a bean outside of an EE container (if that really means anything). The updateFruitVariety method is the what I thought was working at one point. The updateVarietyName is an attempt to make it work. The FruitVariety objects are being updated in a TableModel that is part of a Swing application. Every edit in the Swing JTable view will propagate directly to the database. FruitService is just the interface that declares the methods of this class.

[code]
public FruitServiceBean implements FruitService{
   private EntityManager em;

   public FruitServiceBean(EntityManager em){
      this.em = em;
   }
   
   public void updateFruitVariety (FruitVariety fv){
      em.getTransaction ().begin ();
      em.merge (fv);
      em.getTransaction ().commit ();
   }
   
   public void updateVarietyName (FruitVariety fv, String name){
      em.getTransaction ().begin ();
      FruitVariety mfv = em.merge (fv);
      mfv.setVarietyName (name);
      em.getTransaction ().commit ();
   }
}
[/code]

By the way I'm using TopLink Essentials, and JavaDB.

All help and correction is appreciated.
[Message sent by forum member 'martin_woolstenhulme' (martin_woolstenhulme)]

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