Muthu
Muthu

Reputation: 1550

Hibernate Many to Many Bi-Directional Storing

I followed the Mkyong's (Many-to-Many example – join table + extra column) from mkyong

When I run the application, I am able to store the many to many values

Many To Many Bi-Directional storing from Stock :

session.beginTransaction();

Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
//new category, need save to get the id first
session.save(category1);

Stock stock = new Stock();
stock.setStockCode("7052");
stock.setStockName("PADINI");

StockCategory stockCategory = new StockCategory();

stockCategory.setStock(stock);
stockCategory.setCategory(category1);
stockCategory.setCreatedDate(new Date());
stockCategory.setCreatedBy("system");

stock.getStockCategories().add(stockCategory);

session.save(stock);

session.getTransaction().commit();

It stores Category and uses its id with Stock. It is inserting into three tables . Worrking fine.. It generates sql as follows

Hibernate: 
    insert 
    into
        hibernateLearning.category
        (`DESC`, NAME) 
    values
        (?, ?)

Hibernate: 
    insert 
    into
        hibernateLearning.stock
        (STOCK_CODE, STOCK_NAME) 
    values
        (?, ?)

Hibernate: 
    insert 
    into
        hibernateLearning.stock_category
        (CREATED_BY, CREATED_DATE, CATEGORY_ID, STOCK_ID) 
    values
        (?, ?, ?, ?)

Many Many Bi-Directional storing from Category :

I modified the code and tried to store the stock values from Category as follows :

session.beginTransaction();
// Saved the Stock first
Stock stock = new Stock();
stock.setStockCode("7052");
stock.setStockName("PADINI");
session.save(stock);

// Saving the Category with Stock
Category category1 = new Category("CONSUMER1", "CONSUMER COMPANY");
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock);
stockCategory.setCategory(category1);
stockCategory.setCreatedDate(new Date());
stockCategory.setCreatedBy("system");

category1.getStockCategories().add(stockCategory);
session.save(category1);

session.getTransaction().commit();

This codes generates the following query :

Hibernate: 
    insert 
    into
        hibernateLearning.stock
        (STOCK_CODE, STOCK_NAME) 
    values
        (?, ?)


Hibernate: 
    insert 
    into
        hibernateLearning.category
        (`DESC`, NAME) 
    values
        (?, ?)

I save the stock first and used it with Category. This code create two tables and inserts the values. But the joining table(StockCategory) is missing. I am wondering where I am wrong in this code ?

Upvotes: 0

Views: 65

Answers (1)

JB Nizet
JB Nizet

Reputation: 691785

What is wrong is that you modify only one side of the bidirectional association, and, in the second example, the modified side is the inverse side (i.e. the side which has the mappedBy attribute). Hibernate ignores the inverse side. What matters for Hibernate is the owner side. It's thus normal and expected that the association is not saved.

Upvotes: 1

Related Questions