You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Mattias Malmgren <ma...@freefarm.se> on 2006/10/02 12:37:13 UTC
J2EE EJB design question
Hi
Perhaps out of topic, but anyway, this is my question:
As far as I can understatnd, an enity EJB can not have business methods,
just getter/setter methods for the, in the database underlaying fileds in a
table, along with the methods in the EntityBean interface.
Say I have an Employee object like this:
class Employee {
int id;
String name;
double salary;
void changeSalary(double percentage) {
salary = salary + (salary * (precentage / 100));
}
void save() {
myDatabase.saveEmployee(this);
}
}
Using EJB what do I do? In an enity EJB I can't have the changeSalary
method? If I have it as a Session Bean, I can't have presistance and save
the objects data? What is the design soulotion?
Best regards // Mattias
Re: J2EE EJB design question
Posted by Lasantha Ranaweera <la...@opensource.lk>.
Hi Mattias,
Following is a better way to this thing.
1. Define a Session Bean which act as a Facade.
Say EmployeeServiceBean class with changeEmplpyeeSalary(int id, double
percentage)
2. That method will be some thing like this.
changeSalary(int id, double percentage) {
//EJB Home Find ---
Employee emp = empHome.findByPrimaryKey(id);
emp.setSalaray(emp.getSalary()*pesentage);
}
In this kind of solution Entity Bean will take care of database saving
part. It will automatically save at the end of setSalary() method (This
will be handled in the end of a Transaction).
Regards,
Lasantha Ranaweera
Mattias Malmgren wrote:
> Hi
>
> Perhaps out of topic, but anyway, this is my question:
>
> As far as I can understatnd, an enity EJB can not have business
> methods, just getter/setter methods for the, in the database
> underlaying fileds in a table, along with the methods in the
> EntityBean interface.
>
> Say I have an Employee object like this:
>
> class Employee {
> int id;
> String name;
> double salary;
>
> void changeSalary(double percentage) {
> salary = salary + (salary * (precentage / 100));
> }
> void save() {
> myDatabase.saveEmployee(this);
> }
> }
>
> Using EJB what do I do? In an enity EJB I can't have the changeSalary
> method? If I have it as a Session Bean, I can't have presistance and
> save the objects data? What is the design soulotion?
>
> Best regards // Mattias
>
>
Re: J2EE EJB design question
Posted by Chris Brooking <cj...@googlemail.com>.
I agree totally. Whereas it is possible to have business methods within
entity beans it is much cleaner to leave them to deal solely with
persistence and use session beans for business methods
On 10/2/06, Dasun Perera <da...@gmail.com> wrote:
>
> Hi Mattias,
>
> Whether you should go with entity beans for data persistence is a
> subjective
> decision, but if you prefers that way then, you should have a session bean
> as well as an entity bean and changeSalary method should be implemented in
> Session bean which calls entity bean for data persistence.
>
> Dasun Perera
>
> -----Original Message-----
> From: Mattias Malmgren [mailto:mattias@freefarm.se]
> Sent: Monday, October 02, 2006 1:37 PM
> To: user@geronimo.apache.org
> Subject: J2EE EJB design question
>
> Hi
>
> Perhaps out of topic, but anyway, this is my question:
>
> As far as I can understatnd, an enity EJB can not have business methods,
> just getter/setter methods for the, in the database underlaying fileds in
> a
> table, along with the methods in the EntityBean interface.
>
> Say I have an Employee object like this:
>
> class Employee {
> int id;
> String name;
> double salary;
>
> void changeSalary(double percentage) {
> salary = salary + (salary * (precentage / 100));
> }
> void save() {
> myDatabase.saveEmployee(this);
> }
> }
>
> Using EJB what do I do? In an enity EJB I can't have the changeSalary
> method? If I have it as a Session Bean, I can't have presistance and save
> the objects data? What is the design soulotion?
>
> Best regards // Mattias
>
>
>
RE: J2EE EJB design question
Posted by Dasun Perera <da...@gmail.com>.
Hi Mattias,
Whether you should go with entity beans for data persistence is a subjective
decision, but if you prefers that way then, you should have a session bean
as well as an entity bean and changeSalary method should be implemented in
Session bean which calls entity bean for data persistence.
Dasun Perera
-----Original Message-----
From: Mattias Malmgren [mailto:mattias@freefarm.se]
Sent: Monday, October 02, 2006 1:37 PM
To: user@geronimo.apache.org
Subject: J2EE EJB design question
Hi
Perhaps out of topic, but anyway, this is my question:
As far as I can understatnd, an enity EJB can not have business methods,
just getter/setter methods for the, in the database underlaying fileds in a
table, along with the methods in the EntityBean interface.
Say I have an Employee object like this:
class Employee {
int id;
String name;
double salary;
void changeSalary(double percentage) {
salary = salary + (salary * (precentage / 100));
}
void save() {
myDatabase.saveEmployee(this);
}
}
Using EJB what do I do? In an enity EJB I can't have the changeSalary
method? If I have it as a Session Bean, I can't have presistance and save
the objects data? What is the design soulotion?
Best regards // Mattias