1

我很难弄清楚这件事。我已经设法使用注释创建了这种类型的映射,但是 XML 配置让我头疼了好几个小时。这些是我的 POJO(省略了 getter 和 setter):

    public class Stock implements Serializable {
    private static final long serialVersionUID = 8724254922362918916L;

    private int stockId;

    private String stockCode;

    private String stockName;

    private Set<StockCategory> stockCategories;
}

public class Category implements Serializable {
    private static final long serialVersionUID = -3719693878172387644L;

    private int categoryId;

    private String name;

    private String desc;

    private Set<StockCategory> stockCategories;
}

public final class StockCategoryId implements Serializable {
    private static final long serialVersionUID = -6894097209574496516L;

    private int stockId;
    private int categoryId;
}

public class StockCategory implements Serializable {
    private static final long serialVersionUID = 691323506089277225L;

    private StockCategoryId stockCategoryId;

    private Stock stock;

    private Category category;

    private Date createdDate;

    private String createdBy;
}

这些是休眠映射文件:

<class name="com.rmilovic.hibernate.examples.models.Stock" table="STOCK">
        <id name="stockId" column="STOCK_ID" type="int">
            <generator class="identity" />
        </id>
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
        </property>
    <property name="stockName" type="string">
            <column name="STOCK_NAME" length="10" not-null="true" unique="true" />
    </property>
    <set name="stockCategories" table="STOCK_CATEGORY" inverse="true"
        lazy="true" fetch="select" cascade="all">
    <key column="STOCK_ID" not-null="true" />
        <one-to-many class="com.rmilovic.hibernate.examples.models.StockCategory" />
    </set>
</class>

<class name="com.rmilovic.hibernate.examples.models.Category" table="CATEGORY">
    <id name="categoryId" column="CATEGORY_ID" type="int">
        <generator class="identity" />
    </id>
    <property name="name" type="string">
        <column name="NAME" not-null="true" length="10" unique="true" />
    </property>
    <property name="desc" type="string">
        <column name="DESC" not-null="true" />
    </property>
    <set name="stockCategories" table="STOCK_CATEGORY" lazy="true" inverse="true"
        fetch="select" cascade="all">
        <key column="CATEGORY_ID" not-null="true" />
        <one-to-many class="com.rmilovic.hibernate.examples.models.StockCategory" />
    </set>
</class>
<class name="com.rmilovic.hibernate.examples.models.StockCategory" table="STOCK_CATEGORY">
    <composite-id name="stockCategoryId"
        class="com.rmilovic.hibernate.examples.models.StockCategoryId">
        <key-property name="stockId" type="int" column="STOCK_ID" />
        <key-property name="categoryId" type="int" column="CATEGORY_ID" />
    </composite-id>
    <property name="createdDate" type="date">
        <column name="CREATED_DATE" not-null="true" length="10" unique="true" />
    </property>
        <property name="createdBy" type="string">
        <column name="CREATED_BY" not-null="true" length="10" unique="true" />
    </property>
    <many-to-one name="stock" column="STOCK_ID"
        class="com.rmilovic.hibernate.examples.models.Stock"
        insert="false" update="false" />
    <many-to-one name="category" column="CATEGORY_ID"
        class="com.rmilovic.hibernate.examples.models.Category"
        insert="false" update="false" />
</class>

架构似乎很好,但我得到:

Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rmilovic.hibernate.examples.models.Category

当我试图保存数据时。

这是保存数据的代码:

final Session session = HibernateUtilities.openSession();
session.beginTransaction();

final Stock stock = new Stock("BLA", "Test");
final Category category = new Category("consumer", "consumer company");

final StockCategory stockCategory = new StockCategory(new Date(), "me");
stockCategory.setCategory(category);
stockCategory.setStock(stock);

stock.getStockCategories().add(stockCategory);
category.getStockCategories().add(stockCategory);

session.save(stock);
session.getTransaction().commit();

HibernateUtilities.closeSession();
4

0 回答 0