File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Architectural Suggestion Needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Architectural Suggestion Needed" Watch "Architectural Suggestion Needed" New topic
Author

Architectural Suggestion Needed

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Entity.java

ServiceEntity.java

ConcreteServiceEntity.java

NonServiceEntity.java


I want to provide a common persistence method (Hibernate3) for both kind of entities (Service and NonService). I have achieved it somehow using the above class hierarchy. But If you look carefully to Entity class, you can mark that setter method of id is public. I think it should be private because of two reasons. One is my database(MySQL) supports auto increment so I never need to explicitly call the setter method and the second is I have read many articles suggesting that it should be private for security reasons also(This supports the first reason).

Any suggestion is appreciated.
Rosco Duncan
Ranch Hand

Joined: Apr 23, 2007
Posts: 41
Its worth considering why the id field is there. If it is only there to support an Object Relational mapping layer (like Hibernate through to your database) then its usually good practice to protect the setter method as much as possible. You have probably read this already elsewhere.

I think it is unlikely from your description that you would need your own programatic ability to set the id, so I dont see any reason to leave the method as public.

Cheers,
Rosco
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I think you missed my point.
Vishal Pandya wrote:I want to provide a common persistence method (Hibernate3) for both kind of entities (Service and NonService).

So the definition of my persistence method would be something like,

Now, we map JavaBean class and DB Table in hibernate like,

See the name attribute, it could be subclass of entity (Service/NonService). Now as setter method of ID is public, it would be inherited to its subclasses and hence hibernate would find the setter method and can successfully persist into database. But If I make the method private it wouldn't work obviously. So I have kept it public but ideally it should private. What the heck?

I can think of two alternatives,
1) Let the method visibility public.
2) Move the id field itself to subclasses. In that case Entity class would be empty and the placeholder just for the sake of Persistence method.(Is this good design?)

(Moderators, please don't move this thread to hibernate or any other forum, otherwise it would die.)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

A private field or method would be inherited by subclasses, just not accessible to them. There is a big difference, especially when you are talking about an O/R mapping. If you mark the id as private, and keep the get/set methods for id private, an Object of the child class still has that field and method, it just can't call it (but public or protected methods it inherits from the parent class could access it).

In Hybernate you can persist the Field ID - which can be private - using field access. This way, you can get rid of the setID() all together, and still persist the object's ID.



Note that this assumes that hybernate traverses the inheritance tree when accessing fields. I don't know enough about hybernate to be sure of that.


Steve
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
I tried googling "hybernate", and predictably, the first thing it offered was "Did you mean: hibernate?" Subsequent links didn't seem to offer any evidence of a viable subculture that spells this as "hybernate", but I confess I got bored after the second page. Dictionary.com didn't offer any support either. So Steve: is there some reason to consistently spell this as "hybernate", rather than "hibernate"?

Aside from that:
Steve Luke wrote:A private field or method would be inherited by subclasses, just not accessible to them.

That depends on your definition of what "inherit" means. The JLS definition, for one, clearly contradicts your version. In this context, that may or may not be relevant. But it's not clear that your definition is any more useful here. Sure, the presence of the field is felt, indirectly. Does that mean it's "inherited"? According to the JLS, no. YMMV and all that. But I'm not sure it's useful to draw the distinction here.

I would advise casual readers to simply skip any mention of "inheritance" or "inherited" in this discussion, and move on to more useful things.

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
@Steve Luke

First, thanks Steve, your advice seems to work. and even without changing the access type to field(default is property). But this has really confused me.
Steve Luke wrote:A private field or method would be inherited by subclasses, just not accessible to them.

Ideally, If something is inherited than and only than it could be accessible. But in my case If I make fields and get/set methods private, still it works. What is the meaning of field is inherited just not accessible?
Steve did you really say you don't know Hibernate? .

@Mike
You have been little harsh to Steve.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Vishal Pandya wrote:@Mike
You have been little harsh to Steve.


On the contrary, he is absolutely right (on both accounts, why did hybernate look right to me even with the red underline telling me it is spelled wrong?). I was loosely using a word with very specific meaning.

All I should have said was that the id field would still exist, even if the subclass can't access it. And because it is there then Hibernate should be able to find it.

Vishal Pandya wrote:What is the meaning of field is inherited just not accessible?


What I meant is that an instance of SubClassOfEntity would still have an 'id' because it is an instance of Entity, and Entity has an 'id'.

But like I said, I do not know Hibernate, I don't use it, I just surmised since Hibernate is used to persist the object's state, that would mean it would need to persist the state of the object's parent classes as well, which pretty much means it would have to be able to look up a field declared in a class, or one of its parent classes.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
I am glad to see that Steve read my comment in the spirit it was intended. Just in case anyone else thinks I was being hard on him, or perhaps attacking him in some way: based on what I've seen of his postings here, I have nothing but respect for Steve. He knows his stuff, well. And he does a great job of explaining things to others. He's aces in my book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Architectural Suggestion Needed