I'm using Hibernate 3.3.2GA because I'm working on Weblogic 10.3.2 and it is only supports JPA 1.0. What I have is an EJB3 Session bean that gets the Entity Manager injected and I'm using CMT transactions as follows:
Code:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class PreferenceServiceImpl implements PreferenceServiceLocal,
PreferenceServiceRemote {
@PersistenceContext(unitName = "epfPreferencePU")
private EntityManager entityManager;
...
public void createPreference(Preference preference, EPFContext epfContext) {
if (preference == null) {
throw new EPFPreferenceBusinessException(new EPFMessage(
EPF_PREFERENCE_ERROR_100));
}
createPreference(userService.createAndOrGetUser(epfContext), applicationService.getApplication(epfContext), preference, epfContext);
}
private void createPreference(User user, Application app, Preference preference, EPFContext epfContext) {
if (preference == null) {
throw new EPFPreferenceBusinessException(new EPFMessage(
EPF_PREFERENCE_ERROR_100));
}
preference.setUser(user);
preference.setApplication(app);
preference.setPreferenceCategory(preferenceCategoryService
.getPreferenceCategory(preference.getPreferenceCategoryCode(),
epfContext));
Set<ConstraintViolation<Preference>> constraintViolations = validator
.validate(preference);
if (constraintViolations.size() > 0) {
createPreferenceBusinessException(constraintViolations);
}
user.getPreferences().add(preference);
logger.debug("User Preference Pre-Persist: {}", preference);
entityManager.persist(preference);
logger.debug("User Preference Post-Persist: {}", preference);
}
...
I also have the following persistence.xml file:
Code:
<persistence-unit name="epfPreferencePU"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>epfPreferenceDataSource</jta-data-source>
<class>com.demo.epf.preference.entity.Application</class>
<class>com.demo.epf.preference.entity.PreferenceDetail</class>
<class>com.demo.epf.preference.entity.Preference</class>
<class>com.demo.epf.preference.entity.User</class>
<class>com.demo.epf.preference.entity.PreferenceCategory</class>
<properties>
<property name="hibernate.current_session_context_class" value="jta" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
<property name="hibernate.transaction.auto_close_session" value="true"/>
<property name="hibernate.transaction.flush_before_completion" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
And I'm using orm.xml as follows:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
<mapped-superclass
class="com.demo.epf.preference.entity.PreferenceEntityBase">
<entity-listeners>
<entity-listener
class="com.demo.epf.preference.service.PreferenceEntityBaseListener">
<pre-persist method-name="prePersist" />
<pre-update method-name="preUpdate" />
</entity-listener>
</entity-listeners>
<attributes>
<basic name="createDate">
<column name="CRET_TS" />
<temporal>TIMESTAMP</temporal>
</basic>
<basic name="updateDate">
<column name="UPDT_TS" />
<temporal>TIMESTAMP</temporal>
</basic>
</attributes>
</mapped-superclass>
<entity class="com.demo.epf.preference.entity.Preference"
access="PROPERTY">
<table name="EPF_PREFNC" />
<sequence-generator name="EPF_PREF_SEQ"
allocation-size="1" sequence-name="PREFNC_ID_SEQ" initial-value="1" />
<attributes>
<id name="preferenceId">
<column name="PREFNC_ID" />
<generated-value strategy="SEQUENCE" generator="EPF_PREF_SEQ" />
</id>
<basic name="preferenceName">
<column name="USER_PREFNC_NM" />
</basic>
<basic name="preferenceDescription">
<column name="USER_PREFNC_DESC" />
</basic>
<basic name="displayOrderValue">
<column name="DSPL_ORD_NUM" />
</basic>
<version name="version">
<column name="UPDT_VERS_CNTL_NUM" />
</version>
<many-to-one name="user"
target-entity="com.demo.epf.preference.entity.User" fetch="EAGER"
optional="false">
<join-column name="USER_ID" nullable="false" insertable="false" updatable="false"/>
</many-to-one>
<many-to-one name="preferenceCategory"
target-entity="com.demo.epf.preference.entity.PreferenceCategory"
fetch="EAGER" optional="false">
<join-column name="PREFNC_CATG_CD" nullable="false"/>
</many-to-one>
<many-to-one name="application"
target-entity="com.demo.epf.preference.entity.Application"
fetch="EAGER" optional="false">
<join-column name="APPN_SYS_CD" nullable="false"/>
</many-to-one>
<one-to-many name="preferenceDetails" fetch="EAGER"
target-entity="com.demo.epf.preference.entity.PreferenceDetail"
mapped-by="preference">
<cascade>
<cascade-all />
</cascade>
</one-to-many>
<transient name="applicationSystemCode" />
<transient name="preferenceCategoryCode" />
</attributes>
</entity>
<entity class="com.demo.epf.preference.entity.PreferenceDetail"
access="PROPERTY">
<table name="EPF_PREFNC_DETL" />
<sequence-generator name="EPF_PREF_DETL_SEQ"
allocation-size="1" sequence-name="PREFNC_DETL_ID_SEQ" initial-value="1" />
<attributes>
<id name="preferenceDetailId">
<column name="PREFNC_DETL_ID" />
<generated-value strategy="SEQUENCE" generator="EPF_PREF_DETL_SEQ" />
</id>
<basic name="preferenceDetailDescription">
<column name="PREFNC_DETL_TXT" />
</basic>
<basic name="displayOrderValue">
<column name="DSPL_ORD_NUM" />
</basic>
<version name="version">
<column name="UPDT_VERS_CNTL_NUM" />
</version>
<many-to-one name="preference" fetch="EAGER">
<join-column name="PREFNC_ID" />
</many-to-one>
</attributes>
</entity>
<entity class="com.demo.epf.preference.entity.User"
access="PROPERTY">
<table name="EPF_USER" />
<attributes>
<id name="userId">
<column name="USER_ID" />
</id>
<basic name="userTypeCode">
<column name="USER_TYPE_CD" />
</basic>
<one-to-many name="preferences" fetch="LAZY"
target-entity="com.demo.epf.preference.entity.Preference">
<join-column name="USER_ID" nullable="false" insertable="true" updatable="true" />
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
</attributes>
</entity>
<entity class="com.demo.epf.preference.entity.PreferenceCategory"
access="PROPERTY">
<table name="EPF_PREFNC_CATG" />
<attributes>
<id name="preferenceCategoryCode">
<column name="PREFNC_CATG_CD" />
</id>
<basic name="preferenceCategoryDescription">
<column name="PREFNC_CATG_DESC" />
</basic>
</attributes>
</entity>
<entity class="com.demo.epf.preference.entity.Application"
access="PROPERTY">
<table name="EPF_APPN" />
<attributes>
<id name="applicationSystemCode">
<column name="APPN_SYS_CD" />
</id>
<basic name="applicationSystemName">
<column name="APPN_SYS_NM" />
</basic>
</attributes>
</entity>
</entity-mappings>
What I'm seeing is that when I execute the create method above, an insert is NEVER performed as here are the logged SQL statements:
Code:
Hibernate: select user0_.USER_ID as USER1_3_0_, user0_.CRET_TS as CRET2_3_0_, user0_.UPDT_TS as UPDT3_3_0_, user0_.USER_TYPE_CD as USER4_3_0_ from EPF_USER user0_ where user0_.USER_ID=?
Hibernate: select applicatio0_.APPN_SYS_CD as APPN1_0_0_, applicatio0_.CRET_TS as CRET2_0_0_, applicatio0_.UPDT_TS as UPDT3_0_0_, applicatio0_.APPN_SYS_NM as APPN4_0_0_ from EPF_APPN applicatio0_ where applicatio0_.APPN_SYS_CD=?
Hibernate: select preference0_.PREFNC_CATG_CD as PREFNC1_4_0_, preference0_.CRET_TS as CRET2_4_0_, preference0_.UPDT_TS as UPDT3_4_0_, preference0_.PREFNC_CATG_DESC as PREFNC4_4_0_ from EPF_PREFNC_CATG preference0_ where preference0_.PREFNC_CATG_CD=?
Hibernate: select preference0_.User_Id as User10_4_, preference0_.PREFNC_ID as PREFNC1_4_, preference0_.PREFNC_ID as PREFNC1_2_3_, preference0_.CRET_TS as CRET2_2_3_, preference0_.UPDT_TS as UPDT3_2_3_, preference0_.APPN_SYS_CD as APPN8_2_3_, preference0_.DSPL_ORD_NUM as DSPL4_2_3_, preference0_.PREFNC_CATG_CD as PREFNC9_2_3_, preference0_.USER_PREFNC_DESC as USER5_2_3_, preference0_.USER_PREFNC_NM as USER6_2_3_, preference0_.USER_ID as USER10_2_3_, preference0_.UPDT_VERS_CNTL_NUM as UPDT7_2_3_, applicatio1_.APPN_SYS_CD as APPN1_0_0_, applicatio1_.CRET_TS as CRET2_0_0_, applicatio1_.UPDT_TS as UPDT3_0_0_, applicatio1_.APPN_SYS_NM as APPN4_0_0_, preference2_.PREFNC_CATG_CD as PREFNC1_4_1_, preference2_.CRET_TS as CRET2_4_1_, preference2_.UPDT_TS as UPDT3_4_1_, preference2_.PREFNC_CATG_DESC as PREFNC4_4_1_, user3_.USER_ID as USER1_3_2_, user3_.CRET_TS as CRET2_3_2_, user3_.UPDT_TS as UPDT3_3_2_, user3_.USER_TYPE_CD as USER4_3_2_ from EPF_PREFNC preference0_ inner join EPF_APPN applicatio1_ on preference0_.APPN_SYS_CD=applicatio1_.APPN_SYS_CD inner join EPF_PREFNC_CATG preference2_ on preference0_.PREFNC_CATG_CD=preference2_.PREFNC_CATG_CD inner join EPF_USER user3_ on preference0_.USER_ID=user3_.USER_ID where preference0_.User_Id=?
Hibernate: select PREFNC_ID_SEQ.nextval from dual
Hibernate: select PREFNC_DETL_ID_SEQ.nextval from dual
So it appears as though the sequence is selected, but the record isn't inserted. The interesting thing is that if I run it as resource_local it works.
I've tried various combinations of settings but none of them have an effect. Any thoughts?