aspose file tools*
The moose likes Beginning Java and the fly likes Designing a Simple Java Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Designing a Simple Java Bean" Watch "Designing a Simple Java Bean" New topic
Author

Designing a Simple Java Bean

Harish Moolchandani
Greenhorn

Joined: Mar 12, 2010
Posts: 23
Hello, I'm beginner in MVC concepts and need help in designing java beans.
I've a table names types and it has a foreign key (parentType) pointing to itself.

So what fields should I take for java bean Types.
long typeId;
String name;
String description;
long parentTypeId;

-or-

long typeId;
String name;
String description;
Type parentTypeId;

-or-


long typeId;
String name;
String description;
Type parentTypeId;
Type[] childTypes;

-or-
any other recommendation will be helpful.


???
Please Help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

I would go for a direct reference to the parent (which would be called parentType, not parentTypeId). That leaves the choice whether or not you want to store references to the children as well. The answer is, it depends. If you need to access an object's children then it's better to add them. If you only need to go "up" to the parent then you don't need them.

One more advice: don't use a Type[] for the children, use a List<Type>. That way you can add children without any problems.

You should also take care of data consistency. If, for objects x and y, x.parentType == y, then y.childTypes.contains(x) should be true and vice versa. This means that:
1) If you create a Type with a parent in the constructor, that constructor should add this to the parent's children. This does mean that you may get threading issues as you are publishing the object before it's fully initialized.
2) If you provide an addChild method, this method should remove the given child from its current parent (if any) before adding it to the current object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Harish Moolchandani
Greenhorn

Joined: Mar 12, 2010
Posts: 23
Hello Rob, thanks for reply. I've decided to code it like this:



But will it still be a javaBean, because javabean should not has a getter which accepts an argument.
Any other suggestions/advice?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You didn't follow my advice about data consistency. Consider the following example:
I also dislike the name setChild for adding children.
Harish Moolchandani
Greenhorn

Joined: Mar 12, 2010
Posts: 23
Yes, I kept your data consistency thing in mind.
I'm using a DAO for selecting types, These types will be selected from database.
The DAO will ensure that selected type will be consistent and will have proper parent child relationship. And still working on that

And changed the name to addChild().

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Designing a Simple Java Bean
 
Similar Threads
Designing a Simple Java Bean
Same old problem?
Hibernate - Mapping foreign keys tables
C++ to java
Container Error in FindBy methods in JBoss