SEAM Entity Query on field of sub object - java

Using seam-gen based code. I have an object "Classroom", which contains an instance of "Location". I want to query for classrooms but specifying a value on the Location object.
Something like 'select from Classroom where Location.State = "NY"'. When I try to bind a selectOneMenu with a list of states to #{ClassroomList.classroom.location.state} I'm getting errors.
Was getting a null pointer exception on Location. I'm assuming I need to instantiate a new "Location" on the Classroom object, but not sure where to do that. On the Classroom entity's constructor? On the ClassroomList backing bean (where the example object is bound to the ClassroomList JSP search fields)?

Yes, you need to instantiate ClassroomList.instance.Location. Because the ClassroomList.instance is not bound to the Database, this is not done automagically

Not sure if this is the best way, but I got it working.
I have a String exposed on my ClassroomList backing bean as "String locationState". My dropdown list of states binds to that
Then that's referenced in my restrictions as:
...
lower(classroom.location.state) like lower(concat(#{classroomList.locationState}, '%'))",
...
--
When I tried to instantiate ClassroomList.instance.Location, I would get:
javax.servlet.ServletException with message: "Id and clazz must not be null"
Not sure what's causing that?

Related

Cannot get #Value from inside an instance or bean

I have a property set to "true", that I want to bind to my checkStatus field.
#Value("${prop.checkstatus}")
private boolean checkStatus;
I am able to access the value from inside an #Service class. However, if I use #Value inside a class that I instantiate or retrieve using AppliationContext.getBean("classA"), the value is always "false" since it cannot get the correct value from the property file. Please advise as I need to access this value from an instance and from a bean.
In some parts of the code, I create the object using new and try to access #Value. I understand that this is not possible since the new object is not managed by spring. But there are parts of the code where the object is retrieved using applicationcontext. Still I cannot access the #Value. Is there a better approach in retrieving #Value both for objects created using new and applicationcontext.getbean?

ORMLite ID field cannot be compared

I have several classes, all of which have an ID field declared as Integer the next way:
#Expose
#DatabaseField(columnName = "_id", id = true)
private Integer idField;
Everything compiles and runs correctly, but when I simply try to check if a record exists:
Integer idField = 1;
result = DBHelper.getHelper().getClassDAO().idExists(idField);
I get the exception:
java.sql.SQLException: Field '_id' is of data type null which can not be compared
The thing is that with one class (let's name it A) the method works properly, but with the others fail and I don't know which is the cause because all the classes have its ID field declared the same way.
I'm getting this exception too if I try to createOrUpdate the object of any class, except the refered class A.
Any helping hand would be appreciated.
NOTE. The project uses an ormlite_config.txt file, which is updated.
After a few hours debugging I came into that the mentioned exception comes only when the class to be saved has one o more collections. As far as I know, the exception is thrown inside the idExists method of the Dao class. Having in mind that the createOrSave method surely must call it, that's why I get the SQLException in both cases.
And now, the workaround to solve this. Rather than invoking the idExists method, I had to create my own like this:
return DBHelper.getHelper().getMyDAO().queryForId(idField) != null;
And instead of calling the createOrUpdate method, first I check if the record exists and then, depending on the result, I call the create or update method of the Dao class I want to persist.

How can I put a bean object to sessionMap, and then retrieve its properties on jsp page using Struts 2 property tag

In login Action I am checking user authentication, and if it is validated, I am putting the user bean into sessionMap:
public String execute()
{
if(userValid)
sessionMap.put("userBean", userBean); //userBean retrieved from DB
}
Now on the landing jsp, when trying to retrieve the session items:
<s:property value="#session.userBean.name" />
Obviously this would return an Object type, as I am storing it that way, so how can I type caste this to UserBean class.
I was expecting to get a solution for this on Google, but found it nowhere since this seems to be a basic implementation. So please let me know if there is any other way to implement this functionality using Struts2.
This works fine for me...
<sp:property value="#session.usertype"/>
<sp:property value="#session.bean.loginID"/>
This both worked fine for me...
sessionMap.put("bean", loginBean);
sessionMap.put("usertype", loginBean.getUserType());
I declared something like this....
Just make sure that in property tag you you same name you used while setting the bean in sessionMap ....
This should probably work....
Obviously you can't cast it to UserBean class if the object is not the instance of that class. In the value attribute you have put a string "#session.userBean.name". Struts parse this string for OGNL expression and if it's a valid expression that returns a value, it will replace it with that value. The returned type is Object, but this type is determined by ValueStack implementation.
Then property tag writes this object to the out. It uses toString() to convert the object to string. And if your object implements this method, then this value would be written.
Looks like your expression returns an Object, which has instance type String, so it's already implemented this method.

MappingException: InvocationTargetException

I have a small problem here and hope that someone can help me out.
I wanna do a mapping from Object to Integer. The Object is marked as Fetchtype.Lazy.
When I do the mapping I get the following error message:
11:31:46,569 ERROR MappingProcessor:253 - Field mapping error -->
MapId: null
Type: null
Source parent class: ClassA
Source field name: MyObjectProperty
Source field type: null
Source field value: null
Dest parent class: ClassB
Dest field name: myIntegerProperty
Dest field type: java.lang.Integer
org.dozer.MappingException: java.lang.reflect.InvocationTargetException
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:88)
at org.dozer.util.ReflectionUtils.invoke(ReflectionUtils.java:258)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.invokeReadMethod(GetterSetterPropertyDescriptor.java:234)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.getPropertyValue(GetterSetterPropertyDescriptor.java:68)
at org.dozer.fieldmap.FieldMap.getSrcFieldValue(FieldMap.java:82)
InnerException:
Caused by: java.lang.IllegalStateException: Attempt to access an unloaded field of detached instance
I created a custom converter in dozer xml file but the converter is never called. I guess the application gets a problem before reaching this converter.
Funny thing is I can Map the fields successfully the other way around, so Integer to Object.
I appreciate any ideas, if you need further information please give me a word.
Thx to all.
If this were Hibernate I'd think a lazily loaded field were accessed outside of the Session the data bean is bound to.
I suggest loading the entity and mapping it in the same transaction would solve the problem. I don't know that for sure of course :)

unable to add objects to saved collection in GAE using JDO

I have a ClassA containing an ArrayList of another ClassB
I can save a new instance of ClassA with ClassB instances also saved using JDO.
However,
When I retrieve the instance of Class A,
I try to do like the below:
ClassA instance = PMF.get().getPersistenceManager().GetObjectByID( someid );
instance.GetClassBArrayList().add( new ClassB(...) );
I get an Exception like the below:
Uncaught exception from servlet com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found..
So I was wondering, Is it possible to add a new item to the previously saved collection?
Or was it something I missed out.
Best Regards
"no matching index found"
Perhaps you need to add some index in GAE/J's datastore ?
Nothing to do with JDO

Categories

Resources