Letícia Álvares Barbalho wrote:
Hello, everyone.

I have an app which uses struts + hibernate. I was having a problem of corrupted blobs (which are treated by the app as byte[]), and so far I was blaming hibernate for that. But right now, making a test, I found out that my data gets corrupted BEFORE even being treated by hibernate. Here's what I have:

1. A jsp page that gets a String using html:text, property = descricao
2. An action form that has the descricao property as a byte[], together with its getters and setters 3. An action that takes data from this action form and puts it in my object, which by the way also has this property as a byte[], with its getters and setters.

So, what I wanna do is get this String and put in a blob field. Here comes my action code:

cor.setCor(addCorForm.getCor());
cor.setNome(addCorForm.getNome());
cor.setDescricao(addCorForm.getDescricao());
CorService.getInstance().addCor(cor);

That's basically it: I set the properties and add. The thing is that if I get the byte[] value of addCorForm.getDescricao() and convert it to a string, it gives me NULL or a lot of weird characters. That means my data is getting corrupted right there, before its insertion.

Another proof of it, is that if I insert a line in this table using the database's tool, I can retrieve the information through my application.

So... database tool = correct data. App = incorrect data, even before adding the object in the DB with hibernate. What I can think here is struts is corrupting... could it be?

1. Is html:text the correct component I have to use for this?
2. Do I have to make any conversion, being that I insert a String and it gets a byte[]?

How do you guys do it? Thank you all very much for the help

You need to thing about what happens when your form is submitted:

1) the browser serializes the text into the HTTP request, using encoding X

2) the servlet container reads the HTTP request and deserializes the text into the request object, using encoding Y

3) Struts copies the text into the form bean via BeanUtils, using encoding Z

4) you copy the bytes from the form bean into your database

If X and Y are different, you'll get corrupted data. If encoding Z is not what you're expecting (i.e. doesn't match whatever character set you're expecting data stored in your Blob column to use), you'll get corrupted data.

I would suggest you start by making sure that 1 and 2 are right (try getting the text value directly from the request and comparing it to a known-good String value). There's a number of ways you can get tripped up trying to deal with non-Latin-1 data over HTTP.

Once you have that working, you can look at 3. Take your now known-good request string, encode it to a byte array using the encoding you need and compare the result with the byte array Struts / BeanUtils stores in your form bean.

Actually, I suspect that the String to byte[] conversion in BeanUtils probably isn't doing what you need at all; I just had a quick look at the code and I'm not sure quite what it's doing. You may have to change your form bean to use String instead of byte[] and do the conversion yourself in your action.

HTH,

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to