【问题标题】:How can i insert a row using hibernate method?如何使用休眠方法插入一行?
【发布时间】:2013-03-18 13:35:05
【问题描述】:

我正在尝试在关系表 Stock Category 中插入一行。

我正在关注这个例子:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

现在我已经有了表 stock 和 category 的数据。

稍后我想将股票和类别相互关联。

如何在不编写自定义 sql 查询的情况下做到这一点?

如果我可以像这样添加 StockCategory 可以吗?

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

提前致谢。

【问题讨论】:

  • 教程中没有描述吗?
  • 等待。我正在检查代码
  • 在哪里有一些像 nullable = false 这样的字段。您需要为这些字段设置值

标签: java hibernate hibernate-mapping hibernate-annotations


【解决方案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(stock);

它也在那里

【讨论】:

  • 如果我有其他的 stock 和 category 字段为 null 。还能用吗?
  • 如果你指定的字段不为空,它会抛出异常。如果它们可以为空,没问题。
  • @Thinker 在示例中没有指定为 nullable = false。所以默认情况下它们是可以为空的。所以你没问题
  • @Thinker 你现在明白了吗
  • 不,你明白我的意思。我有一个新股票,id11。新类别的 ID 为12。现在这两个链接在 stockcategory table 中没有一行。然后我只想在stockcategory 表中插入一个新行。我不想更新用户。
【解决方案2】:

像 Hibernate 这样的 ORM 将 Java 对象映射到数据源并创建此数据的模型,然后创建和更新对象并调用保存子例程来更新模型。插入/更新/删除 SQL 命令由 ORM 库完成。

所以在创建新对象的示例中,直到调用session.save(stock) 后数据源才会更新。

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

【讨论】:

  • 这正是我想知道的。
【解决方案3】:

只要您定义了适当的关系,您的代码就可以工作。 例如 - 如果您的 StockCategory.java 看起来像这样,那么您正在做的事情将起作用。

Class StockCategory{

     @ManyToOne(...)
     private Stock stock;

     @ManyToOne(...)
     private Category category;
}

那么下面的代码就可以工作了。您不必填充 Stock 和 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 );

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多