Hibernate version: 3.2.3
Spring framework version: 2.0.4
MySQL version: 5.0.22
My custom SQL Insert statement is not running. Instead, hibernate chooses to use an Identity default insert statement shown below.
I am using hibernate tags from xDoclet version 1.2.3 inside the java classes to generate the Hibernate XML mappings. In order to use MySQL Password() function, I added the following hibernate-properties-User.xml in xDoclet merge directory:
Code:
<sql-insert>
INSERT INTO USER ( VERSION, USER_ROLE_ID, LOGINID, EMAIL,
LAST_LOGIN_TIME, NEW_ACCOUNT_EMAIL_TIME,
NEW_ACCOUNT_EMAIL_ATTEMPT_TIME, PASSWORD, ACTIVE,
NEW_ACCOUNT_EMAIL_SENT ) VALUES ( ?, ?, ?, ?, ?, ?, ?, PASSWORD(?), ?,
? )
</sql-insert>
<sql-update>
UPDATE USER SET VERSION=?, USER_ROLE_ID=?, LOGINID=?, EMAIL=?,
LAST_LOGIN_TIME=?, NEW_ACCOUNT_EMAIL_TIME=?,
NEW_ACCOUNT_EMAIL_ATTEMPT_TIME=?, NEW_ACCOUNT_EMAIL_SENT=?, ACTIVE=?,
PASSWORD=PASSWORD(?) WHERE ID=?
</sql-update>
Mapping documents:Here is the xDoclet generated User.html.xml mapping file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="rg.domain.model.User"
table="USER"
>
<id
name="id"
column="ID"
type="long"
unsaved-value="null"
>
<generator class="identity">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<version
name="version"
column="VERSION"
type="long"
/>
<many-to-one
name="userRole"
class="rg.domain.model.UserRole"
cascade="none"
outer-join="auto"
update="true"
insert="true"
foreign-key="USER_FK"
column="USER_ROLE_ID"
not-null="true"
/>
<property
name="loginId"
type="string"
update="true"
insert="true"
column="LOGINID"
length="200"
not-null="true"
unique="true"
/>
<property
name="email"
type="string"
update="true"
insert="true"
column="EMAIL"
length="200"
not-null="true"
unique="true"
/>
<property
name="lastLoginTime"
type="calendar"
update="true"
insert="true"
column="LAST_LOGIN_TIME"
not-null="false"
/>
<property
name="newAccountEmailTime"
type="calendar"
update="true"
insert="true"
column="NEW_ACCOUNT_EMAIL_TIME"
not-null="false"
/>
<property
name="newAccountEmailAttemptTime"
type="calendar"
update="true"
insert="true"
column="NEW_ACCOUNT_EMAIL_ATTEMPT_TIME"
not-null="false"
/>
<property
name="password"
type="string"
update="true"
insert="true"
column="PASSWORD"
length="200"
not-null="true"
/>
<property
name="active"
type="boolean"
update="true"
insert="true"
column="ACTIVE"
not-null="true"
/>
<property
name="newAccountEmailSent"
type="boolean"
update="true"
insert="true"
column="NEW_ACCOUNT_EMAIL_SENT"
not-null="false"
/>
<sql-insert>
INSERT INTO USER ( VERSION, USER_ROLE_ID, LOGINID, EMAIL,
LAST_LOGIN_TIME, NEW_ACCOUNT_EMAIL_TIME,
NEW_ACCOUNT_EMAIL_ATTEMPT_TIME, PASSWORD, ACTIVE,
NEW_ACCOUNT_EMAIL_SENT ) VALUES ( ?, ?, ?, ?, ?, ?, ?, PASSWORD(?), ?,
? )
</sql-insert>
<sql-update>
UPDATE USER SET VERSION=?, USER_ROLE_ID=?, LOGINID=?, EMAIL=?,
LAST_LOGIN_TIME=?, NEW_ACCOUNT_EMAIL_TIME=?,
NEW_ACCOUNT_EMAIL_ATTEMPT_TIME=?, NEW_ACCOUNT_EMAIL_SENT=?, ACTIVE=?,
PASSWORD=PASSWORD(?) WHERE ID=?
</sql-update>
</class>
</hibernate-mapping>
Name and version of the database you are using: MySQL 5
The generated SQL (show_sql=true):In the logs, I see the following Insert statements for "DEBUG [org.hibernate.persister.entity.AbstractEntityPersister]"
Code:
Insert 0: INSERT INTO USER ( VERSION, USER_ROLE_ID, LOGINID, EMAIL, LAST_LOGIN_TIME, NEW_ACCOUNT_EMAIL_TIME, NEW_ACCOUNT_EMAIL_ATTEMPT_TIME, PASSWORD, ACTIVE, NEW_ACCOUNT_EMAIL_SENT ) VALUES ( ?, ?, ?, ?, ?, ?, ?, PASSWORD(?), ?, ? )
Code:
Identity insert: insert into USER (VERSION, USER_ROLE_ID, LOGINID, EMAIL, LAST_LOGIN_TIME, NEW_ACCOUNT_EMAIL_TIME, NEW_ACCOUNT_EMAIL_ATTEMPT_TIME, PASSWORD, ACTIVE, NEW_ACCOUNT_EMAIL_SENT) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
When the following code is called:
Code:
User user = new User();
user.setLoginId(loginId);
user.setPassword(password);
user.setEmail(email);
user.setUserRole(userRole);
user.setActive(false);
Long userId = (Long) this.getHibernateTemplate().save(user);
Hibernate issues the following insert command:
Code:
18:15:04,015 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
18:15:04,015 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
18:15:04,015 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
18:15:04,062 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:15:04,062 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:15:04,062 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:15:04,078 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
18:15:04,078 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
18:15:04,078 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
Hibernate: insert into USER (VERSION, USER_ROLE_ID, LOGINID, EMAIL, LAST_LOGIN_TIME, NEW_ACCOUNT_EMAIL_TIME, NEW_ACCOUNT_EMAIL_ATTEMPT_TIME, PASSWORD, ACTIVE, NEW_ACCOUNT_EMAIL_SENT) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
18:15:04,203 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
18:15:04,203 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
18:15:04,203 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
18:15:04,218 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
18