-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: ManyToMany JoinTable issue
PostPosted: Thu Feb 14, 2008 7:05 am 
Newbie

Joined: Thu Feb 14, 2008 6:51 am
Posts: 1
Hi,

I have a ManyToMany relation between a Product and Order class for a eCommerce application :

Product :

Code:
@Entity
@Table(name="PRODUCTS")
public class Product implements Serializable{
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int productId;
   private float price;
   private String brand;
   private int quantity;
   
   @ManyToMany(mappedBy="products")
   private List<Order> orders;
        ...
}


Order :

Code:
@Entity
@Table(name="ORDERS")
public class Order implements Serializable{
   
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int orderId;
   
   @ManyToMany(fetch=FetchType.EAGER)
   @Fetch(FetchMode.SUBSELECT)
     @JoinTable(name="OrdersProducts",
              joinColumns=@JoinColumn(name="orderId",           referencedColumnName="orderId"),
              inverseJoinColumns=@JoinColumn(name="productId", referencedColumnName="productId") )
        
   private List<Product> products;

        .....


It works fine, but in my join table I only have the productId and orderId, I would like to have a "quantity" field. Is it possible to map a new field in a join table with an attribute from a collection ? Or is there a pattern to handle my problem ?

Thanx
Best regards,

John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 11:45 am 
Newbie

Joined: Thu Feb 14, 2008 11:40 am
Posts: 1
Hi,

hum... I think that is possible, and I will be interested too but I don't know how to do that.

I hope someone will figure it out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 2:40 pm 
Newbie

Joined: Thu Feb 14, 2008 2:36 pm
Posts: 2
I would also like to know this and would also be interested in finding out from experts on How to create a primary key on the join table ( using annotations) ?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 9:32 pm 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
Create a Join table class like in this case ProductOrder;

create an inner Id class (static) if you have composite Id ( i.e. Primary key(id_1, id_2) ), then in the containing class ( that is class that contains Id, which here is ProductOrder ) declare the fields as you see fit.

ProductOrder has MANY-TO-ONE association with Product and MANY-TO-ONE association with Order, so you would have that in your mapping files as well as in your ProductOrder class, just make sure that you manage foreign key relationships neatly.

I can give an example, but if you have JP with Hibernate, theres an example like that given, you can refer to it.

Regards
Vyas,Anirudh

_________________
Regards,
Vyas, Anirudh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 7:26 am 
Newbie

Joined: Thu Feb 14, 2008 2:36 pm
Posts: 2
Hi ,
Can you please give an example ?


Top
 Profile  
 
 Post subject: Modified Example from the book
PostPosted: Thu Feb 21, 2008 7:14 pm 
Newbie

Joined: Tue Feb 19, 2008 10:25 pm
Posts: 5
This is modified from listing 7.1, pg 304, in Java Persistence with Hibernate...

Code:
@Entity
@Table(name="ordered_lines")
public class OrderLine
{
    @Embeddable
    public static class Id
        implements Serializable
    {
        @Column(name="order_id")
        private Long orderId;

        @Column(name="product_id")
        private Long productId;

        public Id() {}

        public Id(Long orderId, Long productId)
        {
            this.orderId = orderId;
            this.productId = productId;
        }

        public boolean equals(Object o)
        {
            // compare this and that Id properties (orderIds and productIds)
        }

        public int hashCode() {
            // add the orderId and productId hashCode()s
        }
    }

    @EmbeddedId
    private Id id = new Id();

    @ManyToOne
    @JoinColumn(
        name="order_id",
        insertable="false",
        updatable="false"
    )
    private Order order;

    @ManyToOne
    @JoinColumn(
        name="product_id",
        insertable="false",
        updatable="false"
    )
    private Product product;

    // create attributes you want to save in the join table

    public OrderedLine() {}

    public OrderedLine(
        Order order,
        Product product
        /* other attributes */)
    {
        this.id.orderId = order.getId();
        this.id.productId = product.getId();

        // these are two one-to-many relationships mapped
        // one in each of Order and Product
        order.getOrderLines().add(this);
        order.getOrderLines().add(this);

        // set other attributes
    }

    // accessor methods
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 8:19 pm 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
read my this post for the example ( the only thing that is missing from the mapping was cascade="save-update" from ProductParts table

http://forum.hibernate.org/viewtopic.php?t=983932

_________________
Regards,
Vyas, Anirudh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.