Question about optionality in one-to-one relationships

2024-01-09 Thread Riccardo De Menna
Hi,

Can someone help me understand something?

I’m trying to model a one-to-one relationship between two entities but I can’t 
seem to get the relationship to be optional.

In my specific case I need to model an entity representing shipments with a 
postal service. Each shipment needs to have a number taken from a range/group 
that is pre-assigned by the postal service.

Thus I created a SHIPMENT_NUMBER entity with just an INTEGER attribute and then 
used that attribute to build the relationship with the SHIPMENT entity. 
Possibly with “To dep PK” as well.

I want the relationship to be optional so that I can generate SHIPMENT_NUMBER 
as many as I want and populate them with the numbers assigned by the postal 
service and only later, when the real SHIPMENT is actually needed/created, link 
it with the number in a one-to-one fashion.

I’m not sure why, but my class generated content always shows the relationship 
as mandatory.

Coming from the WebObjects world, I'm used to a modeler that explicitly shows 
checkboxes for isMandatory on relationships like with the attributes. Here in 
Cayenne it seems that optionality is implicitly determined based on the design.

Have I misunderstood something? Is my design flawed?

Any tip is appreciated.

Regards,
Riccardo

Re: Question about optionality in one-to-one relationships

2024-01-09 Thread Michael Gentry
Hi Riccardo,

I may have completely misunderstood your intention, but here is my first
cut for a model:

cayenne-o2o.xml:

http://cayenne.apache.org/schema/10/domain";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://cayenne.apache.org/schema/10/domain
https://cayenne.apache.org/schema/10/domain.xsd";
project-version="10">



datamap.map.xml:

http://cayenne.apache.org/schema/10/modelMap";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap
https://cayenne.apache.org/schema/10/modelMap.xsd";
project-version="10">







http://cayenne.apache.org/schema/10/info"; name=
"comment" value="This ID is the same as SHIPMENT's ID."/>


http://cayenne.apache.org/schema/10/info"; name=
"comment" value="This is the shipment number."/>
















Copy/Paste these files somewhere, then try loading them up into your 4.2
Modeler and see if it is close.

mrg


On Tue, Jan 9, 2024 at 9:54 AM Riccardo De Menna  wrote:

> Hi,
>
> Can someone help me understand something?
>
> I’m trying to model a one-to-one relationship between two entities but I
> can’t seem to get the relationship to be optional.
>
> In my specific case I need to model an entity representing shipments with
> a postal service. Each shipment needs to have a number taken from a
> range/group that is pre-assigned by the postal service.
>
> Thus I created a SHIPMENT_NUMBER entity with just an INTEGER attribute and
> then used that attribute to build the relationship with the SHIPMENT
> entity. Possibly with “To dep PK” as well.
>
> I want the relationship to be optional so that I can generate
> SHIPMENT_NUMBER as many as I want and populate them with the numbers
> assigned by the postal service and only later, when the real SHIPMENT is
> actually needed/created, link it with the number in a one-to-one fashion.
>
> I’m not sure why, but my class generated content always shows the
> relationship as mandatory.
>
> Coming from the WebObjects world, I'm used to a modeler that explicitly
> shows checkboxes for isMandatory on relationships like with the attributes.
> Here in Cayenne it seems that optionality is implicitly determined based on
> the design.
>
> Have I misunderstood something? Is my design flawed?
>
> Any tip is appreciated.
>
> Regards,
> Riccardo


Re: Question about optionality in one-to-one relationships

2024-01-09 Thread Riccardo De Menna
Hi Michael,

Thank you for helping. Yes… you modeled exactly what I described down to the 
delete rules. I imported your files in modeler and run class generation but I 
still get the relationship (TO_SHIP in your model) to appear as mandatory. 
Could it be an issue in class generation?

This is a little VE snippet I use in my template to output a static list of 
mandatory relationships.

#foreach( $rel in ${object.DeclaredRelationships} )
  #if (${rel.isMandatory()} && !${rel.ToMany} )
#set($bar = 
$requiredRelationships.add("${stringUtils.capitalizedAsConstant($rel.Name)}_PROPERTY"))
  #end
#end
public static final List REQUIRED_RELATIONSHIPS = List.of(
  #foreach( $rel in ${requiredRelationships} )
${rel}#if(!$foreach.last),#end
  #end
);

And contrary to what I would expect, I get this in my superclass:

public static final List REQUIRED_RELATIONSHIPS = List.of(
  TO_SHIP_PROPERTY
);

Riccardo De Menna


> On 10 Jan 2024, at 01:20, Michael Gentry  wrote:
> 
> Hi Riccardo,
> 
> I may have completely misunderstood your intention, but here is my first
> cut for a model:
> 
> cayenne-o2o.xml:
> 
> http://cayenne.apache.org/schema/10/domain";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:schemaLocation="http://cayenne.apache.org/schema/10/domain
> https://cayenne.apache.org/schema/10/domain.xsd";
> project-version="10">
> 
> 
> 
> datamap.map.xml:
> 
> http://cayenne.apache.org/schema/10/modelMap";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap
> https://cayenne.apache.org/schema/10/modelMap.xsd";
> project-version="10">
> 
> 
> 
>  />
> 
> 
> > 
> http://cayenne.apache.org/schema/10/info"; name=
> "comment" value="This ID is the same as SHIPMENT's ID."/>
> 
> 
> http://cayenne.apache.org/schema/10/info"; name=
> "comment" value="This is the shipment number."/>
> 
> 
>  "optimistic" dbEntityName="SHIPMENT"/>
>  lock-type="optimistic" dbEntityName="SHIPMENT_NUMBER">
> 
> 
> > 
> 
> 
>  toDependentPK="true">
> 
> 
>  deleteRule="Cascade" db-relationship-path="toShipNum"/>
>  deleteRule="Deny" db-relationship-path="toShip"/>
> 
> 
> Copy/Paste these files somewhere, then try loading them up into your 4.2
> Modeler and see if it is close.
> 
> mrg
> 
> 
> On Tue, Jan 9, 2024 at 9:54 AM Riccardo De Menna  wrote:
> 
>> Hi,
>> 
>> Can someone help me understand something?
>> 
>> I’m trying to model a one-to-one relationship between two entities but I
>> can’t seem to get the relationship to be optional.
>> 
>> In my specific case I need to model an entity representing shipments with
>> a postal service. Each shipment needs to have a number taken from a
>> range/group that is pre-assigned by the postal service.
>> 
>> Thus I created a SHIPMENT_NUMBER entity with just an INTEGER attribute and
>> then used that attribute to build the relationship with the SHIPMENT
>> entity. Possibly with “To dep PK” as well.
>> 
>> I want the relationship to be optional so that I can generate
>> SHIPMENT_NUMBER as many as I want and populate them with the numbers
>> assigned by the postal service and only later, when the real SHIPMENT is
>> actually needed/created, link it with the number in a one-to-one fashion.
>> 
>> I’m not sure why, but my class generated content always shows the
>> relationship as mandatory.
>> 
>> Coming from the WebObjects world, I'm used to a modeler that explicitly
>> shows checkboxes for isMandatory on relationships like with the attributes.
>> Here in Cayenne it seems that optionality is implicitly determined based on
>> the design.
>> 
>> Have I misunderstood something? Is my design flawed?
>> 
>> Any tip is appreciated.
>> 
>> Regards,
>> Riccardo