I am using Hibernate 2.1.5 with MySQL 4.0.14-max, running in Resin 3.0.7. Any pointers for the following situation would be appreciated.
Rows are added to a database table using Hibernate. I can query the table and see the data I just entered through my form. However, when I reload my webapp, the data disappears.
Configuration
Code:
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.datasource">java:comp/env/jdbc/mysql-upkeep</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- Mapping files -->
<mapping resource="com/jsoup/upkeep/core/Address.hbm.xml" />
<mapping resource="com/jsoup/upkeep/core/DirectoryCategory.hbm.xml" />
<mapping resource="com/jsoup/upkeep/core/DirectoryEntry.hbm.xml" />
<mapping resource="com/jsoup/upkeep/core/UserAccount.hbm.xml" />
</session-factory>
Mapping documents:Code:
<class name="UserAccount" table="ACCOUNT">
<id name="id" type="long" unsaved-value="null">
<column name="ACCOUNT_ID" not-null="true" unique="true"/>
<generator class="native"/>
</id>
<property name="userId" column="USER_ID" not-null="true" unique="true" index="USER_ID_IDX"/>
<property name="password" column="PASSWORD" not-null="true"/>
<property name="contactName" column="CONTACT_NAME" not-null="true"/>
<property name="email" column="EMAIL_ADDRESS" not-null="true" index="EMAIL_IDX" />
<many-to-one name="mailingAddress" cascade="all">
<column name="MAILING_ADDRESS_ID"/>
</many-to-one>
<many-to-one name="billingAddress" cascade="all">
<column name="BILLING_ADDRESS_ID"/>
</many-to-one>
<property name="newsletterOk" column="NEWSLETTER_OK" not-null="true"/>
<property name="thirdPartyOk" column="THIRD_PARTY_OK" not-null="true"/>
<property name="agreedToTermsOn" column="AGREED_TO_TERMS_ON"/>
<property name="status" column="STATUS_ID" type="com.jsoup.upkeep.core.AccountStatus"/>
<property name="createdOn" column="CREATED_ON" not-null="true"/>
</class>
Code between sessionFactory.openSession() and session.close():Code:
public class AccountController {
private static Log log = LogFactory.getLog(AccountController.class);
private Session session = null;
private Transaction tx = null;
public AccountController() throws UpkeepException {
initialize();
}
public UserAccount createAccount(UserAccount account) throws UpkeepException {
log.debug("Trying to create account: " + account);
if (account.getId() != null) {
log.warn("UserAccount object passed in already has an ID");
}
try {
// make sure user ID is not already taken
List results = session.find("from UserAccount as ua where ua.userId = ?",
account.getUserId(), Hibernate.STRING);
log.debug("Found " + results.size() + " matches");
if (results.size() == 0) {
// create account
account.setCreatedOn(new Date());
session.save(account);
} else {
log.info("Failed to create account due to unavailable user ID "
+ account);
throw new UserIdUnavailableException();
}
tx.commit();
} catch (HibernateException e) {
log.error("Could not create user account", e);
throw new UpkeepException("Failed to create account due to system error");
} finally {
closeAll();
}
log.info("Account created: " + account);
return account;
}
public void recordAgreementWithTerms(Long accountId) throws UpkeepException {
log.debug("Called recordAgreementWithTerms");
try {
// get user account and set agreed to terms to now
UserAccount account = (UserAccount) session.get(UserAccount.class, accountId);
if (account != null) {
account.setAgreedToTermsOn(new Date());
session.update(account);
tx.commit();
} else {
log.warn("UserAccount " + accountId + " not found.");
}
} catch (HibernateException e) {
log.error("Could not set agreed to terms date", e);
throw new UpkeepException("Failed to set agreed to terms date due to system error.");
} finally {
closeAll();
}
log.info("Account " + accountId + " agreed to terms");
}
private void initialize() throws UpkeepException {
try {
if (session == null) {
session = HibernateUtil.currentSession();
}
if (tx == null) {
tx = session.beginTransaction();
}
} catch (HibernateException e) {
log.error("Unable to get Hibernate session or start transaction");
throw new UpkeepException("Problem initializing Hibernate");
}
}
private void closeAll() {
try {
HibernateUtil.closeSession();
} catch (HibernateException e) {
log.warn("Unable to close Hibernate session", e);
}
}
}