如何在Spring中使用Template Hibernate保存继承的类

时间:2012-01-03 01:03:43

标签: java hibernate spring

尝试使用TemplateHibernate保存继承的对象时遇到问题: 假设我有两个课程如下:   联系< --------- EntrepriseContact 这是contact.hbm.xml

   <joined-subclass name="Entreprise" table="Entreprise">
            <key column="ID_ENTREPRISE"  />
            <property name="numSiret">
                <column name="NUM_SIRET" />
            </property>
        </joined-subclass>

当我创建一个Entreprise对象并使用Hibernate保存它时,它可以正常工作

try
        {
            SessionFactory sessionFactory =
                new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            org.hibernate.Transaction tx = session.beginTransaction();

       ........................................

            session.saveOrUpdate(entreprise);
            tx.commit();

        }catch(Exception e){
            System.out.println(e.getMessage());
        }

但是当我尝试使用HibernateTemplate时,我收到以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: domain.Contact; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: domain.Contact

如何解决此问题?

请注意,当我尝试使用Contact时,我在两种情况下都工作过,但是对于Entreprise,它不起作用:(

1 个答案:

答案 0 :(得分:2)

查看Spring Source Documentation,查看如何使用hibernateTemplate。