3

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.

3
  • 4
    Didn't it describe in the tutorial? Commented Mar 18, 2013 at 13:36
  • wait .I am checking the code Commented Mar 18, 2013 at 14:20
  • where are some fields like nullable = false.You need to set values to those Commented Mar 18, 2013 at 14:23

3 Answers 3

6
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

Sign up to request clarification or add additional context in comments.

6 Comments

If I have other fields of stock and category as null . Will it still works?
if you specified the fields are not null it will throw exception.If they are nullable no problem.
@Thinker Did not specified in the example as nullable = false.So by default they are nullable.So no problem for u
@Thinker did you understand now
No.You dint get my point. I have a new Stock having id as 11. And new Category having id as 12. Now these two link does not have a row in stockcategory table . Then I just want to insert a new row in stockcategory table. I dont want to update user.
|
2

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();

1 Comment

This is exactly what I wanted to know.
0

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.