Home » Eclipse Projects » Eclipse Scout » BeanArrayHolder and FormData
BeanArrayHolder and FormData [message #821837] |
Thu, 15 March 2012 21:57 |
Jeremie Bresson Messages: 124 Registered: November 2010 |
Senior Member |
|
|
I am trying to use a BeanArrayHolder with formData:
Here a first example that works:
@Override
public AnswerBean[] loadAll() throws ProcessingException {
BeanArrayHolder<AnswerBean> beansArray = new BeanArrayHolder<AnswerBean>(AnswerBean.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :QuestionNr, :YourName, :AnswerNr", beansArray);
return beansArray.getBeans(null);
}
with the Bean (defined in shared):
public class AnswerBean implements Serializable {
private static final long serialVersionUID = 1L;
private Long m_answerNr;
private Long m_questionNr;
private String m_yourName;
public Long getAnswerNr() {
return m_answerNr;
}
public void setAnswerNr(Long answerNr) {
m_answerNr = answerNr;
}
public Long getQuestionNr() {
return m_questionNr;
}
public void setQuestionNr(Long questionNr) {
m_questionNr = questionNr;
}
public String getYourName() {
return m_yourName;
}
public void setYourName(String yourName) {
m_yourName = yourName;
}
}
This works fine.
Now instead of this bean, I want to use the formData of the AnswerForm:
@Override
public AnswerFormData[] loadAll() throws ProcessingException {
BeanArrayHolder<AnswerFormData> formDataArray = new BeanArrayHolder<AnswerFormData>(AnswerFormData.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :QuestionNr, :YourName, :AnswerNr", formDataArray);
return formDataArray.getBeans(null);
}
This do not work, because the getters getQuestionNr() and getYourName() return a QuestionNr class (extending AbstractValueFieldData<Integer>) and a YourName class (extending AbstractValueFieldData<String>). These getters are just convenience getter for getFieldByClass(..).
The jdbc processor do not seems to recognize the formData and handle this like a normal Bean.
Quote:ProcessingException[ProcessingStatus[ERROR code=0 invoking org.eclipselabs.mcqs.shared.services.process.IAnswerProcessService:loadAll / SQL with binds:
SELECT QUESTION_ID,
NAME,
ANSWER_ID
FROM ANSWERS
INTO :QuestionNr,
:YourName,
:AnswerNr
OUT :QuestionNr => ? [QuestionNr]
OUT :YourName => ? [YourName]
OUT :AnswerNr => ? [Long] / property QuestionNr java.lang.IllegalArgumentException: converting 1 from Long to AnswerFormData$QuestionNr failed with code 3 (no to-mapping)]]
at org.eclipse.scout.rt.server.services.common.jdbc.internal.exec.AbstractBeanPropertyOutput.finishBatch(AbstractBeanPropertyOutput.java:107)
at org.eclipse.scout.rt.server.services.common.jdbc.internal.exec.BeanArrayHolderOutput.finishBatch(BeanArrayHolderOutput.java:67)
at org.eclipse.scout.rt.server.services.common.jdbc.internal.exec.StatementProcessor.finishOutputBatch(StatementProcessor.java:550)
at org.eclipse.scout.rt.server.services.common.jdbc.internal.exec.StatementProcessor.processSelectInto(StatementProcessor.java:278)
at org.eclipse.scout.rt.server.services.common.jdbc.AbstractSqlService.selectInto(AbstractSqlService.java:717)
at org.eclipse.scout.rt.server.services.common.jdbc.SQL.selectInto(SQL.java:117)
at org.eclipselabs.mcqs.server.services.process.AnswerProcessService.loadAll(AnswerProcessService.java:103)
... and more
Caused by: java.lang.IllegalArgumentException: converting 1 from Long to AnswerFormData$QuestionNr failed with code 3 (no to-mapping)
at org.eclipse.scout.commons.TypeCastUtility.createException(TypeCastUtility.java:217)
at org.eclipse.scout.commons.TypeCastUtility.castBasicValueImpl(TypeCastUtility.java:286)
at org.eclipse.scout.commons.TypeCastUtility.castValueImpl(TypeCastUtility.java:145)
at org.eclipse.scout.commons.TypeCastUtility.castValue(TypeCastUtility.java:64)
at org.eclipse.scout.rt.server.services.common.jdbc.internal.exec.AbstractBeanPropertyOutput.finishBatch(AbstractBeanPropertyOutput.java:102)
... 61 more
Is there a FormDataArrayHolder ??? or something similar to achieve this ?
Is it reasonable to change scout in order to offer support for formData in the BeanArrayHolder or to create a FormDataArrayHolder ? (If you think this is a good idea, I could try to submit a patch).
|
|
| | | |
Re: BeanArrayHolder and FormData [message #824169 is a reply to message #824165] |
Mon, 19 March 2012 10:45 |
Ivan Motsch Messages: 154 Registered: March 2010 |
Senior Member |
|
|
Some "best practice" note:
1) To be more precise with binds i would qualify the bean array holder
2) java bean names always start with lowercase characters, use the java bean name, even though the sql binding might be lenient.
3) sql tables could be aliased
4) when getting all beans, beansArrayHolder.getBeans(); would do since the argument is an ellipsis.
This leads to the following:
@Override
public AnswerBean[] loadAll() throws ProcessingException {
BeanArrayHolder<AnswerBean> beansArrayHolder= new BeanArrayHolder<AnswerBean>(AnswerBean.class);
SQL.selectInto(" select a.question_id, a.name, a.answer_id " +
" from answers a" +
" into :{h.questionNr}, :{h.yourName}, :{h.answerNr}",
new NVPair("h",beansArrayHolder));
return beansArrayHolder.getBeans();
}
[Updated on: Mon, 19 March 2012 10:46] Report message to a moderator
|
|
|
Re: BeanArrayHolder and FormData [message #824186 is a reply to message #824169] |
Mon, 19 March 2012 11:12 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
According to the best practices (thanks for that), the first example should be written:
BeanArrayHolder<AnswerBean> beanArrayHolder2 = new BeanArrayHolder<AnswerBean>(AnswerBean.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :{list.questionNr}, :{list.yourName}, :{list.answerNr}", new NVPair("list", beanArrayHolder2));
It works fine with Java Beans (POJO)
For my tests, I tried several methods:
//Plain Select
Object[][] plainSelectResult = SQL.select(" select question_id, name, answer_id from answers ");
System.err.println("plainSelectResult.size: " + plainSelectResult.length); // -> 3 rows.
//BeanArrayHolder -> lenient [SOULD NOT BE USED]
BeanArrayHolder<AnswerBean> beanArrayHolder1 = new BeanArrayHolder<AnswerBean>(AnswerBean.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :questionNr, :yourName, :answerNr", beanArrayHolder1);
System.err.println("beanArrayHolder1.size: " + beanArrayHolder1.getBeanCount()); // -> 3 beans.
//BeanArrayHolder -> explicit binds
BeanArrayHolder<AnswerBean> beanArrayHolder2 = new BeanArrayHolder<AnswerBean>(AnswerBean.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :{list.questionNr}, :{list.yourName}, :{list.answerNr}", new NVPair("list", beanArrayHolder2));
System.err.println("beanArrayHolder2.size: " + beanArrayHolder2.getBeanCount()); // -> 3 beans.
//BeanArrayHolder using a formData -> still do not work because of Type Cast Exception (same as in the first message of the forum post).
BeanArrayHolder<AnswerFormData> formDataArrayHolder = new BeanArrayHolder<AnswerFormData>(AnswerFormData.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :{list.questionNr.value}, :{list.yourName.value}, :{list.answerNr.value}", new NVPair("list", formDataArrayHolder));
System.err.println("formDataArrayHolder.size: " + formDataArrayHolder.getBeanCount()); // Exception before this line.
I still see the described exceptions because of QuestionNr being an AbstractValueFieldData<Integer>.
On the formData, there is no setQuestionNr(Integer). At the end of the batch, the AbstractBeanPropertyOutput should invoke formData.getQuestionNr().setValue(Integer)
I also tried: BeanArrayHolder<AnswerFormData> formDataArrayHolder = new BeanArrayHolder<AnswerFormData>(AnswerFormData.class);
SQL.selectInto(" select question_id, name, answer_id " +
" from answers " +
" into :{list.questionNr.value}, :{list.yourName.value}, :{list.answerNr.value}", new NVPair("list", formDataArrayHolder));
This also do not work.
|
|
| | | |
Goto Forum:
Current Time: Thu Sep 26 02:45:53 GMT 2024
Powered by FUDForum. Page generated in 0.04710 seconds
|