users@glassfish.java.net

jpa data integrity problem (doesn't update)

From: <glassfish_at_javadesktop.org>
Date: Mon, 27 Jul 2009 23:58:12 PDT

Hi,

em.persist(entity);
doesn't update one-to-many relation
for instance sample 2 table:


countries
---------------------
ID
Name

cities
---------------------
ID
CountryID
Name


jpa entity class for countries table:


@Entity
@Table(name = "countries", catalog = "test", schema = "")
@NamedQueries({_at_NamedQuery(name = "Country.findAll", query = "SELECT c FROM Country c")})
public class Country implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "ID", nullable = false)
  private Short id;
  
  @Basic(optional = false)
  @Column(name = "Name", nullable = false, length = 45)
  private String name;
  
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "countryID")
  private List<City> cityList;

  / ... getters and setters
}


jpa entity class for cities table:



@Entity
@Table(name = "cities", catalog = "test", schema = "")
@NamedQueries({_at_NamedQuery(name = "City.findAll", query = "SELECT c FROM City c")})
public class City implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "ID", nullable = false)
  private Short id;
  
  @Basic(optional = false)
  @Column(name = "Name", nullable = false, length = 45)
  private String name;
  
  @JoinColumn(name = "CountryID", referencedColumnName = "ID", nullable = false)
  @ManyToOne(optional = false)
  private Country countryID;

  // gettes and setters ...
}



and simple servlet for testing:



// ...
          DbHelper db = new DbHelper();
          
          List<Country> countries = db.getSpResults("Country.findAll");
          List<City> cities = db.getSpResults("City.findAll");
          
          System.out.println("---------------------");
          System.out.println("countries count: " + countries.size());
          System.out.println("cities count : " + cities.size());
          System.out.println("#####################");
          
          for (Country country : countries) {
            System.out.println(country.getName());
            for (City city : country.getCityList()) {
              System.out.println(" " + city.getName());
            }
          }
          
          Country turkiye = (Country) db.find(Country.class, (short)1);
          
          City c = new City();
          c.setCountryID(turkiye);
          c.setName("Ankara");

          db.create(c);
          
          countries = db.getSpResults("Country.findAll");
          cities = db.getSpResults("City.findAll");
          
          System.out.println("---------------------");
          System.out.println("countries count: " + countries.size());
          System.out.println("cities count : " + cities.size());
          System.out.println("#####################");
          
          for (Country country : countries) {
            System.out.println(country.getName());
            for (City city : country.getCityList()) {
              System.out.println(" " + city.getName());
            }
          }

// ...


cityList property doesn't update.
db.create(c) includes em.persist(entity).
[Message sent by forum member 'firatkucuk' (firatkucuk)]

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