Hi Jose,
since version 1.0.2 the mandatory auto-update/delete settings are 'none'
<snip release-notes 1.0.2>
- odmg-api: ** All relations (1:1, 1:n and m:n) need auto-update/delete
setting 'none' to proper work.**
</snip>
In 1.0.4 we introduce cascading delete for auto-delete, please see
documentation:
http://db.apache.org/ojb/docu/guides/odmg-guide.html#Specific+Metadata+Settings
If this doesn't solve your problem, please let me know (then I will try
to reproduce your problem with a test-case).
By the way, in the example below the data will never be written to
database, because you delete a and b before the first commit. Further
it's recommended to use Database.makePersistent(...) to persist new objects.
http://db.apache.org/ojb/docu/tutorials/odmg-tutorial.html#Persisting+New+Objects
regards,
Armin
Jose Maria wrote:
Hi.
I've recently updated from OJB 1.0.3 to 1.0.4 and I'm having with the
ODMG API. What worked in 1.0.3 now doesn't. It seems to be a change in
the states classes, but I'm not sure. In short, the problem could be
explained with a simple example.
We have two classes A and B, related 0:N. If I create one object of each
class and then delete them, OJB tries to insert B object which result in
a SQL error: foreing key constraint.
Here is some code to show you:
repository_user.xml
--------------------------------
<class-descriptor class="pruojb.A" table="A">
<field-descriptor name="id" column="ID" jdbc-type="INTEGER"
primarykey="true" />
<field-descriptor name="nombre" column="NOMBRE" jdbc-type="VARCHAR" />
<collection-descriptor name="items" element-class-ref="pruojb.B"
auto-retrieve="true" auto-update="false" auto-delete="false">
<inverse-foreignkey field-ref="id"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor class="pruojb.B" table="B">
<field-descriptor name="id" column="ID" jdbc-type="INTEGER"
primarykey="true" />
<field-descriptor name="descrip" column="DESCRIP" jdbc-type="VARCHAR" />
<field-descriptor name="percent" column="PERCENT" jdbc-type="DOUBLE" />
</class-descriptor>
Test class: Main.java
--------------------------------
package pruojb;
import org.apache.ojb.odmg.OJB;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
import org.odmg.Transaction;
public class Main {
public static void main(String[] args) {
Implementation ojb = (Implementation) OJB.getInstance();
Database db = ojb.newDatabase();
try {
db.open("default#MED#med", Database.OPEN_READ_WRITE);
Transaction tx = ojb.newTransaction();
tx.begin();
// Create A and B objects and make persistent
A a = new A();
a.setId(10);
a.setNombre("Ten");
tx.lock(a, Transaction.WRITE);
B b = new B();
b.setId(a.getId());
b.setDescrip("Detail");
b.setPercent(80.5);
a.getItems().add(b);
tx.lock(b, Transaction.WRITE);
// Delete A and B
db.deletePersistent(a);
db.deletePersistent(b);
tx.commit();
db.close();
} catch (ODMGException ex) {
ex.printStackTrace();
}
}
}
A.java
-----------------------------
package pruojb;
import java.util.ArrayList;
import java.util.List;
public class A {
private int id;
private String nombre;
private List items;
public A() {
items = new ArrayList();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
}
B.java
---------------------------------------
package pruojb;
public class B {
private int id;
private String descrip;
private double percent;
public B() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescrip() {
return descrip;
}
public void setDescrip(String descrip) {
this.descrip = descrip;
}
public double getPercent() {
return percent;
}
public void setPercent(double percent) {
this.percent = percent;
}
}
Part of the output when using 1.0.4:
* SQLException during execution of sql-statement:
* sql statement was 'INSERT INTO B (ID,DESCRIP,PERCENT) VALUES (?,?,?) '
* Exception message is [GDS Exception. 335544466. violation of FOREIGN
KEY constraint "FK_B_1" on table "B"]
* Vendor error code [335544466]
* SQL state code [HY000]
* Target class is 'pruojb.B'
* PK of the target object is [id=10]
* Source object: [EMAIL PROTECTED]
* The root stack trace is -->
* org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544466.
violation of FOREIGN KEY constraint "FK_B_1" on table "B"
at
org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:503)
org.apache.ojb.broker.PersistenceBrokerSQLException:
* SQLException during execution of sql-statement:
* sql statement was 'INSERT INTO B (ID,DESCRIP,PERCENT) VALUES (?,?,?) '
* Exception message is [GDS Exception. 335544466. violation of FOREIGN
KEY constraint "FK_B_1" on table "B"]
* Vendor error code [335544466]
* SQL state code [HY000]
* Target class is 'pruojb.B'
* PK of the target object is [id=10]
* Source object: [EMAIL PROTECTED]
at
org.apache.ojb.broker.util.ExceptionHelper.generateException(ExceptionHelper.java:256)
at
org.apache.ojb.broker.util.ExceptionHelper.generateException(ExceptionHelper.java:90)
at
org.apache.ojb.broker.util.ExceptionHelper.generateException(ExceptionHelper.java:71)
at
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:230)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.storeToDb(PersistenceBrokerImpl.java:1891)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.store(PersistenceBrokerImpl.java:879)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.odmg.ObjectEnvelope.doInsert(ObjectEnvelope.java:644)
at
org.apache.ojb.odmg.states.StateNewDirty.commit(StateNewDirty.java:106)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeAllEnvelopes(ObjectEnvelopeTable.java:241)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeObjects(ObjectEnvelopeTable.java:178)
at
org.apache.ojb.odmg.TransactionImpl.doWriteObjects(TransactionImpl.java:403)
at
org.apache.ojb.odmg.TransactionImpl.prepareCommit(TransactionImpl.java:762)
at
org.apache.ojb.odmg.TransactionImpl.commit(TransactionImpl.java:698)
at pruojb.Main.main(Main.java:36)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception.
335544466. violation of FOREIGN KEY constraint "FK_B_1" on table "B"
at
org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:503)
at
org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:144)
at
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:211)
... 12 more
at
org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:144)
at
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:211)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.storeToDb(PersistenceBrokerImpl.java:1891)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.store(PersistenceBrokerImpl.java:879)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.odmg.ObjectEnvelope.doInsert(ObjectEnvelope.java:644)
at
org.apache.ojb.odmg.states.StateNewDirty.commit(StateNewDirty.java:106)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeAllEnvelopes(ObjectEnvelopeTable.java:241)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeObjects(ObjectEnvelopeTable.java:178)
at
org.apache.ojb.odmg.TransactionImpl.doWriteObjects(TransactionImpl.java:403)
at
org.apache.ojb.odmg.TransactionImpl.prepareCommit(TransactionImpl.java:762)
at
org.apache.ojb.odmg.TransactionImpl.commit(TransactionImpl.java:698)
at pruojb.Main.main(Main.java:36)
at org.firebirdsql.gds.GDSException: violation of FOREIGN KEY constraint
"FK_B_1" on table "B"
at
org.firebirdsql.jgds.GDS_Impl.readStatusVector(GDS_Impl.java:1816)
at org.firebirdsql.jgds.GDS_Impl.receiveResponse(GDS_Impl.java:1769)
at
org.firebirdsql.jgds.GDS_Impl.isc_dsql_execute2(GDS_Impl.java:917)
at
org.firebirdsql.jca.FBManagedConnection.executeStatement(FBManagedConnection.java:793)
at
org.firebirdsql.jdbc.AbstractConnection.executeStatement(AbstractConnection.java:946)
at
org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:499)
at
org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:144)
at
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:211)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.storeToDb(PersistenceBrokerImpl.java:1891)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.store(PersistenceBrokerImpl.java:879)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:158)
at
org.apache.ojb.odmg.ObjectEnvelope.doInsert(ObjectEnvelope.java:644)
at
org.apache.ojb.odmg.states.StateNewDirty.commit(StateNewDirty.java:106)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeAllEnvelopes(ObjectEnvelopeTable.java:241)
at
org.apache.ojb.odmg.ObjectEnvelopeTable.writeObjects(ObjectEnvelopeTable.java:178)
at
org.apache.ojb.odmg.TransactionImpl.doWriteObjects(TransactionImpl.java:403)
at
org.apache.ojb.odmg.TransactionImpl.prepareCommit(TransactionImpl.java:762)
at
org.apache.ojb.odmg.TransactionImpl.commit(TransactionImpl.java:698)
at pruojb.Main.main(Main.java:36)
I hope someone can help me. Thank you in advance.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]