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();
}
}