-->
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: Item - Bid with a successfulBid association
PostPosted: Wed Jun 10, 2009 10:11 am 
Newbie

Joined: Wed Feb 25, 2009 8:48 am
Posts: 5
Hi,

I am trying to get the example from "Java Persistence with Hibernate" (2007 edition) to run: an Item can have zero, one, or more Bids and it can have a successfulBid.

Here's my approach:

Code:
package org.hbf.basics.hibernate;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@ContextConfiguration
public class HibernateTest extends AbstractTestNGSpringContextTests
{
  @Test()
  public void bidsTest()
  {
    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

    Item item = new Item();
    Bid bid = new Bid();

    bid.setItem(item);
    item.getBids().add(bid);
    item.setSuccessfulBid(bid);

    session.save(item);
    session.save(bid);

    transaction.commit();
    session.close();
  }

  @Entity
  public static class Item
  {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @OneToMany(mappedBy = "item")
    private final Collection<Bid> bids = new ArrayList<Bid>();

    @OneToOne(optional = true)
    @JoinColumn(unique = true, nullable = true, updatable = false)
    private Bid successfulBid;

    public void setSuccessfulBid(Bid successfulBid)
    {
      this.successfulBid = successfulBid;
    }

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

  @Entity
  public static class Bid
  {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @ManyToOne()
    @JoinColumn(name = "item", nullable = false)
    private Item item;

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


This runs without exceptions but strangely, there the 'successfulBid_id' field contains NULL in the DB even though I call setSuccessfulBid(bid). Any ideas what the problem is?

Thanks a lot,
Hbf


Top
 Profile  
 
 Post subject: Re: Item - Bid with a successfulBid association
PostPosted: Fri Jun 12, 2009 5:55 am 
Newbie

Joined: Wed Feb 25, 2009 8:48 am
Posts: 5
I found code for this example (see Item.java and Bid.java) and have isolated the strange behaviour further.

To-0/1 works but To-1 does not
The following mapping in class Item,

Code:
    @OneToMany(mappedBy = "item")
    private final Collection<Bid> bids = new ArrayList<Bid>();

    @ManyToOne
    @JoinColumn(nullable = true)
    private Bid successfulBid;


makes the code work. However, I have a slightly different use case in which there always is and must be a successfulBid. So I set @JoinColumn(nullable = false) in the above. With this, I get an exception

not-null property references a null or transient value: org.hbf.basics.hibernate.HibernateTest$Item.successfulBid

Does anybody know why?

I also get this exception when I set @ManyToOne(optional=false).

Cascading does not work
Secondly, I want to use cascading and enable it on both (class Item):

Code:
    @OneToMany(mappedBy = "item", cascade = {
      CascadeType.ALL
    })
    private final Collection<Bid> bids = new ArrayList<Bid>();

    @ManyToOne(cascade = {
      CascadeType.ALL
    })
    @JoinColumn(nullable = true)
    private Bid successfulBid;


And in bidsTest() I disable the save of the child:

Code:
    session.save(item);
    //session.save(bid);


With this, I get

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.hbf.basics.hibernate.HibernateTest$Bid

Any hints much appreciated,
Hbf


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.