Reputation: 6892
I am trying to insert a row into a relation table Stock Category.
I am following this example: http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
Now I already have data in table stock and category.
Later I want to associate a stock and category to each other.
How I can do this without writing a custom sql query?
Is it possible if I can add StockCategory like this?
Stock stock = new Stock();
stock.setStockId(1);
Category category = new Category();
category.setCategoryId(1);
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock); //here you need to get the stock object by id
stockCategory.setCategory(category1); //here you need to get the category1 object by id
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stockCategory );
Thanks in advance.
Upvotes: 3
Views: 28208
Reputation: 40318
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock); //here you need to get the stock object by id
stockCategory.setCategory(category1); //here you need to get the category1 object by id
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stock);
It is also there
Upvotes: 6
Reputation: 2425
As long as you define appropriate relationships, your code will work. For example - if your StockCategory.java looks something like this, then what you are doing will work.
Class StockCategory{
@ManyToOne(...)
private Stock stock;
@ManyToOne(...)
private Category category;
}
Then the following code will work. You don't have to populate other fields in Stock and Category.
Stock stock = new Stock();
stock.setStockId(1);
Category category = new Category();
category.setCategoryId(1);
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock);
stockCategory.setCategory(category1);
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stockCategory );
Upvotes: 0
Reputation: 671
An ORM like Hibernate map Java objects to the datasource and create a model of this data, then you create and update the objects and call a save subroutine to update the model. The Insert/Update/Delete SQL commands are done by the ORM library.
So in the example of creating a new object, the datasource is not updated until session.save(stock)
is called.
session.beginTransaction();
Stock stock = new Stock();
stock.setStockCode("7052");
stock.setStockName("PADINI");
//assume category id is 7
Category category1 = (Category)session.get(Category.class, 7);
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock);
stockCategory.setCategory(category1);
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
stock.getStockCategories().add(stockCategory);
session.save(stock);
session.getTransaction().commit();
Upvotes: 2