Mailing List webobjects-dev@wocommunity.org Message #232
From: Walter Lo Nigro <webobjects-dev@wocommunity.org>
Subject: Re: [WO-DEV] Very stupid question re: One-to-many, single table inheritance in D2W ERModernLook
Date: Thu, 8 Jul 2021 07:31:01 +0200
To: WebObjects & WOnder Development <webobjects-dev@wocommunity.org>
Thank you Ralf, that was it. I’ve been away from WO and Wonder for more than 10 years, and forgot quite a lot. Seems I’ll have to get back to Chuck’s book and Wonder source...

Again, thank you very much.
Walter

***  Walter Lo Nigro, Trieste, Italy  -  http://www.wln.it  ***
***  Choral Activities, "G. Tartini" Conservatory, Trieste  ***
***  Freelance conductor, RiscOS/ROOL registered developer  ***

Il giorno 06 lug 2021, alle ore 17:55, Ralf Schuchardt <webobjects-dev@wocommunity.org> ha scritto:

Hi Walter,

maybe I can explain the problem you are seeing.

In `awakeFromInsertion` you create a new Qualifica object.

Qualifica qual = new Qualifica();
eo.insertObject(qual);

This Qualifica object has an `id` attribute probably of type `serial` or maybe an `int4` with an attached sequence. EOF uses the sequence (or an internal table) to assign the new primary key for this new object.
But as you have already inserted some values to Qualifica the sequence generation is probably out of sync with the already inserted values. Therefore you get a duplicate key error when using the sequence values in the new object.
To solve this you may need to update the Qualifica sequence (if used by EOF) with the greatest id value already used in Qualifica.

But I don’t know, why you even create a new Qualifica object in `awakeFromInsertion`, as setting the qualifica relationship already assigns one in `createStudente`.
If you want to insert it as a default value, it may be better to fetch the existing one:

public void awakeFromInsertion(EOEditingContext ec) {
super.awakeFromInsertion(ec);
Qualifica qual = Qualifica.fetchRequiredQualifica(ec, Qualifica.TIPO.eq("studente“));
this.setQualificaRelationship(qual);
}

If you are using Wonder you may also want to use the init(EOEditingContext) method instead of awakeFormInsertion.

Ralf

On 6 Jul 2021, at 15:37, Walter Lo Nigro wrote:

Hi everybody,

I just resolved to write to the list, obviouly risking any sort of ominously nefast/sarcastic comments… (please be kind).

I am struggling with a very simple situation, and I’m sure I’m missing something basic. I apologize for that.

I have a generic entity ‘Utente’, and another one ‘Studente’ which inherits from the first one via single-table inheritance.
I set up a further entity ‘Qualifica’ with just one column ’tipo', and I populate it with some values (just “studente” for a start), and use it merely as a lookup table for Utente and Studente via a One-to-many relationship:

Utente.qualifica.tipo << - > Qualifica.tipo

Utente has also a key ‘tipo’ which is the flattened relationship between Utente and Qualifica.
Obviously same for Studente, which also have a qualifier: tipo = “studente”.

For Studente I have added something to preset value of tipo = “studente”, here is the extended class definition (which inherits from _Studente, which of course inherits from _Utente base class):

public class Studente extends _Studente {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(Studente.class);

public void awakeFromInsertion(EOEditingContext eo) {
super.awakeFromInsertion(eo);
Qualifica qual = new Qualifica();
eo.insertObject(qual);
qual.setTipo("studente");
this.setQualifica(qual);
this.setTipo(qual.tipo());
}

public static Studente createStudente(EOEditingContext editingContext, String nome, your.app.model.Qualifica qualifica) {
Studente eo = (Studente) EOUtilities.createAndInsertInstance(editingContext, _Studente.ENTITY_NAME);
eo.setNome(nome); // (n.b. the only other attribute in Utente/Studente)
eo.setQualificaRelationship(qualifica);
return eo;
}
}

Everything seems ok but, when I try and create a new Studente object (having already inserted a “studente” row into Qualifica), I get the following error:

com.webobjects.eoaccess.EOGeneralAdaptorException: EvaluateExpression failed: <com.webobjects.jdbcadaptor.PostgresqlExpression: "INSERT INTO Qualifica(id, tipo) VALUES (?::int4, ?::varchar(50))" withBindings: 1:4(id), 2:"studente"(tipo)>: Next exception:SQL State:23505 -- error code: 0 -- msg: ERROR: duplicate key value violates unique constraint "qualifica_pk" Dettaglio: Key (id)=(4) already exists.

In Properties I have:
# EOF
er.extensions.ERXEC.safeLocking=true
er.extensions.ERXEC.useSharedEditingContext=false
er.extensions.ERXEnterpriseObject.applyRestrictingQualifierOnInsert=false
er.extensions.ERXEnterpriseObject.updateInverseRelationships=false

Which is the best method to avoid the database server trying to update BOTH sides of relationship? I am using Postgresql.

Please help (and have mercy on me). Thank you.


***  Walter Lo Nigro, Trieste, Italy  -  http://www.wln.it  ***
***  Choral Activities, "G. Tartini" Conservatory, Trieste  ***
***  Freelance conductor, RiscOS/ROOL registered developer  ***


#############################################################
This message is sent to you because you are subscribed to
 the mailing list <webobjects-dev@wocommunity.org>.
To unsubscribe, E-mail to: <webobjects-dev-off@wocommunity.org>
To switch to the DIGEST mode, E-mail to <webobjects-dev-digest@wocommunity.org>
To switch to the INDEX mode, E-mail to <webobjects-dev-index@wocommunity.org>
Send administrative queries to  <webobjects-dev-request@wocommunity.org>

#############################################################
This message is sent to you because you are subscribed to
 the mailing list <webobjects-dev@wocommunity.org>.
To unsubscribe, E-mail to: <webobjects-dev-off@wocommunity.org>
To switch to the DIGEST mode, E-mail to <webobjects-dev-digest@wocommunity.org>
To switch to the INDEX mode, E-mail to <webobjects-dev-index@wocommunity.org>
Send administrative queries to  <webobjects-dev-request@wocommunity.org>


Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster