私はこれを理解するのに苦労しています。注釈を使用してこのタイプのマッピングを作成することはできましたが、XML 構成に何時間も悩まされていました。これらは私のPOJOです(ゲッターとセッターは省略されています):
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();