Wow. This is really nice. / Paul

Jonathan Locke wrote:

i'm working on a hibernate wicket project myself right now and i've been
checking some stuff into wicket-contrib-database under wicket-stuff.  i'm
not really developing this with other people yet, but it might be useful
anyway.

the motivation of this package is to hugely lower the entrypoint for
making db driven wicket apps.  although i can't help people learn this
right now, i'm willing to fix any important bugs because i'm using it.
otoh, i think/hope you'll find it pretty simple to understand anyway. to use, you do something like the following...

1. create DatabaseWebApplication subclass, set database and create
  custom session factory (which you probably need to do anyway)

public class VoiceTribeWebApplication extends DatabaseWebApplication
{
        public VoiceTribeWebApplication()
        {
                setDatabase(new VoiceTribeDatabase());
        }

        protected ISessionFactory getSessionFactory()
        {
                return new ISessionFactory()
                {
                        public Session newSession()
                        {
                                return new 
VoiceTribeWebSession(VoiceTribeWebApplication.this);
                        }
                };
        }

        ...
}

2. create trivial DatabaseWebSession subclass returned by custom
session factory. again, you probably need to do this anyway for a real application that has something like a user that signs in...
  so no extra work here.

public class VoiceTribeWebSession extends DatabaseWebSession
{
        // Any session data here (like signed-in user)

        public VoiceTribeWebSession(VoiceTribeWebApplication application)
        {
                super(application);
        }

        ...
}

3. create database subclass.  call any subclass-specific configuration
  methods.  then call formatTables() to set up a fresh database (or
  don't if you don't want to start over each time)

public class VoiceTribeDatabase extends MySqlHibernateDatabase
{
        public VoiceTribeDatabase()
        {
                final Configuration configuration = 
newAnnotationConfiguration();
                configuration.addClass(User.class);
                configuration.addClass(Commentary.class);
                configure(configuration);
                formatTables();
                populateTables();
        }

        private void populateTables()
        {               
                final User jonathan = new User();
                jonathan.setFirstName("Jonathan");
                jonathan.setLastName("Locke");
                
                final DatabaseSession session = newDatabaseSession();
                session.save(jonathan);
                session.close();
        }
}

4. create base web page to hold DAOs.

public class VoiceTribeWebPage extends DatabaseWebPage
{
        ...
        
        protected final UserDao getUserDao()
        {
                return (UserDao)newDao(UserDao.class);
        }
}

5. create DAOA impl. for your particular database subtype
  (note, only find methods are required since save/update/etc
   are generic to any Database/DatabaseSession subclass)

public final class UserDao extends HibernateDao
{
        public User findUser(String email)
        {
                try
                {
                        Query query = getHibernateSession().createQuery(
                                        "from " + User.class.getName() + " as u 
where u.email = :email");
                        query.setString("email", email);
                        return (User)query.uniqueResult();
                }
                catch (Exception e)
                {
                        throw new DatabaseException(e);
                }
        }

        public List findUsers()
        {
                return super.findAll(User.class);
        }
}

6. subclass your new base page with the DAOs in it and
  use them...  when you're ready to save/update/etc,
  there are generic methods in DatabaseWebPage.

public class UserEditPage extends VoiceTribeWebPage
{
        public UserEditPage()
        {
                final User user = getUserDao().findUser(...);
                if (user != null)
                {
                        add(new UserEditForm(user));
                }
        }

        public class UserEditForm extends UploadForm
        {
                ...

                @Override
                protected void onSubmit()
                {
                        User user = (User)getModelObject();

// Update using database associated with // application that created this web page
                        update(user);
                }
        }
}




-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to