Andor Nemeth

Greenhorn
+ Follow
since Jul 06, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Andor Nemeth

Originally posted by Ann Basso:
Yes, that is what I was thinking but I don't know whether that will work. For example, if you want to get all the employees that are managed by an employee, how will you get that?

[ August 09, 2007: Message edited by: Ann Basso ]



The code example I've gave was for a many-to-one unidirectional relationship.
This way you can't determine directly the employees that are managed by an employee("manager").

For this you'll need obiviously a many-to-one bidirectional relationship:




Then I've created two methods in a session bean to play with this Employee class a bit:



Then I've created a client to locate my remote session bean and to call these two methods above!
After calling these methods this was the output on my JBoss console:

19:50:42,931 INFO [STDOUT] F�n�kJ�zsi's staff:
19:50:42,941 INFO [STDOUT] Munk�sB�la1
19:50:42,941 INFO [STDOUT] Munk�sB�la2


So, this is the soulution! It did work with this Employee class and with these annotations.


Greetings,
Andor N�meth

p.s.
It did take some time to accomplish this, because I run to this bug:
http://forum.java.sun.com/thread.jspa?threadID=5137725&tstart=60

[ August 09, 2007: Message edited by: Andor Nemeth ]

Originally posted by Ann Basso:
I am wondering whether EJB 3.0 supports hierarchical relationship in Entities. For example, you have an Employee table having EmpID and ManagerID columns, where ManagerID contains an employeeid of the manager of that employee.

How can we use 3.0 entities to model such relationship? Any idea?



Hi Ann,

i think this code represents the statements above:



I've omitted getter/setters and used public instance variables to focus on the problem.

Correct me if i was wrong!

Greetings,
Andor N�meth

Originally posted by gowher amin naik:
what is reason behind "only session bean with BMT can keep transaction open at the end of a method"?



Only STATEFUL session beans with BMT can keep a transaction open at the end of a method!
Remember, stateless and message driven beans(MDBs) are pooled, so two method calls to these types of beans might be served by two different instances! That's why transactions should be closed (commited/rolled back) before your method ends.

If you have a STATEFUL bean, then you have a "conversational state". Every method call will be served by the same instance, therefore with BMT it's possible to keep transactions open between method calls!

If you have container managed transaction(CMT), then all the transactions will last for the time of a method call. They begin when your method starts, and end when your method ends. This is maintained explicitly by the container.

Correct me if i was wrong!
Andor N�meth


The enterprise bean must not attempt to define a class in a package.



I think this statement is rather about the Reflection API or something like that...

With the Java Reflection API it's possible to do many "nasty", unusual things. For instance: accessing private class members! (Dependency injection is heavily based on this.)

This statement you've quoted (actually the EJB-container contract) prohibits some of these functions for the developer(bean provider).
That's why:

This function (defining a class in a package) is reserved for the EJB container. Allowing the enterprise bean to perform this function would create a security hole.


Correct me if I'm wrong!
Andor N�meth
[ August 07, 2007: Message edited by: Andor Nemeth ]

Originally posted by James Frankman:

If the client is on a remote host, how does the client know where the server is? Is this done in the initialcontext. Is there some setting file you need to setup?



Hi James!

Yes, it's done in the InitialContext. If you look at the InitialContext, it can take a Properties object with information about your remote server as an argument. These settings aren't the same for every app. server, so you should consult your container vendor's manual.

Actually you're locating a remote JNDI service this way.

Here's an example client code for JBoss:



I've omitted exception handling in this example. "YOUR_SERVER_IP_COMES_HERE" could be also: localhost!

Andor
[ August 08, 2007: Message edited by: Andor Nemeth ]
The container interleaves calls to the timeout callback method with the calls to the business methods and the life cycle callback methods of the bean. The time at which the timeout callback method is called may therefore not correspond exactly to the time specified at timer creation. If multiple timers have been created for a bean and will expire at approximately the same times, the Bean Provider must be prepared to handle timeout callbacks that are OUT OF SEQUENCE. The Bean Provider must be prepared to handle EXTRANEOUS calls to the timeout callback method in the event that a timer expiration is outstanding when a call to the cancellation method has been made.


I have been thinking about this text, but I'm not quite sure if I understand it. I suppose, I don't
The last sentence makes me a bit crazy!

May be some native English speaker with SCBCD ambitions could help!
Somebody!?

Greetings,
Andor
[ July 26, 2007: Message edited by: Andor Nemeth ]
First look at some web pages with BASIC information just to have some feeling! Try this one for example:
http://trailblazer.demo.jboss.com/EJB3Trail/

You'll have some feelings and you'll see something that also WORKS in practice.

Then, start with this book: "Pro EJB3 Java Persistence API"
Read this one carefully, make notes. This is a VERY GOOD book. It won't cover everything for the exam, but start with this one! It will help your understanding a lot!

After this one I would go for "OReilly Enterprise JavaBeans 3.0 5th Edition". At this level you should write also your own EJBs, Interceptors etc.

After this, use Mikalai Zaikin's study guide ( http://java.boot.by/scbcd5-guide/ ) to cover the rest what is missing from the books above! Of course, search engines are also your friends!

At least, this is the plan I follow.

Good luck!
Andor

> 1. What does the name attribute in @Stateles(name="MyTempBean") signify?



This is the <ejb-name> element for the bean if you're using an XML deployment descriptor.
Your bean will be registered under the name "MyTempBean" in JNDI, so if you want to do a JNDI lookup, you have to use this name:

Context jndiContext = new InitialContext();
Object ref = jndiContext.lookup("MyTempBean");

2. Explain the following with example:

Dependency injection is performed when the interceptor instance is created, using the naming context of the associated enterprise bean.



Interceptor objects are tied to enterprise beans they intercept.
Every enterprise bean has its OWN naming context (think of it as own JNDI registry).
Consider an interceptor as an extension to the EJB's bean instance! Interceptors are created along with bean instances. They are created, passivated, activated, destroyed with the bean instanced they belong to.

So: as interceptors have the same access to the same naming context as the beans they belong to, dependency injection will be based also on this shared naming context. (Naming context shared among the bean and it's interceptors.)

3. "If an AroundInvoke method is overridden by another method (regardless of whether that method is itself an AroundInvoke method), it will not be invoked."

Example of this - Why this rule?



Lets suppose we have two interceptor classes (a class with an @AroundInvoke method:

public class Interceptor1 {
@AroundInvoke
private Object doMyInterception(InvocationContext ic) throws Exception { //do something... }
}

public class Interceptor2 extends Interceptor1 {
@AroundInvoke
private Object doMyInterception(InvocationContext ic) throws Exception { //do someting else... }
}

In this case if we use Interceptor2 to intercept a bean, Interceptor1's doMyInterception method won't be called! It's like the normal Java override!

The second case:
Interceptor1 class is the same as above.
We change Interceptor2 a little bit:

public class Interceptor2 extends Interceptor1 {
//Overriding without annotating with @AroundInvoke
private Object doMyInterception(InvocationContext ic) throws Exception { //do someting in this regular method... }

@AroundInvoke
private Object myNewInterceptorMethod(InvocationContext ic) throws Exception { //do interception... }

} //Interceptor2

If Interceptor2 is used with an EJB, the overridden doMyInterception in Interceptor1 won't be called. myNewInterceptorMethod in Interceptor2 will be called. (Because it has been annotated with @AroundInvoke!)

Conclusion: just follow the regular Java overriding rules whether your method is annotated with @AroundInvoke!


4. @PostLoad: Methods marked with this annotation will be invoked after all eagerly fetched fields of your class have been loaded from the datastore. No other persistent fields can be accessed in this method.

Does that mean fields marked for lazy loading cannot be referred to in @PostLoad method?



I think the same as you...
People! Somebody else? ;-)


I hope i've helped.
Greetings,
Andor


Originally posted by Shivani Chandna:
I had list of doubts, If some one can clarify them.....

1. What does the name attribute in @Stateles(name="MyTempBean") signify?

2. Explain the following with example:

Dependency injection is performed when the interceptor instance is created, using the naming context of the associated enterprise bean.

3. "If an AroundInvoke method is overridden by another method (regardless of whether that method is itself an AroundInvoke method), it will not be invoked."

Example of this - Why this rule?

4. @PostLoad: Methods marked with this annotation will be invoked after all eagerly fetched fields of your class have been loaded from the datastore. No other persistent fields can be accessed in this method.

Does that mean fields marked for lazy loading cannot be referred to in @PostLoad method?



Thanks!
Shivani

I've solved this problem.
So, if you're using JBoss you have to get jbossall-client.jar into your CLASSPATH! (Be careful to import the right one, there's also a jboss-client.jar!)

Here's the client code I've used to send a message to the JMS queue destination:


[ July 22, 2007: Message edited by: Andor Nemeth ]
Hi all,

I have an MDB. It has been successfuly deployed in JBoss:



I have trouble writting a client.
How can I locate the JMS destination and send messages to it from a regular java client application?!
[ July 22, 2007: Message edited by: Andor Nemeth ]

Originally posted by Steve Jerome:
I'm new to EJB and i'm trying learn from mikalai notes.when I try to run the calculatorclient java class it's showing following error.Can anyone tell me what the problem is.


javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1414)
...




Look at this thread:
https://coderanch.com/t/90078/JBoss/JBoss-error-Could-not-obtain
[ July 12, 2007: Message edited by: Andor Nemeth ]

Originally posted by Khaled Mahmoud:
I am new to EJB and preparing for the Sun Certified Business Component Developer.
I am now reading the book EJB 3.0 for Oreilly.

How many persistence.xml and application can have?

In the chapter about EntityManger section 5.3 the book says one of the places that a persistence.xml file located is at :

A plain JAR file within the classpath of a regular Java SE program

Does this mean that I can deploy many persistence units in java application each with its own persistence.xml file in a seperete jar file.



Yes, of course it works.
I've just tried it. I've created an entity bean and packed it into a JAR file with a persistence.xml (with <persistence-unit name="domain2"> ) and i've deployed it into my EJB 3.0 container (JBoss).

Then, i've created an other JAR with an other persistence.xml(with <persistence-unit name="domain1"> ) and i've put an other entity bean with a stateless session bean into it. The stateless bean had some methods to persist and read out my entity beans from "both of the JARs" (corretly said: persistence units).

Of course, i've injected (dependency injection) TWO EntityManager references (respectively for both persistence units) into the stateless session bean like this:

@PersistenceContext (unitName="domain1")
private EntityManager em1;

@PersistenceContext (unitName="domain2")
private EntityManager em2;

persist() and find() have worked for both of the persistence units.

I hope I've helped you.
[ July 12, 2007: Message edited by: Andor Nemeth ]
Hi ashraf or anybody with some information,

could you please tell us how "deep" questions could we expect on the SCBCD 5 exam?

For example: I've just understood how interceptors work. Should I memorize also the deployment descriptor tags, or it's ok if I know what can I do and where?

I just wanted to download the 10 sample questions from Sun but they aren't available right know

Should we expect source code snippets? What kind of?
Any info would be appreciated!

Thanks for your long post! It's very valuable!

Originally posted by Senthil Kumar SS:
How dependency injection is done using DD file.I have seen the tag <injection-target> in an article. But haven't find that in specs. is this the only tag available. where can i find the information about injection tags



The <injection-target> element is used if you want to inject an EntityManagerFactory into an EJB's bean class.

<persistence-unit-ref> <persistence-unit-ref-name>persistence/YourDB</persistence-unit-ref-name>
<persistence-unit-name>YourDB</persistence-unit-name>
<injection-target> <injection-target-class>org.andir.YourBean</
injection-target-class>
<injection-target-name>yourField</injection-target-name>
</injection-target>
</persistence-unit-ref>

The EntityManager is injected into the field named yourField or passed as a parameter to a setter method named setYourField( ) in YourBean class.

(I've modified the example a bit, because i didn't wanted to quote totally the book!)