简体   繁体   中英

How can i insert a row using hibernate method?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM