|
Subject:
|
Persisting Simple Association
|
|
Posted By:
|
aadz5
|
Post Date:
|
7/25/2008 1:11:14 PM
|
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){ stock.setBar(this); this.getStocks().add(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" > <hibernate-mapping> <class name="com.venture.hibernate.Bar" table="bar" > <id name="barID" type="java.lang.Integer" column="barID" > <generator class="increment" /> </id> <property name="barName" type="string"> <column name="bar_name" length="20"/> </property> <set name="stocks" inverse="true"> <key column="barID"></key> <one-to-many class="com.venture.hibernate.Stock"/> </set> </class> </hibernate-mapping>
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" > <hibernate-mapping> <class name="com.venture.hibernate.Stock" table="inventory" > <id name="stockID" type="java.lang.Integer" column="inventoryID" > <generator class="increment" /> </id> <property name="product" type="string" length="20"> <column name="productid"></column> </property> <property name="quantity" type="integer"> <column name="quantity"></column> </property> <many-to-one name="bar" column="barID" class="com.venture.hibernate.Bar" /> </class> </hibernate-mapping>
I try to persist a stock item adding the folowing:
public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Started"); SQLImpl sqlImpl = new SQLImpl(); Bar bar = new Bar(); bar.setBarName("Hello"); Stock stock = new Stock(); bar.addBar(stock); stock.setQuantity(10); stock.setBar(bar); stock.setUserName("Adnan"); sqlImpl.insertStockDetails(stock); System.out.println("Finished"); }
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?
Regards
Ads
Adz - Learning The J2EE Ways.
|
|
Reply By:
|
rakesh_mscit
|
Reply Date:
|
8/13/2008 3:49:16 AM
|
I think you can solve it by following the info. given in the bellow link
http://parasjain01.wordpress.com/2008/01/28/orghibernatetransientobjectexception-revisited/
- Rakesh
|