Это моя конфигурация "springexample-hibernate.xml" xml
<bean id="exampleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/betatd" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="exampleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.cache.query_cache_factory">org.hibernate.cache.StandardQueryCacheFactory</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
</props>
</property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="exampleSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="exampleDataSource">
<!--<ref local="exampleDataSource"/>
--></property>
<property name="hibernateProperties">
<ref bean="exampleHibernateProperties" />
</property>
<!-- Must references all OR mapping files. -->
<property name="mappingResources">
<list>
<value>spring/Customer.hbm.xml</value>
<value>spring/Account.hbm.xml</value>
</list>
</property>
</bean>
<!-- Pass the session factory to our UserDAO -->
<bean id="customerDAOTarget" class="springexample.hibernate.CustomerDAOImpl">
<property name="sessionFactory">
<ref local="exampleSessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="exampleSessionFactory"/>
</property>
</bean>
<bean id="userDAO"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="customerDAOTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="addCustomer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Код, в котором хранятся Клиент и Аккаунт
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
"spring/springexample-hibernate.xml"
});
Customer customer = new Customer();
customer.setEmail("test@email.com");
customer.setUserId("V");
customer.setPassword("pwd");
customer.setFirstName("tom");
customer.setLastName("jerry");
Account acc = new Account();
acc.setAccountName("testaccnt");
acc.setType("T");
acc.setCreateDate(new Date());
acc.setUpdateDate(new Date());
acc.setBalance(new Double(500.00));
customer.addAccount(acc);
CustomerDAOImpl customerDAOImpl = (CustomerDAOImpl) appContext.getBean("customerDAOTarget");
customerDAOImpl.addCustomer(customer);
CustomerDAOImpl.java класс
public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO{
public void addCustomer(Customer customer) {
getHibernateTemplate().save(customer);
// TODO Auto-generated method stub
}
}
Файл Account.hbm
<id name="id" column="ACCOUNT_ID" type="java.lang.Long"
unsaved-value="-1">
<generator class="native">
</generator>
</id>
<many-to-one name="customer" column="CUSTOMER_ID"
class="springexample.hibernate.Customer" not-null="true" />
<property name="accountName" type="string" update="false"
insert="true" column="ACCOUNT_NAME" length="50" not-null="true" />
<property name="type" type="string" update="false" insert="true"
column="ACCOUNT_TYPE" length="1" not-null="true" />
<property name="createDate" type="date" update="false"
insert="true" column="CREATE_DATE" not-null="true" />
<property name="updateDate" type="date" update="true" insert="true"
not-null="true" column="UPDATE_DATE" />
<property name="balance" type="double" update="true" insert="true"
column="ACCOUNT_BALANCE" not-null="true" />
</class>
Customer.hbm
<id name="id" column="CUSTOMER_ID" type="java.lang.Long"
unsaved-value="-1">
<generator class="native">
</generator>
</id>
<set name="accounts" inverse="true" cascade="all-delete-orphan">
<key column="CUSTOMER_ID" />
<one-to-many class="springexample.hibernate.Account" />
</set>
<property name="email" type="string" update="false" insert="true"
column="CUSTOMER_EMAIL" length="82" not-null="true" />
<property name="password" type="string" update="false"
insert="true" column="CUSTOMER_PASSWORD" length="10" not-null="true" />
<property name="userId" type="string" update="false" insert="true"
column="CUSTOMER_USERID" length="12" not-null="true" unique="true" />
<property name="firstName" type="string" update="false"
insert="true" column="CUSTOMER_FIRSTNAME" length="25" not-null="true" />
<property name="lastName" type="string" update="false"
insert="true" column="CUSTOMER_LASTTNAME" length="25" not-null="true" />
</class>
Мой вопрос: теперь он работает без транзакций.Поскольку даже после того, как клиент вставлен в базу данных в то время, когда возникает исключение, данные не добавляются в таблицу счетов.В этот момент я хочу удалить запись в таблице клиентов.Что я должен сделать, чтобы выполнить это?