Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Hibernate
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Hibernate section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 25th, 2008, 01:11 PM
Friend of Wrox
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Persisting Simple Association

Hi Guys,

I have two tables stock and bar, I want to associate a bar to every stock. I have a Bar class:

public class Bar {

    private int barID;
    private String barName;
    private Address address;
    private Set stocks = new HashSet();

    public Set getStocks() {
        return stocks;
    public void setStocks(Set stocks) {
        this.stocks = stocks;
    public Address getAddress() {
        return address;
    public void setAddress(Address address) {
        this.address = address;
    public int getBarID() {
        return barID;
    public void setBarID(int barID) {
        this.barID = barID;
    public String getBarName() {
        return barName;
    public void setBarName(String barName) {
        this.barName = barName;
    public void addBar(Stock stock){

and a bar descriptor:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

        <class name="com.venture.hibernate.Bar" table="bar" >
          <id name="barID" type="java.lang.Integer" column="barID" >
                <generator class="increment" />
          <property name="barName" type="string">
              <column name="bar_name" length="20"/>

          <set name="stocks" inverse="true">
              <key column="barID"></key>
              <one-to-many class="com.venture.hibernate.Stock"/>

and a stock class:

public class Stock {

    private int stockID;
    private int productID;
    private int quantity;
    private Set stock = new HashSet();
    private Bar bar = new Bar();
    private Product product = new Product();
    private Date date;
    private String userName;

    public String getUserName() {
        return userName;
    public void setUserName(String userName) {
        this.userName = userName;
    public Date getDate() {
        return date;
    public void setDate(Date date) {
        this.date = date;
    public int getProductID() {
        return productID;
    public void setProductID(int productID) {
        this.productID = productID;
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    public int getStockID() {
        return stockID;
    public void setStockID(int stockID) {
        this.stockID = stockID;
    public int getQuantity() {
        return quantity;
    public Bar getBar() {
        return bar;
    public void setBar(Bar bar) {
        this.bar = bar;
    public Product getProduct() {
        return product;
    public void setProduct(Product product) {
        this.product = product;
    public Set getStock() {
        return stock;
    public void setStock(Set stock) {
        this.stock = stock;


and a stock descriptor:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

        <class name="com.venture.hibernate.Stock" table="inventory" >
          <id name="stockID" type="java.lang.Integer" column="inventoryID" >
                <generator class="increment" />
          <property name="product" type="string" length="20">
                <column name="productid"></column>
          <property name="quantity" type="integer">
                <column name="quantity"></column>

          <many-to-one name="bar"

I try to persist a stock item adding the folowing:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        SQLImpl sqlImpl = new SQLImpl();
        Bar bar = new Bar();

        Stock stock = new Stock();

but I get the following error:

Exception in thread "main" org.hibernate.TransientObjectException: com.venture.hibernate.Bar

I have no idea why, could someone please point me in the right direction?



Adz - Learning The J2EE Ways.
Adz - Learning The J2EE Ways.
Old August 13th, 2008, 03:49 AM
Friend of Wrox
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post

I think you can solve it by following the info. given in the bellow link


- Rakesh

Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables persisting in memory Keith Smith Access VBA 2 February 12th, 2008 08:34 AM
Genre and Show objects association vyurik BOOK: Professional Java Development with the Spring Framework 0 July 6th, 2006 06:00 AM
persisting data in a web app badgolfer ASP.NET 1.0 and 1.1 Basics 3 October 20th, 2004 01:59 PM
Persisting viewstate between pages drettberg ASP.NET 1.0 and 1.1 Professional 3 September 22nd, 2003 11:30 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.