1

I'm having a hard time to figure this thing out. I have managed to create this type of mapping using annotations, but XML configuration is giving me headache for hours. These are my POJOs (getters ans setters are omitted):

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

And those are hibernate mapping files:

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

Schema seems to be good but I'm getting:

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

when I'm trying to save data.

This is the code which saves data:

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();
3
  • Your POJOs have bi-directional mapping (ie your Stock class has a reference to StockCategory, and StockCategory has a reference to Stock) this is not bad however XML mapping can not handle this without causing infinite recursion. I am not familiar with Hibernate but googling "hibernate.TransientObjectException" got me this link stackoverflow.com/questions/1044913/… Commented Mar 25, 2014 at 14:46
  • Is this the problem with XML configuration only? I have successfully implemented the same example using annotations. Commented Mar 25, 2014 at 14:59
  • Hi did you figure this out, I need to do the same thing but all the tutorials show annotation based solutions Commented May 23, 2021 at 23:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.