Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

pengpeng lin

Greenhorn
+ Follow
since Dec 24, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by pengpeng lin

I use spring class AbstractTransactionalDataSourceSpringContextTests to test my service logic, below is my testing code:

==========================================================
public void testSaveUser() {
User newUser = createUser();
try {
userService.saveUser(newUser);
assertTrue(true);
<b>setComplete();</b>
String sql = "Select USERNAME from NEWSLETTER_USER where UPPER(USERNAME)= ?";
String userName = (String) jdbcTemplate.queryForObject(sql,
new Object[] { "PAUL" }, java.lang.String.class);
assertNotNull(userName);
assertEquals(userName, newUser.getUserName());
} catch (NewsletterException ne) {
fail(ne.getMessage());
}
}

please note that the setComplete() call, I found that the save operation has succeed to insert a record to databae, and the sql: <b>Select USERNAME from NEWSLETTER_USER where UPPER(USERNAME)= 'PAUL'</b> can get the expected result while running in SQL*PLUS.

But when test run it failed at <b>assertNotNull(userName)</b>, the error message said that the jdbcTemplate.queryForObject method return a <b>null</b> object.

<b>So why can't the jdbcTemplate get the record even if transaction has been committed?</b>
Hi all, I have encounted a testing problem whit spring AbstractTransactionalDataSourceSpringContextTests class.

I test my service class whit spring transcation support, the testing method calls method of service layer to insert a record, then use jdbcTemplate to select it.

The test failed when jdbcTemplate queries object which just insert into database, said that there is no record, even if I have called the setComplete method and found the record in database, the test also failed.

Here is my code:

【DAO】:
=========================================================================
public User saveUser(User newUser) {
getHibernateTemplate().save(newUser);
//getHibernateTemplate().flush();
return (User) getHibernateTemplate().get(User.class,
newUser.getUserId());
}

【Service】:
=========================================================================
public User saveUser(User newUser) throws NewsletterException {
try {
return userDao.saveUser(newUser);
} catch (HibernateObjectRetrievalFailureException he) {
throw new ServiceException(systemMessage.getMessage(SERVICE_FAILED)
+ ": saveUser");
} catch (Exception e) {
throw new NewsletterException(e.getMessage());
}
}


public User getUserByName(String userName) throws NewsletterException {
try {
return userDao.getUserByName(userName);
} catch (HibernateObjectRetrievalFailureException he) {
throw new ServiceException(systemMessage.getMessage(SERVICE_FAILED)
+ ": userName");
} catch (Exception e) {
throw new NewsletterException(e.getMessage());
}
}

【Testing method】:
=========================================================================
public void testSaveUser() {
User newUser = createUser();
try {
userService.saveUser(newUser);
assertTrue(true);
String sql = "Select USERNAME from NEWSLETTER_USER where UPPER(USERNAME)= ?";
String userName = (String) jdbcTemplate.queryForObject(sql,
new Object[] { "PAUL" }, java.lang.String.class);
assertNotNull(userName);
assertEquals(userName, newUser.getUserName());
setComplete();
} catch (NewsletterException ne) {
fail(ne.getMessage());
}

【Spring config file】:
=========================================================================
<!-- User Service Definition -->
<bean id="userServiceTarget"
class="com.newsletter.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
<property name="systemMessage">
<ref local="systemMessage" />
</property>
</bean>

<!-- Transactional proxy for the User Service -->
<bean id="userService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="userServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

If I use another service method getUserByName() or use getHibernateTemplate().flush() in DAO after insert, then I can get the record.

So why ?
Because it is a column name, such as

to_date(txn_date, 'yyyy-mm-dd hh24:mi:ss') = another_string_date

so I think I should set it as String, am I right?

If it should be treated as Date format, how should I call the method? since the argument 'dateType' is string ?

Thank you very much for your quick response !
I tried another SQL with and it goes well, the code is :
=========================================================================
public List<Job> getJobByDate(final Date jobDate, final String dateType) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
String strDate = DateUtil.convertDateToString(jobDate,
"yyyy-MM-dd HH:mm");
Query query = session.createQuery("from Job job where "
+ "to_char(" + dateType + ",'yyyy-mm-dd hh24:mi') = '"
+ strDate + "'");
return (List<Job> query.list();
}
});
}
=========================================================================

and Hibernate will generate SQL like this:
=========================================================================
select job0_.TXN_NO as TXN1_5_,
job0_.TXN_TYPE as TXN2_5_,
job0_.TXN_DATE as TXN3_5_,
from NEWSLETTER_MESSAGE job0_
where to_char(job0_.TXN_DATE, 'yyyy-mm-dd hh24:mi') = '2008-05-22 16:11'
=========================================================================

but the code seems such ugly , does anyone have other methods to avoid hardcode and database-binding ?

Thanks
Hi all,

I'm new to Hibernate and came up with a problem when using HQL.

The problem is I want to search records from Oracle database according a column called 'txn_date', which its type is 'Date', and my HQL like this

=========================================================================
<sql-query name="job.getJobByDate">
select {job.*} from Newsletter_Message job
where to_char( ateType,'yyyy-mm-dd hh24:mi') =
to_char(:jobDate, 'yyyy-mm-dd hh24:mi')
<return alias="job"
class="com.newsletter.domain.job.Job" />
</sql-query>
=========================================================================

and the method is :
=========================================================================
public List<Job> getJobByDate(final Date jobDate, final String dateType)
{
return (List<Job> getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Query query = session.getNamedQuery("job.getJobByDate");
query.setString("dateType", dateType);
query.setDate("jobDate",jobDate);
return query.list();
}
});
}
=========================================================================

Hibernate will generate the SQL like this:
=========================================================================
select job.TXN_NO as TXN1_5_0_,
job.TXN_TYPE as TXN2_5_0_,
job.TXN_DATE as TXN3_5_0_,
from Newsletter_Message job
where to_char(?, 'yyyy-mm-dd hh24:mi') = to_char(?, 'yyyy-mm-dd hh24:mi')
=========================================================================

When execute, it keep complainning that
"org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not execute query"....

Caused by: java.sql.SQLException: ORA-01722: invalid number

I'm so confused that why oracle keep complainning "Invalid number"? I changed the sql to another format:

where to_char( ateType,'yyyy-mm-dd hh24:mi') = :strDate

and set string to variable strDate but also got same exception.

Can anybody help me? Thanks a lot !!!
Hi all,

I have a jsp page which named job.jsp , this page displaies all the curreently running jobs , and each job can be stoped by click the "stop" button in the web page.

The running jobs are executed by another java project in the same host.

So I wonder that how can I send the "stop running" message through the jsp
page to the java application? and how can I stop the running job when received the message?

thanks
12 years ago
JSP
Hi all,

I have a trouble with some UI output.Here is the requirement below:

1.The program will use the DOS as UI.

2.The program will execute some important job with a background thead,alos
it must provide the monitor function by displaying some information about
the transaction every several minutes.

For example:

There are *** records in souce database
There are *** records have been inserted to the destination database
There are *** records have been failed to insert to the destination
There are *** records remain in source database

All the inforamtion above must be refreshed in every 5 minutes.

So far as I know, I could use thread to get the real information from some other class,but the most troublesome job is that :

How can I refresh the information, not "append" ! just refresh the output!

Can anyone help me!

Thanks very much!
13 years ago
thanks! Ulf!

Do you think it's useful for the problem below

1.the application is staring with the console UI,when it accepts the parameter and then start a thread.

2.the thread execute the main job,with logic looks like below:

try{
// main job
}
catch(Exception){
// 1.get the thread state
// 2.if the thread is dead then call the log class
}

3.the log class has a method to deal with the unexpected exception in the folowing steps:

3.1 call Runtime.getRuntime().addShutdownHook(Thread)
3.2 the thread object is used to do some thing such as writes log to
local file

This is the only wany that I can think out.
Thank you very much!!
13 years ago
Deal all,

I have met a trouble with java.The requirement looks likes below:

1.The program must run in DOS console.

2.The program must catch the event and write log when someone close the DOS
window unexpectedly.

So there are 2 important point puzzle me:

1.How can we konw the "state" of the program:running or terminated?
2.How can we catch the event when the program was terminated unexpectedly?

Pls guide me to solve the problem,it's a urgency task.

Thanks all!
13 years ago
Let's suppose that I have installed the JDK1.4 and set the proper JAVA_HOME and CLASSPATH.If I want to install and config the JDK1.5,how should I do ?

By ceating a new system variable called like 'JAVA_HOME_JDK1.5' and point to the JDK1.5 directory,also like the CLASSPATH.

Or by adding a new string represent the JAVA_HOME and CLASSPATH of jdk1.5 to the existent system variable?

Could you guide me to solve this problem?

Thanks very much!
13 years ago

Originally posted by Satou kurinosuke:
Installing different versions is no problem, as long as you set CLASSPATH, JAVA_HOME and the PATH to the correct version.



thanks Satou! Could you explain how to set JAVA_HOME CLASSPATH correctly?For example, I have install jdk1.4 and set the JAVA_HOME CLASSPATH,if I install the jdk1.5
13 years ago
Dear All,

I have to install multiple JDKs in my computer:jdk1.4 & jdk1.5,and not sure that whether there will be a confused situation in my develop environment.Especailly the CLASSPATH's configuration.

How to make sure the compiler will find the right CLASSPATH ?
Should I create a new variable point to the new JDK's lib?
How to choose the JRE in Eclipse?

Thanks all !
13 years ago
Hi All,

I wonder what is the difference between Enumeration and Iterator? Both all them have hasMoreXXX and nextXXX methods.When shoud I use Enumeration and when shoud I use Iterator? Are there any difference in performance?

Thanks !
13 years ago
Thanks,Jason Menard and other people,your kind suggestions have help me a lot.

Now I have realized that what I need and what should I concern about is the basic tech.I'll readjust my study plan in the future.

Thank you everyone!