-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to embed "NOW()" into column?
PostPosted: Fri Dec 02, 2005 1:08 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Using XDoclet, I'd like to know how to force that SQL Server's "NOW()" operation is called on a specified column (see timestamp below) when inserting new rows. Is this even possible?


Hibernate version: 3.1

Mapping documents:

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="myapp.audit.user.UserLoginAudit"
        table="myapp_userloginaudit"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-UserLoginAudit.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

            <property
                name="username"
                type="java.lang.String"
                    update="true"
                    insert="true"
            column="username"
            length="25"
        />

            <property
                name="workstationId"
                type="java.lang.String"
                    update="true"
                    insert="true"
            column="workstationId"
            length="254"
        />

            <property
                name="timestamp"
                type="java.util.Date"
                    update="true"
                    insert="true"
            column="timestamp"
        />

            <property
                name="isSuccessful"
                type="java.lang.Boolean"
                    update="true"
                    insert="true"
            column="isSuccessful"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-UserLoginAudit.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
N/A

Full stack trace of any exception that occurs:
N/A

Name and version of the database you are using:
SQL Server 2000 SP4

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:
N/A


Class source code:
Code:
package myapp.audit.user;

import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
import myapp.db.HibernateIDObject;

/**
* Stores the all login attempts, whether successful or not.
*
* @author funkhous
* @version <pre>1.0</pre>
*
* @hibernate.class table="myapp_userloginaudit"
*/
public class UserLoginAudit extends HibernateIDObject {
   private static final Logger logger = Logger.getLogger(UserLoginAudit.class);
   private String username;
   private String workstationId;
   private Date timestamp;
   private Boolean isSuccessful;

   /**
    * Default constructor.
    */
   public UserLoginAudit() {
      // noop
   }

   /**
    * Explicit constructor.
    *
    * @param username Login ID used to login.
    * @param workstationId Workstation ID associated with login
    * @param timestamp Time of login attempt
    * @param isSuccessful If login was successful or not
    */
   public UserLoginAudit(String username, String workstationId, Date timestamp, Boolean isSuccessful) {
      this.username = username;
      this.workstationId = workstationId;
      this.timestamp = timestamp;
      this.isSuccessful = isSuccessful;
   }

   /**
    * Returns the username associated with a user login audit.
    *
    * @return Returns the username.
    *
    * @hibernate.property length="25"
    */
   public String getUsername() {
      return username;
   }

   /**
    * Sets the username associated with a user login audit.
    *
    * @param username The username to set.
    */
   public void setUsername(String username) {
      if (null != username && 25 < username.length()) {
         logger.error("Audit trimmed oversized user name:" + username);
         username = username.substring(0, 25);
      }
      this.username = username;
   }

   /**
    * Gets the workstation ID associated with a user login audit.
    *
    * @return Returns the workstationId.
    *
    * @hibernate.property length="254"
    */
   public String getWorkstationId() {
      if (null != workstationId && 254 < workstationId.length()) {
         logger.error("Audit trimmed oversized workstation ID:" + workstationId);
         workstationId = workstationId.substring(0, 254);
      }
      return workstationId;
   }

   /**
    * Sets the workstation ID associated with a user login audit.
    *
    * @param workstationId The workstationId to set.
    */
   public void setWorkstationId(String workstationId) {
      this.workstationId = workstationId;
   }

   /**
    * Gets the timestamp of the user login audit.
    *
    * @return Returns the timestamp.
    *
    * @hibernate.property
    */
   public Date getTimestamp() {
      return timestamp;
   }

   /**
    * Sets the timestamp of the user login audit.
    *
    * @param timestamp The timestamp to set.
    */
   public void setTimestamp(Date timestamp) {
      this.timestamp = timestamp;
   }

   /**
    * Determines if the login was successful.
    *
    * @return Returns the isSuccessful.
    *
    * @hibernate.property
    */
   public Boolean getIsSuccessful() {
      return isSuccessful;
   }

   /**
    * Set this to true if the login was successful.
    *
    * @param isSuccessful <code>true</code> if login was successful; <code>false</code> otherwise
    */
   public void setIsSuccessful(Boolean isSuccessful) {
      this.isSuccessful = isSuccessful;
   }

   /*
    *  (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   public boolean equals(Object obj) {
      boolean rv = false;
      if (this == obj) {
         rv = true;
      } else {
         if (obj instanceof UserLoginAudit) {
            UserLoginAudit other = (UserLoginAudit) obj;
            rv = new EqualsBuilder().append(getUsername(), other.getUsername()).append(getWorkstationId(),
                     other.getWorkstationId()).append(getTimestamp(), other.getTimestamp()).append(getIsSuccessful(),
                     other.getIsSuccessful()).isEquals();
         }
      }
      return rv;
   }

   /*
    *  (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   public int hashCode() {
      return new HashCodeBuilder(getMultiplierPrime(this), getNonZeroOddPrime(this)).append(getUsername()).append(
               getWorkstationId()).append(getTimestamp()).append(getIsSuccessful()).toHashCode();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Only on insert?

If you want it both on insert and update, you can use a generated <timestamp/> mapping:
Code:
<timestamp name="timestamp" column="timestamp" source="db"/>

No idea if you can do that through XDoclet, though.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:27 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
In what context of the hbm.xml file does the timestamp tag belong? (class? property? etc?)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:37 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Regarding:
Code:
<timestamp name="timestamp" column="timestamp" source="db"/>


According to the HBM DTD:

Code:
<!ELEMENT timestamp (meta*)>
   <!ATTLIST timestamp name CDATA #REQUIRED>
   <!ATTLIST timestamp node CDATA #IMPLIED>
   <!ATTLIST timestamp column CDATA #IMPLIED>
   <!ATTLIST timestamp access CDATA #IMPLIED>
   <!ATTLIST timestamp unsaved-value (null|undefined) "null">


So what exactly is the "source" attribute?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You said you were using 3.1?

Here is the DTD for 3.1:
Code:
<!ELEMENT timestamp (meta*)>
   <!ATTLIST timestamp name CDATA #REQUIRED>
   <!ATTLIST timestamp node CDATA #IMPLIED>
   <!ATTLIST timestamp column CDATA #IMPLIED>
   <!ATTLIST timestamp access CDATA #IMPLIED>
   <!ATTLIST timestamp unsaved-value (null|undefined) "null">
    <!ATTLIST timestamp source (vm|db) "vm">
    <!ATTLIST timestamp generated (never|always) "never">


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:02 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Ah, yes, you are correct. I was wrong though, I'm still using v3.0.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:50 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Where is the DTD v3.1 documented?


http://hibernate.sourceforge.net/hibernate-mapping-3.1.dtd does not exist.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:57 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Because we are nice guys we decided not to break the old one for 3.1:

http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd


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

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.