cancel
Showing results for 
Search instead for 
Did you mean: 

EJB QL statements

Former Member
0 Kudos

Hi all,

I'm using EJB, an entity and a session Bean, in a similar way of the common known example "BonusCalculation".

I would like to update a row in my defined database but I have read that it is not posible to use UPDATE statement with EJB QL.

Anyone has ideas about how to update rows in EJB? (using entity and session beans)

very thanks in advance,

ivan.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ivan,

EJB QL is used to define the queries for finder & select methods of an entity bean, cannot be used to for DB updates. How you update rows in database depends on type of entity bean you are using. If it is CMP, you do mapping in deployment descriptor. If it is BMP, then you write SQL to handle this. You do the same thing with session beans by writing SQL to handle your DB operations.

Thanks

Vijay

Former Member
0 Kudos

Hi Vijay,

Thanks very much for your response.

It will be a big favor if you could tell me how to "do mapping in deployment descriptor" since I'm using a CMP entity bean. Or how to do it with SQL in a session bean.

Thanks in advance,

Ivan.

Former Member
0 Kudos

Hi Ivan,

Look at this SAP library.

http://help.sap.com/saphelp_nw04s/helpdata/en/f6/6a9266482d114f9b3e312768578c94/frameset.htm

Also look at sun tutorial for General introduction to EJB..

http://java.sun.com/javaee/5/docs/tutorial/doc/

If you are beginner with EJB, its going to huge learning curve for you.

All the best.

Thanks

Vijay

Former Member
0 Kudos

Hi Vijay,

Great Links, thanks.

Now I understant what you ment with "deployment descriptors". It means editing ejb-j2ee-engine.xml, ejb-jar.xml and persistent.xml.

In fact, I have already done all of this. I got the application to create new rows or return all rows using the entity bean methods called by session bean methods. But do not see what should I define or which metods from the entity bean should I use for updating a row.

I will follow all the tutorial of the link, but I hope you could give me another hint.

ThanksVijay.

Former Member
0 Kudos

Hi Ivan,

In the of CMP persistence is handled by the EJB container. Container call ejbStore() method to synchronise the data. Say for example if you want to update customer row in a database say his age, You will abstract getters/setters in bean class. like this

public abstract String getAge();

public abstract void setAge(String age);

in your remote interface class

public String getAge() throws RemoteException;

public void setAge(String age) throws RemoteException;

in the client you update age

Customer cus = (Customer ) PortableRemoteObject.narrow(home.findByPrimaryKey(new Customer PK(customerID)),Customer.class);

cus.setAge("25");

That should do. Code may not work, but should to something similar.

Thanks

Vijay

Former Member
0 Kudos

thanks very much,

this should work, i'll try it! 10 points for you!

The last question before ending the thread, should I write something inside the ejbStore() method?

thnanks, great!

Former Member
0 Kudos

Hi Ivan,

If it is CMP then just write

public void ejbStore(){

//Don't implement

}

If it is BMP then implement ejbStore() to handle your updates.

Thanks

Vijay

Former Member
0 Kudos

Hi Vijay;

I'm getting trouble with:

Customer cus = (Customer ) PortableRemoteObject.narrow(home.findByPrimaryKey(new Customer PK(customerID)),Customer.class); 

I'm using a similar one:


bonusHome = (BonusLocalHome)ctx.lookup("java:comp/env/ejb/BonusBean");
BonusBean bonusBean = (BonusBean)PortableRemoteObject.narrow(bonusHome.findByPrimaryKey("Ivan"),BonusBean.class);
bonusBean.setBonus("2,57");

I'm getting ClassCastException...Maybe it's not so easy changing between the classes of the bean:

BonusBean.java

BonusLocal.java

BonusLocalHome.java

What do you think about it?

Regards,

Former Member
0 Kudos

Hi Ivan,

In EJB you are not allowed to call bean class directly. This line does not work

BonusBean bonusBean = (BonusBean)PortableRemoteObject.narrow(bonusHome.findByPrimaryKey("Ivan"),BonusBean.class);

Try this

//Creating JNDI Contex

Context jndiContext = new InitialContext(env);

//Doing a JNDI Lookup on Home Interface

// The name you put here must match the name in ejb-jar.xml in the deployment jar.

Object ref = jndiContext.lookup("java:comp/env/ejb/BonusBean");

//Upcasting to Home Interface

BonusHome bonusHome = = (BonusHome ) PortableRemoteObject.narrow(ref, Bonus.class);

//Now, looking up Bonus to see if it's there

Bonus bonus = bonusHome.findByPrimaryKey("yourbonuskey");

//Got hold of bonus.

bonus.setBonus("2,57");

Hopefully that should fix it.

Thanks

Vijay

Former Member
0 Kudos

Hi Vijay, thanks Problem Solved!!

I have used your advices and added some ideas. your last post was correct but also needed to use diferent path since I did not write any JNDI name in ejb-jar.xml. So my resolt is:


try {
		InitialContext ctx = new InitialContext();
		Object objref = ctx.lookup("sap.com/BonusEAR/BonusBean");
		BonusHome home = (BonusHome) PortableRemoteObject.narrow(objref,BonusHome.class);
		Bonus bonus = home.findByPrimaryKey("Ivan");
		bonus.setQuantity("2,57");
	} catch (Exception NamingException) {
		NamingException.printStackTrace();
	}

Hope this help to others,

Thanks

Answers (0)