ryan sukale

Ranch Hand
+ Follow
since Aug 11, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by ryan sukale

Hi Ben,

As you would have already noticed, objects are stored as references.
When you do a map.get() you get a reference to the object that was stored in the map.
When you manipulate the object, you are in fact, manpulating the original object.

When you again store this object using the map.put('mark', order2), you are storing the reference to the original object in the map.

Both, the keys - john and mark store a reference to the same sales order object in the map. You dont have 2 objects.

What you actually want to do is to create a copy of the object.

You may want to do this by creating a new constructor in the SalesOrder class that accepts another sales order object and copies all the attributes of the argument to its own instance variables.




What you actually want to do is a deep copy of the 'orderToCopy'. However there is no simple solution to doing a deep copy in java.

One way would be to use serialization, that is, hoping that your SalesOrder class implements the serializable interface.



IMO, copying the object via the overloaded constructor is a pretty neat way to do this as it gives you a clear indication of what is being copied and what not.
The downside is that you would have to copy each primitive value for the nested objects in the SalesOrder(the way we copied each primitive in the array) and not mistakenly assign references to the original object's instance variables.

I hope this helps or at least gives you a hint for the approach you might have to take.
12 years ago
Ok guys, i got it

I suppose i was using a different version of struts wherein using the %{} was required for the expression to be evaluated. I changed the jar files now. This is what did the job for me

<s:property value="#mymap.[#mykey2]"/>

My problem was coming because i was trying to use it in a href for a s:a tag. And without the %{} operator, the expression was not being evaluated.

So, i guess, i was right in the beginning itself. Rest of the time, it was just me making silly mistakes. :>

Regards,
Ryan Sukale
14 years ago
Hi Rajiv,

Yes, you can return dynamic pages from your struts 2 action. The idea here would be to save the page name in an instance variable in the action, and create a getter/setter for it. Then in your action mapping, you can use the above created getter for your page name.

I created some quickie dummy actions and results just for elaboration purpose. So the naming might look a little bit eerie. Just bear with it.

Check out the following code for the action class.


I will be invoking the "anotherMethod" function in my action mapping that follows. If you wish to, you can use the the code written in the "anotherMethod" function inside the execute method itself.
Here's the action mapping.



As you can see, i returned a "success" string from my "anotherMethod" in the action class. So, the above result will be invoked on completion of the action. The page to be displayed is evaluated as an EL variable and fetches the nextPage property on the DummyAction2 class
The action invocation is just as usual.



I guess, you can let your business logic determine the page name. And you page will be displayed accordingly based upon the value set in a field in the action.

Hope this helps ;)

Regards,
Ryan Sukale
14 years ago
Hi everyone,

I have two concerns that have been annoying me since quite some time now.

I have hashmap that was created on a page using the struts2 <s:set> tag. It looks something like this


At some point in the page, i need to get a value from the hashmap based upon a key, and i want to do it using OGNL. I dont want to iterate the map. I simply need to get a single value from the map. How am i supposed to do that without iterating?

The second problem is the following
The key is generated based upon some logic, which i store using another <s:set> tag. Something like this


I need to get a value from the hashmap using this key. And i need to display it.

How do i simply call the get function on the hashmap?

I tried this

and this

and this


The third one obviously does not work because of the nesting problem. But the same nesting logic is applicable to the second case as well, due to the manner the value attribute is handled. However none seem to work for me.


I suppose the issues are very simple. I almost feel that i get it, but somehow the solution eludes me.
Could someone please tell me what i am missing here?

Regards,
Ryan Sukale
14 years ago
Hi Midhun

In code1, you have clearly declared variabel 'a' to be of type A.
So, when the compiler reaches line 19 in code 1, it is fooled into thinking that the process() method that is being called is of class A. So, it expects that the method should be surrounded with a try catch block.
It is only at runtime that the the JVM decides the actual method that is to be called, which in the case of code 1, will be the overridden method process() of class B.


In code2, the pseudo object that you have created is of class B.
Since there is no trace of any reference of class A, the compiler 'KNOWS' that the method that will be called is of class B, which throws no exception.

To sum up,
When calling a method, the compiler checks the type of the reference to determine the class whose method will be called.
Whereas at runtime, the actual type of the object, pointed to by the reference, gets called. The compiler, at any given point of time, is not aware of the actual object. For it, its the reference type that is of the utmost importance.


Regards,
Ryan Sukale
14 years ago
Hi Dharmendra,

MZ notes are good.

But ther is nothing to beat the real deal.
I read the specs, and i found them pretty awesome because of the clarity with which they are written. I cannot find a single reason as to why someone preparing for the cert should not read them.


Regards,
Ryan Sukale
Hi Prasad,

I wrote the exam some 3 months ago, and as far as i can remember, I did not get even a single question from deployment descriptors.
However, while preparation, I also roughly read the stuff related to deployment descriptors, just to get a feel of the relationships and the syntax.

Personally I dont think there is much emphasis on learning about all the aspects of the deployment descriptors. Annotations are in, and i suppose we need to primarily focus on learning to write EJB's using annoations, at least for the current version of the cert.


Regards,
Ryan Sukale
Congrats!!




Regards,
Ryan Sukale
Hi Sharmila,

I dont think that we are going to see a HFEJB 3.0 book.

However, there are several others if you want to learn EJB 3.
You can try the following

Oreily publications.. Enterprise JavaBeans 3.0
Manning -EJB 3 in action
Pro EJB 3 Java Persistence API
Beginning EJB 3 application development.

If you are targeting the cert, make sure that you also go through the specs, preferably after finishing at least one of the manning or oreilly books.

Regards
Ryan Sukale
Hi Sandeep,

Its appropriate. I have been referencing Pro EJB 3 Java Persistence API while writing the code, and there is a clear example of using the discriminatorcolumn in the same way that i have done here.

Moreover, hibernate does not need to use the discriminator column, unlike other implementations. Thats what i have found out after googling around a bit.

Had it been an issue, i would never have been able to perisist these entities elsewhere, or when i remove the not null constraint on my database column.

Anyways, Thanks for trying. But I still cant find out whats wrong...

Regards,
Ryan Sukale
I am using the joined inheritance strategy.

I dont know why, but hibernate is not populating the discriminator column in the table. I need to access the discriminator column value in elsewhere, so i had to create it as a property and populate it using an enum.

Here is the base entity


Here is the subclass entity.






Before passing objects of this class to the addOption method, i ensure that all its property setters are called so that the instance variables are properly initialized. And i print the values of all the instance variables in the prepersist method, for both, the base class and the sub class(the other properties i have not shown here, which but i have also set. I know they are set bcos of the log of the prepersist displays them).

Something very small is wrong with the whole thing, and its just managing to escape my notice.. Its frustrating. I am using the same entities elsewhere and they are perisiting just fine, so i know that the inheritance and class mappings are working perfectly, except for the fact that hibernate is not helping me to fill in the discriminator type column, and neither is that of my concern anymore. Its only when i call the addOption method that the commit fails.

Kindly help.

Regards,
Ryan Sukale
Hi xsunil,

here is the code

I feel like i have hit a wall. Its been 3 days since i have been unable to make any progress.

All the properties of the objects are populated before the method is called. The 'MyEntity' already exists in the database, so merge does not fail.

The code is to insert a option which is of type 'SubEntity' into the database. If i remove the not null constraints from the database, it inserts the rows successfully, but except the id's, all the remaining columns get a null or their default values.

What am i doing wrong? Even the prepersist method in the entity SubEntity and BaseEntity print the values of their instances to the values that i have initialized. But its just not going into the database.



Regards,
Ryan Sukale
Hi Adolfo,

I remember using Tomcat for running EJB's for experimental purposes and used a container managed entity manager. But since tomcat is not a EJB server, i had to use the JBoss embedded server for it. Using container managed transactions for an application that does not use EJB's seems quite out of place. If one could do that then one could just make any web app and leave the transaction management issues to the container, making life a lot easy(at least for small applications).

I think that the using container managed transactions and entitymanagers is closely bound to EJB's because the container knows about the life cycle of the EJB's and is in absolute control of their creation/access/destruction have complex processes to handle their memory issues. Quite unlike your own classes that would always be at the mercy of your mind. Its like you sacrifice the control over your classes to gain privileges like container managed transactions. But i dont know for sure what's the reason.

If there is a way to use container managed transactions elsewhere, i would certainly like to know how to do that as well.

Regards,
Ryan Sukale
Hi All,

I have been facing a very typical problem since the past 7-8 hours.
I have been trying to persist an entity instance using hibernate. The entity maps to a database table whose columns have a 'not null' constraint.

I initialize all the instance variables of the entity before calling the persist method. I have a @PrePersist, and @PostPersist method that prints the values of these instance variables. However, when hibernate generates sql for the database, it is inserting null values in the columns and hence failing with constraint violation errors.

The column mappings are correct, or else even the insert statements would not have been generated by hibernate. I am going absolutely bald over the issue and have not even the slightest idea as to why am i getting a null while even the prePersist method prints the initialized values.

Has anyone faced a similar problem? Any solution??

Regards,
Ryan Sukale
Hi Deepthi,

A persistence context is somwhat like a logical collection of managed entities in the memory.
When you create an application-managed persistence context, for a given persistence unit, you are specifying the entitiy types that the persistence context will manage.

Now, when an application managed entity manager is associated with a transaction, it is bound to get in sync with the database when the transaction commits., ie, all the changes made to the entities that had become managed during the scope of the transaction using that entity manager will be saved in the database at transaction commit time. Once the transaction commits, the 'in-memory' entities are not cleared. They will still be tracked down by the entity manager in the case of an application-scoped entity manager. So, if you make changes to the managed entities when there is no transaction surrounding the entity manager, then entity manager will keep track of them.

The next time your app-managed entity manager becomes associated with a transaction, the chages will be reflected in the database when the transaction commits or a flush is done, as implemented by your persistence provider.

This indicates that your persistence context has an 'extended' lifespan, because even in the absence of a transaction, the entity instances remain managed. So the 'standalone' feature refers to the fact that persistence context 'propagation' i.e, using the same persistence context for utility method that you call from you method that creates the entity manager, is not inherently available for a app-managed entity manager but which is available in the case of a container-managed entity manager. The container handles the propagation for you behind the scenes.

In the case of a container-managed entity manager, all the entity that were managed during the transaction become detached the moment the transaction commits. So, changes made outside the transaction will not be reflected. These changes can be updated in your DB only when you try to again make these detached instances managed by the merge method.

Hope this helps

Regards
Ryan Sukale