Using HashMaps in an EJB

Ok this is the second time Im posting this problem. Last time I posted all the details and it may have been more general. This time I will be more specific. The following are the requirement that we were given for a school assignment,

Develop a prototype software module to implement a messenger service. Messaging

Systems consist of one Messaging server and several clients. Message can differ from a simple text message to a complex object like an Image. System should include following components.

?Messaging Server: A Messaging server will hold the message queue for our

Messaging system (which you need to develop)

?Messaging Queue: A Queue will hold the messages from the client. The

messages on this queue will be MapMessages that will allow us to store

name/value pair information about the message to be sent out

?Message Client: A client will create a message, and put it on the Messaging

Queue. This message will hold the information for the email message to be sent out

?Message Driven Bean: A message driven bean will be responsible for taking in the Message MapMessage and for mailing it out.

To develop the system you have to use EJB 3.0 architecture. Usage of Java Persistence

API for modeling data would be recommended.

I dont want the coding to develop the system, I have already created that part and it works perfectly when I send text or numbers as messages. What I need help is on how to handle a hashMap. Simply SET'*** and GET'*** a hashMap does not seem to work. If you good people can give me some pointers on how to set up the entity and how the GET'*** and SET'*** methods work in relation to the HashMap I would be a very happy and thankful guy indeed.

P.S - The *** indicate'ing

. The forum seems to sensor those words.

Message was edited by:

jomanlk

[1922 byte] By [jomanlka] at [2007-11-26 19:55:53]
# 1
and what exactly do you mean by "does not seem to work"?That's WAY too broad a statement for anyone to say anything useful about what you're doing wrong.
jwentinga at 2007-7-9 22:49:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thats the problem!! I gave a full description in this thread and no one answered so I decided to be more general.

For reference here is the link with full details

http://forum.java.sun.com/thread.jspa?threadID=5138929&messageID=9509475#9509475

Am I doing something wrong? When you use a HashMap is the persistence handled the same way you might handle a String or Float ? Im just passing the HashMap

When I deploy the app though I get an SQL error. There is no specific error but I get millions of transaction rollbacks and toplink errors. I do remember it saying something about not being able to map it to the database (I am at office now and I will try to get the exact error from home) .

Someone please help!

jomanlka at 2007-7-9 22:49:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
you gave NO description except "does not seem to work".That's nothing.
jwentinga at 2007-7-9 22:49:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
1)pl paste the error log and the corresponding class frame your problem accordingly!
cvasu4a at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

WELL I gone through your code in the link

http://forum.java.sun.com/thread.jspa?threadID=5138929&messageID=9509475#9509475

>Well thats the important parts of the code. it works fine when I send >simple messages but when I try out the HashMap the servlet is >displayed but no messages are sent to the queue. It would be great if >anyone could help.

The reason is simple;

THE HASHMAP CONTAIN NULL VALUES AND YOU ARE SENDING NULL TO THE JMS SO NO OUTPUT.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

String title=request.getParameter("title");

System.out.println("TITLE IS:"+title);

String body=request.getParameter("body");

System.out.println("BODY IS:"+body);

HashMap hm = new HashMap();

//hm.put("title", request.getParameter("title"));

//hm.put("body", request.getParameter("body"));

hm.put("title",title );

hm.put("body", body);

if ((title!=null) && (body!=null)) {

try {

Connection connection = connectionFactory.createConnection();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageProducer messageProducer = session.createProducer(queue);

ObjectMessage message = session.createObjectMessage();

// here we create NewsEntity, that will be sent in JMS message

NewsEntity e = new NewsEntity();

e.setTitle(title);

e.setBody(body);

e.setHm(hm);

message.setObject(e);

messageProducer.send(message);

messageProducer.close();

connection.close();

// response.sendRedirect("ListNews");

} catch (JMSException ex) {

ex.printStackTrace();

}

}

try out this code this modified code and allot some dukes if you succeed best of luck :))

cvasu4a at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Sorry for the late reply, I am going through my mid terms at the moment.

To cvasu4

I will try that code out, but I dont think it was an empty string. Because when I deploy even the Strings named Title and Body do not display (not just the HashMap). And in the error log (I will get the error log soon, so many exceptions are getting thrown I have to wade through it all :( )I noticed something akin to this hm{title="Test", Body="test"} or something close to it so Im prettry sure it wasnt empty. Anyway for sure I will try your suggestions out and report back to you.

To jwenting

Thanks for at lease reading the problem and trying to correct my miscreant ways :) I will definitely be posting all the relevant info soon.

jomanlka at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Hi

That did not work out. I tried replacing it still have the same problem. Anyway I reverted to the original and went through the logs. This is error of any note that comes up when I try to insert into the message Queue

Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseExceptionInternal Exception: org.apache.derby.client.am.SqlException: Invalid data conversion: Parameter object type is invalid for requested conversion.Error Code: -99999Call:INSERT INTO NEWSENTITY (ID, BODY, HM, TITLE) VALUES (?, ?, ?, ?)bind => [1, ddddd, {body=ddddd, title=Test Message}, Test Message]Query:InsertObjectQuery(ejb.NewsEntity[id=1])

Let me know if that was helpful. Let me know if any of you people think of something.

In any case cvasu4, here are some points for trying to help me. Let me know if you think of something

jomanlka at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
You should really be able to figure that one out for yourself...Check the parameters of your SQL call to the ones it expects...
jwentinga at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Hi jwenting!

I understand that the DB is expecting 4 values but the bean is sending 3 values plues a sort of nested value. But the persistence is handled by the bean itself right ? It checks the GET and SET methods and creates the tables. Since I have just declared it as a Hashmap the bean will be handling how it is stored right ? If this is not the case how do I fix it so that I can store the HashMap in the DB (I want to be able to store the whole Hashmap and not just extract the values and store separately. This is because I want to store a binary stream as well).

Any suggestions? Please keep in mind that Im very new to all this and most of this is Greek to me

jomanlka at 2007-7-9 22:49:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...