How to design such relationship? is it possible at all with caenne?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to design such relationship? is it possible at all with caenne?

Tomas Jucius
I have many-to-many relationship with additional field "discount" in
relational table..

This is scheme:

Table Order fields:      order_id, .......other fields....

Table OrderProduct:   order_id, product_id, discount

Table Product:           product_id, ......other fields......

When there is no "discount" when everything is fine..

How to design such relationship with cayenne.. Is it possible..

And if possible, when how I would have to add new product to orders
(how about discount value)?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to design such relationship? is it possible at all with caenne?

Gentry, Michael (Contractor)
Yes, it's possible.

You have Order <->> OrderProduct <<-> Product

Assuming you already have an "order" and an "product" in memory (in the
same DataContext):

DataContext dc = order.getDataContext();
OrderProduct orderProduct =
  (OrderProduct) dc.createAndRegisterNewObject(OrderProduct.class);

orderProduct.setOrder(order);
orderProduct.setProduct(product);
orderProduct.setDiscount(discount);

dc.commitChanges();


Be sure to setup your Cayenne model with the appropriate
relationships/attributes and it should work fine (it has for me before).
Of course, make sure your discount allows nulls so you can omit it if
not present (or set it to zero/etc).

/dev/mrg


-----Original Message-----
From: Tomas Jucius [mailto:[hidden email]]
Sent: Friday, August 12, 2005 4:14 PM
To: [hidden email]
Subject: How to design such relationship? is it possible at all with
caenne?


I have many-to-many relationship with additional field "discount" in
relational table..

This is scheme:

Table Order fields:      order_id, .......other fields....

Table OrderProduct:   order_id, product_id, discount

Table Product:           product_id, ......other fields......

When there is no "discount" when everything is fine..

How to design such relationship with cayenne.. Is it possible..

And if possible, when how I would have to add new product to orders
(how about discount value)?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to design such relationship? is it possible at all with caenne?

Joshua Pyle
In reply to this post by Tomas Jucius
its fairly simple but you will not be able to "flatten" the relationship.

On 8/12/05, Tomas Jucius <[hidden email]> wrote:

> I have many-to-many relationship with additional field "discount" in
> relational table..
>
> This is scheme:
>
> Table Order fields:      order_id, .......other fields....
>
> Table OrderProduct:   order_id, product_id, discount
>
> Table Product:           product_id, ......other fields......
>
> When there is no "discount" when everything is fine..
>
> How to design such relationship with cayenne.. Is it possible..
>
> And if possible, when how I would have to add new product to orders
> (how about discount value)?
>


--
Joshua T. Pyle
Go has always existed.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to design such relationship? is it possible at all with caenne?

Tomas Jucius
In reply to this post by Gentry, Michael (Contractor)
One more question..

How then easily you access discount value form product?

I dont see easy solution..

I would like something like.. --- product.getDiscount()..

How have you accessed these attributes in relational table?

On 8/12/05, Gentry, Michael (Contractor) <[hidden email]> wrote:

> Yes, it's possible.
>
> You have Order <->> OrderProduct <<-> Product
>
> Assuming you already have an "order" and an "product" in memory (in the
> same DataContext):
>
> DataContext dc = order.getDataContext();
> OrderProduct orderProduct =
>   (OrderProduct) dc.createAndRegisterNewObject(OrderProduct.class);
>
> orderProduct.setOrder(order);
> orderProduct.setProduct(product);
> orderProduct.setDiscount(discount);
>
> dc.commitChanges();
>
>
> Be sure to setup your Cayenne model with the appropriate
> relationships/attributes and it should work fine (it has for me before).
> Of course, make sure your discount allows nulls so you can omit it if
> not present (or set it to zero/etc).
>
> /dev/mrg
>
>
> -----Original Message-----
> From: Tomas Jucius [mailto:[hidden email]]
> Sent: Friday, August 12, 2005 4:14 PM
> To: [hidden email]
> Subject: How to design such relationship? is it possible at all with
> caenne?
>
>
> I have many-to-many relationship with additional field "discount" in
> relational table..
>
> This is scheme:
>
> Table Order fields:      order_id, .......other fields....
>
> Table OrderProduct:   order_id, product_id, discount
>
> Table Product:           product_id, ......other fields......
>
> When there is no "discount" when everything is fine..
>
> How to design such relationship with cayenne.. Is it possible..
>
> And if possible, when how I would have to add new product to orders
> (how about discount value)?
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to design such relationship? is it possible at all with caenne?

Tomas Jucius
In reply to this post by Tomas Jucius
You see I have gave this only as example.. I don't have product and
orders.. I have other objects..

But in relational table I need additional field.. ("discount" in example)..

There is no other way..

The thing is that every relation has to have additional one field..

Maybe I could design this relation in other way.. Maybe somehow with 4
talbes.. (Have such idea)..

So I shall think about this.. Really for now don't see any easy solution...

If you would find - please tell:)..



On 8/13/05, Gentry, Michael (Contractor) <[hidden email]> wrote:

> You can't access the discount easily from the Product.  Now that I'm
> thinking about it a little more, would it make more sense to have the
> discount on the Order and not the OrderProduct?  If the discount applies
> to all Products in the Order, it would make more sense to put it in the
> Order.  If the discount is for a specific Product in an Order, it makes
> sense to put it in the OrderProduct.
>
> If you do need the discount with the OrderProduct, I'd probaby write
> some helper methods in your Order subclass so you could say
> order.getDiscountForProduct(product) or something similar which would
> find the OrderProduct associated with the passed-in product and return
> the discount attribute for it.  I'd also write a helper methods so you
> could do order.getProducts(), too.  Probably several other useful
> methods could be added, too.  :-)
>
> /dev/mrg
>
>
> -----Original Message-----
> From: Tomas Jucius [mailto:[hidden email]]
> Sent: Friday, August 12, 2005 4:37 PM
> To: [hidden email]; Gentry, Michael (Contractor)
> Subject: Re: How to design such relationship? is it possible at all with
> caenne?
>
>
> One more question..
>
> How then easily you access discount value form product?
>
> I dont see easy solution..
>
> I would like something like.. --- product.getDiscount()..
>
> How have you accessed these attributes in relational table?
>
> On 8/12/05, Gentry, Michael (Contractor) <[hidden email]>
> wrote:
> > Yes, it's possible.
> >
> > You have Order <->> OrderProduct <<-> Product
> >
> > Assuming you already have an "order" and an "product" in memory (in
> the
> > same DataContext):
> >
> > DataContext dc = order.getDataContext();
> > OrderProduct orderProduct =
> >   (OrderProduct) dc.createAndRegisterNewObject(OrderProduct.class);
> >
> > orderProduct.setOrder(order);
> > orderProduct.setProduct(product);
> > orderProduct.setDiscount(discount);
> >
> > dc.commitChanges();
> >
> >
> > Be sure to setup your Cayenne model with the appropriate
> > relationships/attributes and it should work fine (it has for me
> before).
> > Of course, make sure your discount allows nulls so you can omit it if
> > not present (or set it to zero/etc).
> >
> > /dev/mrg
> >
> >
> > -----Original Message-----
> > From: Tomas Jucius [mailto:[hidden email]]
> > Sent: Friday, August 12, 2005 4:14 PM
> > To: [hidden email]
> > Subject: How to design such relationship? is it possible at all with
> > caenne?
> >
> >
> > I have many-to-many relationship with additional field "discount" in
> > relational table..
> >
> > This is scheme:
> >
> > Table Order fields:      order_id, .......other fields....
> >
> > Table OrderProduct:   order_id, product_id, discount
> >
> > Table Product:           product_id, ......other fields......
> >
> > When there is no "discount" when everything is fine..
> >
> > How to design such relationship with cayenne.. Is it possible..
> >
> > And if possible, when how I would have to add new product to orders
> > (how about discount value)?
> >
>
Loading...