开发者

Java persistence, communications between db entities and other models

I'm using the TopLink persistence library with the MySQL db in my Java applications.

I've developed the class library which is used by several desktop applications, it contains some classes that are specific for my project. Classes are linked to each other, generally with one-to-m开发者_开发技巧any relationships.

I want to have DB Entity classes and Project-specific classes to be separate.

How should I develop the communications between this classes (inheritance, using or something?). How should the relationships between project-specific classes be developed when loading(or putting) them to the database?


I guess since it's a desktop application, it's a UI based application. So MVC architecture is most suitable here. Also I guess you would have to use Java beans for asynchronous access to the model.

First define a model with all definition required.

let say

        class person
{
        public PropertyChangeSupport propertyChange = new PropertyChangeSupport(this);

        private String firstName;
        private String middleName;
        private String lastName;
        private int gender;
        private Integer age;
        .
        .

        set Fname(String firstName)
        {
                      propertyChangeSupport.firePropertyChange("firstName",this.firstName,this.firstName         = firstName);
        }
        .
        .
        .
        public void setPropertyChange(PropertyChangeSupport propertyChange) {
                this.propertyChange = propertyChange;
        }


         public PropertyChangeSupport getPropertyChange() {
                return propertyChange;
         }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChange.removePropertyChangeListener(listener);
        }

}

Now we need a model provider, which would be a singleton pattern.

class PersonMOdelProvider
{

private static List<Person> lst ;
private static PersonMOdelProvider content;
private PersonMOdelProvider()
{
  // Get data from data base layer.
  lst = new ArrayList<Person>();
  //load the list from database
}

public static PersonModelProvider getInstance()
{
 if (content!=null) return content;
content = new PersonMOdelProvider();
return content;
}

.
.
.
set ... get methods for binding db with model. 

}

Now on the UI part you first need to get instance of model provider, and carry all further interaction through model provider.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜