java.lang.Long cannot be cast to java.lang.String - google-app-engine

I need to iterate a List<myClass> in a jsp. This is how I obtain the list:
(when I commented it, the page loaded just fine).
<%
List<myClass> pjList = null;
StringBuffer ejbQuery = new StringBuffer();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistence");
EntityManager em = emf.createEntityManager();
ejbQuery.append("SELECT e ");
ejbQuery.append("FROM myClass e ");
pjList = em.createQuery(ejbQuery.toString()).getResultList();
for(myClass pj : pjList)
{
%>
<br />
<%= pj.getSomeField()%>
<br />
<%
}
%>
This is the error I get when running it in google appengine. locally it runs fine.
Uncaught exception from servlet
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
at org.datanucleus.store.appengine.DatastoreFieldManager.fetchStringField(DatastoreFieldManager.java:188)
at org.datanucleus.state.AbstractStateManager.replacingStringField(AbstractStateManager.java:1180)
at ar.edu.kennedy.proveedores.entities.ProEnteEy.jdoReplaceField(ProEnteEy.java)
at ar.edu.kennedy.proveedores.entities.ProPersonaJuridicaEy.jdoReplaceField(ProPersonaJuridicaEy.java)
at ar.edu.kennedy.proveedores.entities.ProEnteEy.jdoReplaceFields(ProEnteEy.java)
at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2772)
at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2791)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:443)
at org.datanucleus.store.appengine.query.DatastoreQuery.entityToPojo(DatastoreQuery.java:433)
at org.datanucleus.store.appengine.query.DatastoreQuery.entityToPojo(DatastoreQuery.java:391)
at org.datanucleus.store.appengine.query.DatastoreQuery.access$800(DatastoreQuery.java:97)
at org.datanucleus.store.appengine.query.DatastoreQuery$5.apply(DatastoreQuery.java:515)
at org.datanucleus.store.appengine.query.DatastoreQuery$5.apply(DatastoreQuery.java:507)
at org.datanucleus.store.appengine.query.StreamingQueryResult.resolveNext(StreamingQueryResult.java:137)
at org.datanucleus.store.appengine.query.StreamingQueryResult$1.computeNext(StreamingQueryResult.java:163)
at org.datanucleus.store.appengine.query.AbstractIterator.tryToComputeNext(AbstractIterator.java:132)
at org.datanucleus.store.appengine.query.AbstractIterator.hasNext(AbstractIterator.java:127)
at org.datanucleus.store.appengine.query.StreamingQueryResult$AbstractListIterator.hasNext(StreamingQueryResult.java:229)
at org.apache.jsp.busqueda_jsp._jspService(busqueda_jsp.java:138)
If I use a ListIterator and call hasNext() I get the same error. Help me understand what is happening, how to solve this?

It looks like the mapping you have from "myclass" is wrong.
There is a field marked as "String" when in fact it is a "number".
What it seems to happen from the StackTrace is that your value is fetched from the database and then casted to a string.
Since the value is not an string, you get that exception.
Try identifying in your class which "numeric" values are mapped as String and have them fixed. Start one by one until it works.

The problem is that in your class myClass has a field of type String that is representing by an integer in the database. The DataNucleus JDO is trying to convert this value into a Long, which is not a String, thus the error. You need to make sure the datatypes of the object match the data in the data store.

I can only guess that myClass doesn't match its description (probably something XML-based ;-) in the persistence layer.

I created web service with java, which formats fo to pdf with tomcat 6 version in my local computer(windows XP). But on unix, which was in my server(and there was tomcat 7) it threw an error java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer.
I was looking for the answer and finally I resolved the problem by changing tomcat version in server from 7 to 6. I think it may be help some of them, who have met the problem like this.

criteria.add(Expression.like("status", 115));
Error:java.lang.classCastException: java.lang.Integer cannot cast to java.lang.String
Do like this:
criteria.add(Expression.like("status", new String("115")));

Related

Uninitialized constant Table with Datamapper and Sinatra

When I try to access to the data of my table Task in irb I've got the error :
NameError: Unintialized constant Task
I'm working with Sinatra and I use Datamapper for the database.
require "sinatra"
require 'haml'
require 'data_mapper'
get "/" do
#posts = Post.all
haml :index
end
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Post
include DataMapper::Resource
property :title, String
property :content, String
property :photo, String
property :rating, Serial
end
DataMapper.finalize
If you have any idea of the problem :)
Finally the error appears beacause I forgot to require './main.rb and to Post.auto_migrate! in irb

Issues while Processing Large Strings in Grails Application

I am working on grails application. I am getting issues in processing string with length greater then 255 characters. When I try to update a record with string greater than 255 characters I get this exception:
nested exception is org.hibernate.exception.DataException: could not update:
Caused by: org.hibernate.exception.DataException: could not update: [com.ef.apps.mediasense.recordings.Calls#1]
Caused by: java.sql.DataTruncation: Data truncation
This is attribute in which I strore large string:
static mapping = {
tag column:'sessionTag'
}
And, This is the constraint I am applying on this:
static constraints = {
tag (nullable:true, maxSize:1000)
}
And, If I see the design of table, It looks like this for this attribute:
So, Everything seems fine then why It's not allowing me to store string having characters greater than 255.
I have tried this approach too, But no Luck:
static mapping = {
tag column:'sessionTag', type: 'text'
}
with constraint:
static constraints = {
tag (nullable:true, maxSize:1000)
}
And If I see the design, It looks like this:
But I am still getting the same issue. I just simply want to store string with greater than 255 characters in Grails. Guide me If I am doing anything wrong or I can achieve this by some other approach.
Thanks for your time and consideration :)

Migrating working ServiceStack to live causes Unable to cast object of type 'System.Byte' to type 'System.String'

I have developed a ServiceStack API, using ORMLite based on a SQL Server. The app works perfectly pointing at both my local SQL database and an Azure database. Happy Days!
I have now tried to move this solution to the live server which has it's own local copy of the same database and I am getting strange results. The error is:
Error Code: InvalidCastException
Message: Unable to cast object of type 'System.Byte' to type 'System.String'.
[EMEM: 1/16/2014 11:49:29 AM]: [REQUEST: {Equipment:DP112}]
System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.String'. at lambda_method(Closure , Object , Object ) at
ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetDbValue(FieldDefinition fieldDef, IDataReader dataReader, Int32 colIndex, Object instance) at
ServiceStack.OrmLite.ReadExtensions.ExprConvertToList[T](IDataReader dataReader) at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExprConvertToList[T](IDbCommand dbCmd, String sql) at
ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func`2 filter) at
ViewPoint.EquipmentService.Get(EMEM request) at
ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)
I have checked the database schemas and they look identical.
This is the code that works on 2 out of the 3 databases quite happily but not the third.
public object Get(EMEM request)
{
var dbFactory = new OrmLiteConnectionFactory(WebConfigurationManager.ConnectionStrings["db"].ToString(), SqlServerDialect.Provider);
using (IDbConnection db = dbFactory.OpenDbConnection())
{
if (request.Equipment == null)
{
List<EMEM> results = db.Select<EMEM>();
return results;
}
else
{
List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.Equipment == request.Equipment));
return results;
}
}
}
I can literally fix the problem by pointing the connection string at the azure database which tends to suggest it's database related(?)
Extra Information:
I have also written a put method which updates a row in the database and that works fine.
On 2 of the servers EMEM is a table but on the third, where it doesn't work, it's a View.
Can anyone suggest where to start looking for this problem?
UPDATE: I have now created a View on my local development database so it should now be identical to the live database. I was expecting this to break the local dev site but it hasn't... :(
BINGO! FIXED IT!
IT WAS linked to the View, but it wasn't the View's fault....
The view was looking at a table with different data types against most of the values. The demo table I was working against had all the columns set to String!
So, look out when people give you "demo tables, with identical data to the live" to develop against.
They aren't always identical!!
HTH

AD returns Objectsid as String and SecurityIdentifier is failing interprete this

Usually AD returns 'Objectsid' as a byte[]. So I type cast the value returned by AD in to byte[]. This procedure worked against several AD but not in one case. In this AD environment, I get following exception.
Exception: Unable to cast object of type 'System.String' to type 'System.Byte[]'. (System.InvalidCastException)
To debug this I started checking data-type of the value returned by AD, and it was system.string not byte[]. I printed this string and it was garbage. Then I passed this string to SecurityIdentifier() and I got exception again.
Exception: Value was invalid. Parameter name: sddlForm (System.ArgumentException)
Code:
//Using System.DirectoryServices.Protocols objects
object s = objSrEc[k1].Attributes[(string)obj3.Current][0];
string x = s.GetType().FullName;
if (x.ToLower() == "system.byte[]")
{
byte[] bSID = ((byte[])s);
if (bSID != null)
{
SecurityIdentifier SID = new SecurityIdentifier(bSID, 0);
String ObjectSID = SID.Value;
}
}
else if (x.ToLower() == "system.string")
{
SecurityIdentifier SID = new SecurityIdentifier((String)s); //ssdl excception
String ObjectSID = SID.Value;
}
This is the first time I am seeing AD return string data for ObjectSID. I have run my code against many AD servers. I am planning to check the data-type of ObjectSID in AD schema.
Do any one come across this behavior? Should I call the Win32 api ConvertByteToStringSid()?
Thanks
Ramesh
Sorry for reviving a graveyard post, but I had the same issue a year or so ago, managed to find out why and I figured I'd at least share the reason behind this behavior.
When using the System.DirectoryServices.Protocols namespace, all attribute values should be either a) a byte array, or b) a UTF-8 string. Thing is, the developers at Microsoft figured that they should help people by returning a string when the byte array returned from the underlying LDAP API can be formatted as one, and the byte array itself when the UTF-8 conversion fails. However, this is only true for the indexer of the DirectoryAttribute class, and not for the iterator (which always returns the byte array) or the GetValues method.
The safest way to always get a byte array when you want the SID is, as previously mentioned by others, the GetValues method.
I came through the same. Found this behavior normal when deal with ForeignSecurityPrincipals, however recently found this when translate attributes of built-in groups from some old Win 2K3 domains.
I don't like this as can't just ask the result attribute to tell me via GetType() what type are you and what should I do with you ( GetValues(Attribute.GetType()) ). One of the solutions was reading all attributes definition from AD schema, but this part might be a bit heavy (depends what you're looking for) although it was only a small part of overall AD processing the solution was performing.
Cheers,
Greg

How-to: Load a type from a referenced assembly at runtime using a string in Silverlight

I have tried this, specifying the assembly name:
Type.GetType(string.Format("{0}.{1}, {0}", typeToLoad.AssemblyName, typeToLoad.ClassName));
Which throws the following:
The requested assembly version conflicts with what is already bound in the app domain or specified in the manifest
Trying the same without including the trailing assembly name like this:
Type.GetType(string.Format("{0}.{1}", typeToLoad.AssemblyName, typeToLoad.ClassName));
-- returns null.
So, I am looking for a way to instantiate a class by providing its fully qualified name in Silverlight 4.0.
Any ideas would be greatly appreciated, Thanks.
I had the same issue and it worked when I tried the assembly qualified type name in the following format :
", , Version="", Culture=, PublicKeyToken="

Resources