Property field not updated UNLESS 1st initialised [message #492475] |
Tue, 20 October 2009 10:56  |
Eclipse User |
|
|
|
Have a problem with property fields not being updated in the dB - am using eclipselink for ORM with JPA.
Have underlying dB in javadb with
tab 1
id (PK)
..
tab 2
..
NAME varchar
AGE int
FK-->id (tab 1)
Then have entitiy POJOs for each table with the FK in tab 2 annotated with joincol/many-1 annotations. Also have cascade type set to ALL for collection property in tab1 to hold tab 2 objects.
I create editor class in swing with property to hold newly created tab1 and tab 2 objects. The NAMEand AGE (for tab 2) are set via swing txt fields that have been bound to corrsponding entity properties.
I only persist the object which corresponds to tab 1 (cascadetype set to ALL) within the editor class.
The problem is I enter name and age values within the editor, but only the name value is updated in the datbase? HOWEVER, if I initialise the AGE value and change it within the editor it IS updated in the dB.
Likewise, if I initialise the AGE value in the tab 2 entity constructer to anything BUT NULL, all works fine.
In summary, the AGE property field for POJO represented by tab 2 only gets updated via binding IF it is initiliased to a non NULL value? Can anyone shed any light on this - tis a mystery to me.
thanks,
[Updated on: Wed, 21 October 2009 04:12] by Moderator
|
|
|
|
Re: Property field not updated UNLESS 1st initialised [message #493075 is a reply to message #492475] |
Thu, 22 October 2009 20:47   |
Eclipse User |
|
|
|
Relevant code snippets below...
=========Entity classes ===============
public class Customers implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Integer id;
...
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "custId")
private List<Boonga> boongaCollection;
...
...
public class Boonga implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "B_ID")
private Integer bId;
@Column(name = "B_NAME")
private String bName;
@Column(name = "B_AGE")
private Integer bAge;
@JoinColumn(name = "CUST_ID", referencedColumnName = "ID")
@ManyToOne
private Customers custId;
...
...
//setters for Boonga entity......
public void setBName(String bName) {
this.bName = bName;
}
public void setBAge(Integer bAge) {
Integer oldbAge = this.bAge;
this.bAge = bAge;
//added later to try and solve issue
changeSupport.firePropertyChange("bAge", oldbAge, bAge);
}
=====CustomerEditor Class
public class CustomerEditor extends javax.swing.JDialog {
protected Customers currentRecord;
protected Boonga currentBoonga;
init()..{
...
binding =
org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.
READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentBoonga.BAge}"),
jFormattedTextField3, org.jdesktop.beansbinding.BeanProperty.create("value"));
bindingGroup.addBinding(binding);
...
...
binding =
org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.
READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentBoonga.BName}"), jTextField8,
org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
======Main app class...
...
...
public void newRecord() {
Customers c = new Customers();
Boonga boo = new Boonga();
Collection<Boonga> booc = new LinkedList<Boonga>();
c.setBoongaCollection((List)booc);
boo.setCustId(c);
booc.add(boo);
entityManager.persist(c);
list.add(c);
int row = masterTable.getRowCount() - 1;
masterTable.setRowSelectionInterval(row, row);
masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
setSaveNeeded(true);
JFrame mainFrame = CustomerRecordsApp.getApplication().getMainFrame();
CustomerEditor ce = new CustomerEditor(mainFrame, false);
ce.setCurrentRecord(c);
ce.setCurrentBoonga(boo);
ce.setVisible(true);
if (ce.isCustomerConfirmed()) {
save().run();
} else {
refresh().run()
...
...
You'll see Name and Age are part of the same entity - BName get's updated via binding and Age doesn't unless first initialised either in the constructor or by using it's setter upon creation.
thanks,
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04237 seconds