• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to insert a current date into the databse colunm using hybernate ?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all

first i want to elaborate scenario
1) i have one table which name is user_maintenance the table description is like this

create table USER_MAINTENANCE
(
USER_ID VARCHAR2(256) not null,
USER_PASS VARCHAR2(256),
USER_NAME VARCHAR2(256),
USER_BRANCH VARCHAR2(256),
USER_EMAIL VARCHAR2(256),
USER_STARTDATE DATE,
USER_EXPIRYDATE DATE,
LAST_LOGIN DATE,
CREATION_DATE DATE,
CREATED_BY VARCHAR2(256),
LAST_MODIFIED_DATE DATE,
LAST_MODIFIED_BY VARCHAR2(256),
DISABLE_FLAG CHAR(1) default 'N'
)

[color=darkred][color=red] comment on column USER_MAINTENANCE.LAST_LOGIN
is 'Last successful login date';
[/color][/color]

2) now i want to give the details of the hbm file of this table

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 13, 2011 1:15:43 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.tra.Generate.UserMaintenance" table="USER_MAINTENANCE" schema="RAKESH">
<comment>User Maintenance table</comment>
<id name="userId" type="string">
<column name="USER_ID" length="256" />
<generator class="assigned" />
</id>
<property name="userPass" type="string">
<column name="USER_PASS" length="256">
<comment>Encrypted password</comment>
</column>
</property>
<property name="userName" type="string">
<column name="USER_NAME" length="256">
<comment>User Name</comment>
</column>
</property>
<property name="userBranch" type="string">
<column name="USER_BRANCH" length="256">
<comment>User Branch</comment>
</column>
</property>
<property name="userEmail" type="string">
<column name="USER_EMAIL" length="256">
<comment>Email address of user</comment>
</column>
</property>
<property name="userStartdate" type="date">
<column name="USER_STARTDATE" length="7">
<comment>Start date of user</comment>
</column>
</property>
<property name="userExpirydate" type="date">
<column name="USER_EXPIRYDATE" length="7">
<comment>Expiry date of user</comment>
</column>
</property>
<property name="lastLogin" type="date" formula="(select sysdate from dual)" >
<column name="LAST_LOGIN" length="7">
<comment>Last successful login date</comment>
</column>
</property>
<property name="creationDate" type="date">
<column name="CREATION_DATE" length="7">
<comment>User creation date</comment>
</column>
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="256">
<comment>User id of the user who created this user</comment>
</column>
</property>
<property name="lastModifiedDate" type="date">
<column name="LAST_MODIFIED_DATE" length="7">
<comment>Last modification date</comment>
</column>
</property>
<property name="lastModifiedBy" type="string">
<column name="LAST_MODIFIED_BY" length="256">
<comment>User id of the user who modified this user</comment>
</column>
</property>
<property name="disableFlag" type="java.lang.Character">
<column name="DISABLE_FLAG" length="1">
<comment>Set value to Y for disabling user</comment>
</column>
</property>
<set name="traCurrentUserses" inverse="true">
<key>
<column name="USER_ID" length="256" not-null="true" unique="true">
<comment>Logged in user id</comment>
</column>
</key>
<one-to-many class="com.tra.Generate.TraCurrentUsers" />
</set>
<set name="traRoleses" inverse="false" table="TRA_USER_ROLES" lazy="false" >
<key>
<column name="USER_ID" length="256" not-null="true">
<comment>User id</comment>
</column>
</key>
<many-to-many entity-name="com.tra.Generate.TraRoles" >
<column name="ROLE_ID" length="20" not-null="true">
<comment>Role id</comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>


3) now i want to give the description of the pojo file of this table
@Entity
@Table(name="USER_MAINTENANCE"
,schema="RAKESH"
)
public class UserMaintenance implements java.io.Serializable {


private String userId;
private String userPass;
private String userName;
private String userBranch;
private String userEmail;
private Date userStartdate;
private Date userExpirydate;
private Date lastLogin;
private Date creationDate;
private String createdBy;
private Date lastModifiedDate;
private String lastModifiedBy;
private Character disableFlag;
private Set traCurrentUserses = new HashSet(0);
//because of one to many to many relationship
private Set<TraRoles> traRoleses = new HashSet<TraRoles>(0);


public UserMaintenance() {
}


public UserMaintenance(String userId) {
this.userId = userId;
}
public UserMaintenance(String userId, String userPass, String userName, String userBranch, String userEmail, Date userStartdate, Date userExpirydate, Date lastLogin, Date creationDate, String createdBy, Date lastModifiedDate, String lastModifiedBy, Character disableFlag, Set traCurrentUserses, Set traRoleses) {
this.userId = userId;
this.userPass = userPass;
this.userName = userName;
this.userBranch = userBranch;
this.userEmail = userEmail;
this.userStartdate = userStartdate;
this.userExpirydate = userExpirydate;
this.lastLogin = lastLogin;
this.creationDate = creationDate;
this.createdBy = createdBy;
this.lastModifiedDate = lastModifiedDate;
this.lastModifiedBy = lastModifiedBy;
this.disableFlag = disableFlag;
this.traCurrentUserses = traCurrentUserses;
this.traRoleses = traRoleses;
}

@Id

@Column(name="USER_ID", unique=true, nullable=false, length=256)
public String getUserId() {
return this.userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

@Column(name="USER_PASS", length=256)
public String getUserPass() {
return this.userPass;
}

public void setUserPass(String userPass) {
this.userPass = userPass;
}

@Column(name="USER_NAME", length=256)
public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Column(name="USER_BRANCH", length=256)
public String getUserBranch() {
return this.userBranch;
}

public void setUserBranch(String userBranch) {
this.userBranch = userBranch;
}

@Column(name="USER_EMAIL", length=256)
public String getUserEmail() {
return this.userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
@Temporal(TemporalType.DATE)
@Column(name="USER_STARTDATE", length=7)
public Date getUserStartdate() {
return this.userStartdate;
}

public void setUserStartdate(Date userStartdate) {
this.userStartdate = userStartdate;
}
@Temporal(TemporalType.DATE)
@Column(name="USER_EXPIRYDATE", length=7)
public Date getUserExpirydate() {
return this.userExpirydate;
}

public void setUserExpirydate(Date userExpirydate) {
this.userExpirydate = userExpirydate;
}
@Temporal(TemporalType.DATE)
@Column(name="LAST_LOGIN", length=7)
public Date getLastLogin() {
return this.lastLogin;
}

public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
@Temporal(TemporalType.DATE)
@Column(name="CREATION_DATE", length=7)
public Date getCreationDate() {
return this.creationDate;
}

public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}

@Column(name="CREATED_BY", length=256)
public String getCreatedBy() {
return this.createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
@Temporal(TemporalType.DATE)
@Column(name="LAST_MODIFIED_DATE", length=7)
public Date getLastModifiedDate() {
return this.lastModifiedDate;
}

public void setLastModifiedDate(Date lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}

@Column(name="LAST_MODIFIED_BY", length=256)
public String getLastModifiedBy() {
return this.lastModifiedBy;
}

public void setLastModifiedBy(String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}

@Column(name="DISABLE_FLAG", length=1)
public Character getDisableFlag() {
return this.disableFlag;
}

public void setDisableFlag(Character disableFlag) {
this.disableFlag = disableFlag;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userMaintenance")
public Set getTraCurrentUserses() {
return this.traCurrentUserses;
}

public void setTraCurrentUserses(Set traCurrentUserses) {
this.traCurrentUserses = traCurrentUserses;
}
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER )
@JoinTable(name="TRA_USER_ROLES", schema="RAKESH", joinColumns = {
@JoinColumn(name="USER_ID", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="ROLE_ID", nullable=false, updatable=false) })
public Set<TraRoles> getTraRoleses() {
return this.traRoleses;
}

public void setTraRoleses(Set<TraRoles> traRoleses) {
this.traRoleses = traRoleses;
}




}


4---) so here initially lastlogin colunm take the value as last login date by using the
<property name="lastLogin" type="date" formula="(select sysdate from dual)" >
<column name="LAST_LOGIN" length="7">
<comment>Last successful login date</comment>
</column>
</property>


5) but this value is not seen into the databse table colunm , i want when user is able to login the succesfully then this date is automaticaaly save into the databse

so for this what i have done earlier is like this

a) make a userdto

public class UserDTO {

//--------------------------------------------------------------------------
/**
* This variable stores the userid
*/
public String userId;
//--------------------------------------------------------------------------
/**
* This variable stores the user password
*/
public String userPass;
//--------------------------------------------------------------------------
/**
* This variable stores the user name
*/
public String userName;
//--------------------------------------------------------------------------
/**
* This variable stores the user's branch
*/
public String userBranch;
//--------------------------------------------------------------------------
/**
* This variable stores the user's email address
*/
public String userEmail;
//--------------------------------------------------------------------------
/**
* This variable stores the user's start date
*/
public Date userStartDate;
//--------------------------------------------------------------------------
/**
* This variable store the user expiry date
*/
public Date userExpiryDate;
//--------------------------------------------------------------------------
/**
* true if user is expired
*/
public Date lastlogin;
public boolean isExpired;
// creation date of the user
public Date creation_date;
// this user is created by
public String created_by;
//the last modified date
public Date lastmodified_date;
//modified by
public String modified_by;
//shows the eligibility of user by default it eligible
public char disable_flag;
// this variable holds all the roles related to the particular role_id
public String[] role;

public boolean notadmin;
/**
* Default no-arg constructor
*/
public UserDTO() {
}
//--------------------------------------------------------------------------
}

// it defines the query
private static String USER_QUERY =
"FROM UserMaintenance"
+ " WHERE user_id=?"
;


//function for update data

public List getUpdateData(Class className[], String query, Object p_value, Type p_type) {
List list = null;
try {

getSession(className);
list = sess.createQuery(query).setParameter(0,p_value).list();
startTransaction();

} catch (org.hibernate.HibernateException e) {
log.error("getUpdateData::Exception",e);
}
return list;
}

//function for seesion.save
public void updateData(Object classobject) {
try {
log.info("for updating");
sess.saveOrUpdate(classobject);
log.info("after updating");
endTransaction(SUCCESS);
log.info("after commit");
} catch (HibernateException e) {
log.error("updateData::Exception");
log.error(e.getMessage(),e);
endTransaction(FAILURE);
} finally {
log.debug("updateData::finally");

}
}


//now we use these two function in this
public LoginResponseDTO changeLastlogin(UserDTO p_request) {

LoginResponseDTO responseDTO = new LoginResponseDTO();
TransactionMngr txnMngr = new TransactionMngr(THISCOMPONENT);

Class[] classes = {
UserMaintenance.class,TraCurrentUsers.class,TraProjects.class,TraProjectsData.class,TraProjectModules.class,TraDataTypes.class,TraGlobalData.class,TraRoles.class,TraRoleFunctions.class
};

//copy the request dto to response
// responseDTO.request = p_request;

try {
List l_list = txnMngr.getUpdateData(
classes,
USER_QUERY,
p_request.userId,
TransactionHelper.getObjectType(p_request.userId));

if (l_list.isEmpty()) {
responseDTO.result = ApiHelper.addErrorMessage(
ApiConstants.USER_ID_DOESNOTEXIST,
responseDTO.result);
} else {
log.info("going for updating process is: ");
Iterator<UserMaintenance> iter = l_list.iterator();
while (iter.hasNext()) {

UserMaintenance userMaintenance = iter.next();

Date lastlogindate = userMaintenance.getLastLogin();
log.info("lastlogindate is: "+userMaintenance.getLastLogin()); //////////////////////// this stataement print the value of todays date
userMaintenance.setLastLogin(lastlogindate); //now here set the lastlogin as current date
txnMngr.updateData(userMaintenance); //it is like update the fields in dTABASE BUT THIS IS VERY TROUBLESOME FOR ME THAT THIS IS NOT EXECUTED
break;//break loop after 1 execution
}
}

} catch (Exception ex) {
log.error("LASTLOGINDATE ::Exception:"+ex,ex);
responseDTO.result = ApiHelper.addErrorMessage(
ApiConstants.MID_SYSTEM_ERROR,
responseDTO.result);

}

return responseDTO;

}
//---------------


BUT THIS CURRENT DATE IS NOT GOING INTO THE DATABASE COLUNM PLEASE SUGGEST ME

I HAVE ALSO TRYIED IT WITH INSERT=FALSE AND UPDATE =FALSE IN HBM BUT IT DOES NOT WORK .

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic