on 01-12-2007 8:51 AM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
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
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,
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
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
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.