-->
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.  [ 2 posts ] 
Author Message
 Post subject: ManyToMany with extra columns - pb when inserting
PostPosted: Thu Jul 22, 2010 3:22 am 
Newbie

Joined: Mon Sep 21, 2009 2:51 am
Posts: 5
Hi everybody,

I have a database with two table "Item" and "Order" which are associated by a third table "Item_Order" which store some additionnal information :

Table Item : Id Integer
Table Order : Id Integer
Table Item_Order : item_id Integer, order_id Integer, status String

I am using hibernate 3.3.2 GA and Spring.

My Java Object are :

Code:
@Entity
public class Item {

   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer id;
   
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
   @Cascade({org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private List<ItemOrder> orders= new LinkedList<ItemOrder>();

        // getter and setter
}


Code:
@Entity
public class Order {

   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer id;
   
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.order", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
   @Cascade({org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private List<ItemOrder> items = new LinkedList<ItemOrder>();

        // getter and setter
}


Code:
@Entity
@Table(name = "item_order")
@AssociationOverrides( { @AssociationOverride(name = "pk.item", joinColumns = @JoinColumn(name = "item_id")),
      @AssociationOverride(name = "pk.order", joinColumns = @JoinColumn(name = "order_id")) })
public class ItemOrder{

   private ItemOrderPk pk = new ItemOrderPk();

    @EmbeddedId
    private ItemOrderPk getPk() {
        return pk;
    }

    @Column (name = "status")
    private String status;
   
    @Transient
    public Article getItem() {  return getPk().getItem();    }

    @Transient
    public Commande getOrder() { return getPk().getOrder();  }

  // setter, hashCode and equals
}


Code:
@Embeddable
public class ItemOrderPk implements Serializable {

   private static final long serialVersionUID = -2700739856925530035L;
   
   private Item item;
    private Order order;

    @ManyToOne
    public Article getItem() { return item; }

    @ManyToOne
    public Commande getOrder() { return order; }

  // setter, hashCode and equals
}


My test function is :

Code:
@Test
public void createOrder() {

      Order order = new Order();
      order.setFoo("bar");
      
      Item item = new Item();
      item.setBar("foo");
      
      ItemOrder assoc = new ItemOrder();
      assoc.setItem(item);
      assoc.setOrder(order);
      assoc.setStatus(foo.bar);

      ArrayList<ItemOrder> assocs = new ArrayList<ItemOrder>();
      assocs.add(assoc);
      order.setItem(assocs);

//      ItemManager.addItem(item);

      OrderManager.addOrder(order);
}


OrderManager.addOrder will do :
Code:
hibernateTemplate.saveOrUpdate(order);


My problem is with this code, only order is inserting in the database.
If I uncomment ItemManager.addItem(item); item is inserting in the database.

But I have no solution to have the association information (in table Item_Order) inserted in the database.

What is wrong in my code ? What to do to have the assocation information inserted in the database ?

Thanks


Top
 Profile  
 
 Post subject: Re: ManyToMany with extra columns - pb when inserting
PostPosted: Thu Sep 16, 2010 11:42 am 
Newbie

Joined: Thu Sep 16, 2010 9:35 am
Posts: 2
Location: Germany.Berlin
Hi,

may, you are still locking for a solution ...

please visit http://en.wikibooks.org/wiki/Java_Persi ... al_Columns


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.