This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Wicket-contrib-database-usage
Skip to end of metadata
Go to start of metadata

From the wicket-user list

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
{
{panel}
       public VoiceTribeWebApplication()
       {
               setDatabase(new VoiceTribeDatabase());
       }
{panel}

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

{panel}
       ...
{panel}
}

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
{
{panel}
       // Any session data here (like signed-in user)
{panel}

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

{panel}
       ...
{panel}
}

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
{
{panel}
       public VoiceTribeDatabase()
       {
               final Configuration configuration = newAnnotationConfiguration();
               configuration.addClass(User.class);
               configuration.addClass(Commentary.class);
               configure(configuration);
               formatTables();
               populateTables();
       }
{panel}

{panel}
       private void populateTables()
       {
               final User jonathan = new User();
               jonathan.setFirstName("Jonathan");
               jonathan.setLastName("Locke");
{panel}

{panel}
               final DatabaseSession session = newDatabaseSession();
               session.save(jonathan);
               session.close();
       }
{panel}
}

4. create base web page to hold DAOs.

public class VoiceTribeWebPage extends DatabaseWebPage
{
{panel}
       ...
{panel}

{panel}
       protected final UserDao getUserDao()
       {
               return (UserDao)newDao(UserDao.class);
       }
{panel}
}

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
{
{panel}
       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);
               }
       }
{panel}

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

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
{
{panel}
       public UserEditPage()
       {
               final User user = getUserDao().findUser(...);
               if (user != null)
               {
                       add(new UserEditForm(user));
               }
       }
{panel}

{panel}
       public class UserEditForm extends UploadForm
       {
               ...
{panel}

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

{panel}
                       // Update using database associated with
                       // application that created this web page
                       update(user);
               }
       }
{panel}
}
  • No labels