I have a helper class like this:
public class FtHelper {
Session session = null;
public FtHelper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public FinancialTransactions getByBeginDate(String beginDate) {
List<FinancialTransactions> FtList = null;
try {
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery(
"from FinancialTransactions where DATE=:date")
.setParameter("date", beginDate);
FtList = (List<FinancialTransactions>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return FtList.get(0);
}
}
I add a System.out.println(beginDate); just above return, but it doesn't appear to print anything. I also used System.out.println("anytext"), but it didn't work either. Where can I see that output message in Netbeans? By the way, I get the value beginDate from my JSF.
You should be able to find the output (most of the time) in catalina.out.
Under Windows, you should be able to find it in the folder: <Tomcat Folder>\logs
Related
In the below code i want to make "OrgLoginId" case insensitive. For example if OrgLoginId value is 'NSE' and in table org_loginid='Nse' then also I should get the result. Pls help me..
My Code:
public String getLoginId(String OrgLoginId) {
LOGGER.info("Inside getLoginId method : start");
List result=null;
String loginId=null;
try
{
org.hibernate.Session session= sessionFactory.openSession();
String SQL_QUERY = "select org_loginid from users where org_loginid='"+OrgLoginId+"'";
Query query = session.createSQLQuery(SQL_QUERY);
result=query.list();
loginId=(String)result.get(0);
}
catch (Exception e) {
return null;
}
return loginId;
}
1.9.0 i what to do this key-next trigger validation in java
i what something like this but in java adf
protected PreparedStatement createStatementADF(String query)
{
PreparedStatement statement=null;
try {
/*create transaction for current statemtnt*/
DBTransaction dbTransaction = (DBTransaction) this.getTransaction();
statement= dbTransaction.createPreparedStatement(query, 0);
} catch (SQLException e)
{
throw new JboException(e);
}
return statement;
}
/*Executes single query*/
protected ResultSet executeQueryADF (String query, Object[] bindVars)
{
PreparedStatement statement=null;
ResultSet ret=null;
try {
/*create transaction for current statemtnt and resuse it*/
statement=createStatementADF(query);
if ((bindVars != null)&&(statement!=null)) {
// 2. Loop over values for the bind variables passed in, if any
for (int z = 0; z < bindVars.length; z++) {
// 3. Set the value of each bind variable in the statement
statement.setObject(z + 1, bindVars[z]);
}
}
// 4. Execute the statement
ret=statement.executeQuery();
} catch (SQLException e) {
throw new JboException(e);
} finally {
if (statement != null) {
try {
// 5. Close the statement
statement.close();
} catch (SQLException e) {
}
}
}
return ret;
}
public void getUsrStatus()
{
ResultSet rs = null;
Object[] bindVars = new Object[]{"TestUser"};
try {
rs = executeQueryADF("SELECT account_status FROM dba_users WHERE username = ?", bindVars );
while(rs.next())
{
//..... process data
}
} catch (Exception e)
{
}
}
where i can use valueChangeListener,i what to validate the password and call the procedure to validate the password,i have to validate based on the enterd useid
public void labelListener(ValueChangeEvent valueChangeEvent)
{ UIComponent c = valueChangeEvent.getComponent();
//This step actually invokes Update Model phase for this
//component
c.processUpdates(FacesContext.getCurrentInstance());
//Jump to the Render Response phase in order to avoid
//the validation
FacesContext.getCurrentInstance().renderResponse();
}
Create a View Object (VO) which uses following query:
SELECT account_status
FROM dba_users
WHERE username = :p_username
Where p_username is a bind variable and add the VO to your Application Module (AM)
In your AM you will need to create a new method which gets the VO,
set the bind variable and execute the VO. Here you do the validations by checking the number of rows and see if the status etc is correct.
Depending on your use case you should expose the method in your AM (which probably returns a boolean or string). Add that method to your bindings and execute it when the user does some action (click a button). With the result of your method you could do some navigation or show an error. (Throwing an exception could also be a option).
Edit:
You can move your PL/SQL into a function/procedure and execute it from your AM. Very nice ex: http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
i have to update some rows in database using Hibernate and Struts2:
the method DAO where i put the requete is:
public void modifier(String cond) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
try{Query query = session.createQuery("Update Processus set selectionne = '1' where"+cond );
// query.setString("idproc",idprocessus);
// query.setLong("idsi", identifiantsi);
}catch(HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
}
In my action class where i call the DAO, i specify the cond:
public String update(){
cond="id_processus="+checked;
procdao.modifier(cond);
return SUCCESS;
}
can u help me it doens't show any error in the console but the row's value don't change!!!!
Following code could be helpful: Processus Table name selectionne and idproc are column name
You need to execute the query
To check the number of updated rows.
public Boolean modifier(String cond) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Boolean returnValue = false;
try {
Query query = session.createQuery("Update Processus set selectionne = '1' where idproc=:cond");
query.setString("cond", cond);
int noOfUpdate = query.executeUpdate();
returnValue = (noOfUpdate > 0);
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return returnValue;
}
I am trying to run an update query which would look like this in sql:
update studentMaster set sess_status = 'G' where ACADEM_YEAR = COURSE_YEAR;
I am trying to re-create the query using Criteria like this:
public void updateSessionStatus() {
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
Criteria crit = sess.createCriteria(CollegeStudentsMaster.class);
crit.add(Restrictions.eqProperty("academicYear", "courseYears"));
CollegeStudentsMaster e = (CollegeStudentsMaster) crit.uniqueResult();
e.setSessionStatus("G");
sess.saveOrUpdate(e);
tx.commit();
} catch (HibernateException asd) {
if (tx != null) {
tx.rollback();
}
log.debug(asd.getMessage());
} finally {
sess.close();
}
}
This is not working because the rows which meet this Criteria are many, my unique result is the problem here I guess.
How can I convert this into an update for all the rows that meet the Criteria. I do not want to use HQL query, I am rather doing it with Criteria.
public void updateSessionStatus() {
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
Criteria crit = sess.createCriteria(CollegeStudentsMaster.class);
crit.add(Restrictions.eqProperty("academicYear", "courseYears"));
// Here is updated code
ScrollableResults items = crit.scroll();
int count=0;
while ( items.next() ) {
CollegeStudentsMaster e = (CollegeStudentsMaster)items.get(0);
e.setSessionStatus("G");
sess.saveOrUpdate(e);
if ( ++count % 100 == 0 ) {
sess.flush();
sess.clear();
}
}
tx.commit();
} catch (HibernateException asd) {
if (tx != null) {
tx.rollback();
}
log.debug(asd.getMessage());
} finally {
sess.close();
}
}
It is always suggested that execute bulk operations very close to database and we do not need keep updated object in session unless they are required, Hence try to avoid load objects in session while executing bulk operations.
The following query was generated by hibernate
Select sum(B.c_rejected_count) as y0_,B.c_date as y1_,B.c_node_site_id as y2_,
A.c_sccp_addr as y3_ from tb_sccp_addr A inner join tb_dm_sccp_rej_day B on
A.c_sccp_addr_id=B.c_sccp_addr_fk group by A.c_date, A.c_node_site_id,
B.c_sccp_addr, B.c_sccp_addr_id ;
My Beanclass has following data:
private Date date;
private long rejectedCount;
private int nodeSiteId;
private String sccpAddress;
when I cast the resultant list of select query to my beanclass, I get a cast exception
java.lang.ClassCastException
Please identify what is going wrong and how to solve it
I am using Java reflection
//function initialises select query
private void initializeSelectQuery()
{
log(Level.DEBUG, "Entering initializeSelectQuery");
Class classname = beanclass.getMappedClass();
crit = hibernateSession.createCriteria(classname);
if(group.getAggregate().equals("SUM"))
{
projectionList.add(Projections.sum(beanDefinition
.getPropertyForColumn(group.getColumnName())));
}
else
{
projectionList.add(Projections.groupProperty(beanDefinition
.getPropertyForColumn(group.getColumnName())));
}
crit.setProjection(projectionList);
crit.setMaxResults(10);
}
public List executeSelectQuery() {
log(Level.DEBUG, "Entering executeSelectQuery");
List datalist = null;
try {
tx = hibernateSession.beginTransaction();
if (crit != null) {
datalist = crit.list();
}
tx.commit();
} catch (MappingException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (HibernateException e) {
e.printStackTrace();
}
log(Level.DEBUG, "Exiting executeSelectQuery");
return datalist;
}
You don't seem to use any result transformer (such as AliasToBeanResultTransformer) for your query. So, since it uses a projection list, your query returns a List<Object[]>. there is no way Hibernate can know that it must return instances of your bean class. Either you transform the List<Object[]> into what you want explicitely, or you use a ResultTransformer.