-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Custom sql-insert not working
PostPosted: Mon Apr 23, 2007 7:57 pm 
Newbie

Joined: Sat Jan 27, 2007 2:03 pm
Posts: 6
Location: Dallas, TX - USA
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

_________________
--
Rubens Gomes
www.rubens-gomes.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.