EJB/CMP entity bean question

D

Donkey Hot

Lets have an entitybean, say UserBean

class UserBean extends EntityBean
{
Long id ;
String name ;
Integer height ;
...
// and the getters and setters
}


// and the Local and Remote interfaces... and a POJO with similar
// properties..


How does the EJB (2.x) container generate SQL when updating entitybeans
properties?

I have seen code like this

void updateUser(UserPOJO user)
{
UserLocal userLocal = UserLocalHome.findByPrimaryKey(
new UserKey(user.getId()) ; // or something

if (!user.getName().equals(userLocal.getName())
userLocal.setName(user.getName()) ;

if (!user.getHeight().equals(userLocal.getHeight())
userLocal.setHeight(user.getHeight()) ;
}

Do the ejb setters mark properties as dirty, and generate SQL according to
that, or would the end result be same, if the all properties would be
always updated without checking if there is any difference?
 
A

Ajay

Lets have an entitybean, say UserBean

class UserBean extends EntityBean
{
        Long id ;
        String name ;
        Integer height ;
        ...
        // and the getters and setters

}

// and the Local and Remote interfaces... and a POJO with similar
// properties..

How does the EJB (2.x) container generate SQL when updating entitybeans
properties?

I have seen code like this

void updateUser(UserPOJO user)
{
        UserLocal userLocal = UserLocalHome.findByPrimaryKey(
                new UserKey(user.getId()) ; // or something

        if (!user.getName().equals(userLocal.getName())
                userLocal.setName(user.getName()) ;

        if (!user.getHeight().equals(userLocal.getHeight())
                userLocal.setHeight(user.getHeight()) ;

}

Do the ejb setters mark properties as dirty, and generate SQL according to
that, or would the end result be same, if the all properties would be
always updated without checking if there is any difference?


The container is free to implement in whatever fashion their
respective hearts (or brains) desire.

Why is this of interest to you?
If this is an academic exercise, I suggest you look at jboss's code -
which I suspect uses hibernate under the wraps.

Ajay
 
D

Donkey Hot

The container is free to implement in whatever fashion their
respective hearts (or brains) desire.

Why is this of interest to you?
If this is an academic exercise, I suggest you look at jboss's code -
which I suspect uses hibernate under the wraps.

Ajay

I just want to write efficient ejb code.. I'm quite new with ejb's.

My job is to maintain an existing application, and we use XDoclet to
generate code. My snippet may not be perfect, as did not write that from
work, where all the code is.

We use JBoss for development, but I will not dive into it's code to see
that ;D

I was after well known best practises, if any exists. I believe there are
good patterns, ejb techology is not anything new. I'm just a newbie with
ejb..
 
A

Arved Sandstrom

Donkey Hot said:
I just want to write efficient ejb code.. I'm quite new with ejb's.

My job is to maintain an existing application, and we use XDoclet to
generate code. My snippet may not be perfect, as did not write that from
work, where all the code is.

We use JBoss for development, but I will not dive into it's code to see
that ;D

I was after well known best practises, if any exists. I believe there are
good patterns, ejb techology is not anything new. I'm just a newbie with
ejb..

The best pattern for update is simply this - ejbStore() is doing it in both
cases. For CMP ejbStore() will be empty. For BMP, ejbStore() [which you
write] acquires a connection, you set the values of the persistent fields in
a PreparedStatement, and call executeUpdate on the PreparedStatement. You're
not explicitly calling ejbStore() in either case.

In the BMP case, where you write the code, you are writing the literal SQL
for the UPDATE in the ejbStore() method.

AFAIK the container is just going to do a straightforward SQL UPDATE in both
cases.

The above comments are for EJB 2.x with no persistence frameworks.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top