Hibernate saveOrUpdate方法无法按预期工作

时间:2011-01-09 18:22:45

标签: hibernate

学习Hibernate并且它真的让我疯了我试图坚持一个实体类和它的孩子们,这里是为我做这个的方法

log.info("About to persist the given Entity instance");
        Session session=HibernateSessionFactory.getSessionfactory().openSession();
        Transaction tx=session.getTransaction();
        try{
            tx.begin();
            session.saveOrUpdate(entity);
            tx.commit();
        }
        catch(Exception e){
            tx.rollback();
        }
        finally{
            session.close();
        }
        return entity;
    }
一切似乎都没问题,这里是控制台输出我完全没有hibernate没有显示任何异常也没有在数据库中保存任何东西

23:47:08,627  INFO GenericDAOImpl:172 - About to persist the given Entity instance
2011-01-09 23:47:08,627 [http-8080-3] INFO  com.raisonne.tr.dao.impl.GenericDAOImpl - About to persist the given Entity instance
9 Jan, 2011 11:47:08 PM org.hibernate.impl.SessionFactoryImpl close
INFO: closing
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:mysql://localhost/travellingrants
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/travellingrants
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****}
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.51-community
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.13 ( Revision: ${bzr.revision-id} )
9 Jan, 2011 11:47:08 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
9 Jan, 2011 11:47:08 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
9 Jan, 2011 11:47:08 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default schema: travellingrants
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: enabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
9 Jan, 2011 11:47:08 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory createRegionFactory
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
9 Jan, 2011 11:47:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
9 Jan, 2011 11:47:08 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
9 Jan, 2011 11:47:08 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured

任何一个对hiberntae有所了解的人都可以告诉我什么是错的,我可以看到一些可疑的日志内容

9 Jan, 2011 11:47:08 PM org.hibernate.impl.SessionFactoryImpl close
INFO: closing
9 Jan, 2011 11:47:08 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:mysql://localhost/travellingrants

但真的很无能为力

EDIT1 这是父映射文件

<hibernate-mapping>
    <class name="com.raisonne.tr.model.destination.Destination" table="DESTINATION">
       <id name="uuid" type="java.lang.String">
                 <column name="UUID" />
                 <generator class="uuid"/>
      </id>
        <property name="destinationID" type="java.lang.String" not-null="true">
            <column name="DESTINATIONID" />
        </property>
        <property name="name" type="java.lang.String" not-null="true">
            <column name="NAME" />
        </property>
        <property name="shortDescription" type="java.lang.String">
            <column name="SHORTDESCRIPTION" />
        </property>
        <property name="longDescription" type="java.lang.String">
            <column name="LONGDESCRIPTION" />
        </property>

        <set name="airTransport" table="AIRTRANSPORT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.destination.destinationattributes.AirTransport" />
        </set>
        <set name="roadTransport" table="ROADTRANSPORT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.destination.destinationattributes.RoadTransport" />
        </set>
        <set name="trainTransport" table="TRAINTRANSPORT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.destination.destinationattributes.TrainTransport" />
        </set>

        <set name="emergencyContact" table="EMERGENCYCONTACT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.destination.destinationattributes.EmergencyContact" />
        </set>

        <set name="placesToVisit" table="PLACETOVISIT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.destination.destinationattributes.PlaceToVisit" />
        </set>
        <set name="news" table="NEWS" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.news.News" />
        </set>
        <set name="userComments" table="USERCOMMENT" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.user.UserComment" />
        </set>
        <set name="address" table="BASICADDRESS" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.address.BasicAddress" />
        </set>
        <set name="categories" table="CATEGORY" inverse="true" lazy="true" cascade="save-update">
            <key>
                <column name="DESTINATIONID" />
            </key>
            <one-to-many class="com.raisonne.tr.model.category.Category" />
        </set>
    </class>
</hibernate-mapping>

和子映射文件的条目如下。

<hibernate-mapping>
    <class name="com.raisonne.tr.model.destination.destinationattributes.PlaceToVisit" table="PLACETOVISIT">
        <id name="uuid" type="java.lang.String">
            <column name="UUID" />
            <generator class="uuid" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <many-to-one name="destination" class="com.raisonne.tr.model.destination.Destination" fetch="join">
            <column name="DESTINATION" />
        </many-to-one>
        </class>
</hibernate-mapping>

2 个答案:

答案 0 :(得分:1)

尝试使用

重新补充代码中的catch
catch(Exception e){
            Logger.getAnonymousLogger().log(Level.SEVERE, e.getLocalizedMessage());
            tx.rollback();
}

答案 1 :(得分:0)

您失踪的是儿童关系中的'Casade'设置。

当hibernate保存一个新实体时,它会查找新的子对象并设置为save-update(稍后会挖出一个例子)。