-->
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: Transitive persistence problem using parent/child model
PostPosted: Sat Oct 03, 2009 2:46 am 
Newbie

Joined: Sat Oct 03, 2009 2:29 am
Posts: 1
Hi everybody
I have problems using parent/child model. I followed Bid-Item example from the book "Java Persistence with Hibernate". Can anybody help me out?

Here is the Bid class
Code:
@Entity
public class Bid implements Serializable {

  @Id
  @GeneratedValue
  private Long bid_id;
  private String name;

  @ManyToOne ( targetEntity=Item.class, cascade = { CascadeType.ALL })
  @JoinColumn(name="itemid", nullable=false)
  private Item item;

  public Item getItem() {
    return item;
  }

  public void setItem(Item item) {
    this.item = item;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}


And Item class

Code:
@Entity
public class Item implements Serializable {

  @Id
  @GeneratedValue
  private Long item_id;
  private String name;

  @OneToMany(mappedBy="item", cascade={CascadeType.PERSIST, CascadeType.MERGE})
  private Set<Bid> bids = new HashSet<Bid>();

  public Set<Bid> getBids() {
    return bids;
  }

  public void setBids(Set<Bid> bids) {
    this.bids = bids;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}


They are copied from page 260-269.

My test program is listed below.
Code:
    SessionFactory sFactory = config.buildSessionFactory();
    Session s = sFactory.openSession();
    s.beginTransaction();

    Item item = new Item();
    item.setName("item");
    Bid bid1 = new Bid();
    bid1.setName("bid1");

// In book, the following two lines is replaced by Item.addBid()
    bid1.setItem(item);
    item.getBids().add(bid1);

    s.save(item);
    s.flush();
    s.getTransaction().commit();
    s.close();


According to the book, bid1 should also be persisted. But in fact, I have only item record but no bid in database.

What's the problem here?

Thanks.


Top
 Profile  
 
 Post subject: Re: Transitive persistence problem using parent/child model
PostPosted: Sun Oct 04, 2009 7:44 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi FranklyDing,

FranklyDing wrote:
Code:
...
  @OneToMany(mappedBy="item", cascade={CascadeType.PERSIST, CascadeType.MERGE})
  private Set<Bid> bids = new HashSet<Bid>();
...

...
Code:
...
    s.save(item);
...


You should use cascade="save-update" for the annotation of the bids if you persist your entity with Session.save() (as the book says if you read carefully).

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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.