wicket and hibernate

View: New views
5 Messages — Rating Filter:   Alert me  

wicket and hibernate

by overseastars :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I just wanna know how to integrate wicket and hibernate??

can someone give me a simple example????  even just one entity is ok. I have my entities(hibernate annotation) ready and I have no ideas of making them work together. If any buddy can send me an example project, I will really appreciate it. Thanks in advance.

Regards

Re: wicket and hibernate

by Pills :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Have a look at
http://wicketstuff.org/confluence/display/STUFFWIKI/Wicket-Iolite


overseastars a écrit :

> Hi
>
> I just wanna know how to integrate wicket and hibernate??
>
> can someone give me a simple example????  even just one entity is ok. I have
> my entities(hibernate annotation) ready and I have no ideas of making them
> work together. If any buddy can send me an example project, I will really
> appreciate it. Thanks in advance.
>
> Regards
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: wicket and hibernate

by Flavius :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Here's how I do it.

I have my wicket layer call a service layer, which calls a DAO.
I'm not a big fan of a lot of layers and I like to keep my projects
flat.

So, if you want a list of users on a page, for example, you can
use any of the canned wicket tables.  Those are pretty nice.
I use DefaultDataTable unless I need something special.  So in
your page class you do something like

DefaultDataTable defaultDataTable = new DefaultDataTable("table", columnsList, new SortableUserDataProvider(userFilter), 10);

If this doesn't make sense look at the DefaultDataTable.java class in the wicket examples.

In my SortableUserDataProvider, I pass in a filter obj depending on what the user
is asking for.  This includes any search criteria, sort options, paging, etc.

My SortableUserDataProvider calls my service.  That preps the hibernate query
and calls my dao.

So for the SortableUserDataProvider you want to override the iterator() method,
something like:

public Iterator<User> iterator(int first, int count)
        {
                SortParam sp = getSort();

                userFilter.setFirstRecord(first);
                userFilter.setRecordsToReturn(count);
                userFilter.setSortCol(sp.getProperty());
                userFilter.setSortAsc(sp.isAscending());

                return UserService.getUsers(userFilter).iterator();
        }

I prep my queries in my service layer.  So something like

Criteria userCriteria = session.createCriteria(User.class)
.setFirstResult(filter.getFirstRecord())
//other filter info here as needed

List<User> userList = userCriteria.list();

Hibernate returns models and lists of models, and wicket
uses models and lists of models.  

The only catch with this is your web layer is getting hibernate
aware models, not POJOs.  So if it's a closed system where nobody
else hits your hibernate code, you're fine.  If the service layer
is an SOA type arch, you'll need to convert your hibernate models
(or the list), to equivalent pojos on select and vice-versa on saves.


The only thing I did which I regret was I defined my collections
in hibernate as Lists instead of sets.  I did this because wicket
takes a list as a param in a lot of places and Lists are generally
easier to work with.

But hibernate treats Lists as bags and when you are doing eager fetches
on multiple collections, Hibernate will complain.  
It won't let you fetch multiple bags simultaneously.  It used to though.
They keep threatening to fix it.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-1718

Re: wicket and hibernate

by overseastars :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Flavius

Very impressive. Many thanks. I learnt a lot. But I still have a question. For example, I know I should use Dao to access the persistence layer. Let's say I have 2 entities which means two classes in java. I put them in the source folder. Once I start the server, I guess they wont help me create the schema and tables because there should be a config file hibernate.cfg.xml. Now I dont have that file, I have a HibernateUtil.java which contains codes like following:
public static void main(String[] args) {
                // TODO Auto-generated method stub

// Configuration config = new Configuration().configure();
                AnnotationConfiguration config = new AnnotationConfiguration();
               
                config.addAnnotatedClass(com.xingxing.autotable.User.class);
                config.addAnnotatedClass(com.xingxing.autotable.Address.class);
                config.addAnnotatedClass(com.xingxing.autotable.Person.class);
                config.addAnnotatedClass(com.xingxing.autotable.CreditCard.class);
                config.setProperty("hibernate.show_sql", "true");
                config.setProperty("hibernate.format_sql", "true");
                config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
// config.setProperty("hibernate.connection.createDataBaseIfNotExist", "true");
                config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test?createDatabaseIfNotExist=true");
                config.setProperty("hibernate.connection.autocommit", "true");
                config.setProperty("hibernate.connection.username", "root");
                config.setProperty("hibernate.connection.password", "passw0rd");
                config.setProperty("c3p0.min_size", "5");
                config.setProperty("c3p0.max_size", "20");
                config.setProperty("c3p0.timeout", "1800");
                config.setProperty("c3p0.max_statements", "50");
                config.setProperty("hibernate.hbm2dll.auto", "create");
               
                System.out.println("Creating Tables.....");
                SchemaExport schemaExport = new SchemaExport(config);
                schemaExport.create(true, true);
               
        }


How can I just this code to run my Eclipse Dynamic Web Project so that it will create the schema and tables????

I need more java files?? Or I have to use hibernate.cfg.xml file ?? Even if the above is not a Main function, I guess I have to call this part from somewhere in wicket layer????? Would you please show me a way......

Regards





Flavius wrote:
Here's how I do it.

I have my wicket layer call a service layer, which calls a DAO.
I'm not a big fan of a lot of layers and I like to keep my projects
flat.

So, if you want a list of users on a page, for example, you can
use any of the canned wicket tables.  Those are pretty nice.
I use DefaultDataTable unless I need something special.  So in
your page class you do something like

DefaultDataTable defaultDataTable = new DefaultDataTable("table", columnsList, new SortableUserDataProvider(userFilter), 10);

If this doesn't make sense look at the DefaultDataTable.java class in the wicket examples.

In my SortableUserDataProvider, I pass in a filter obj depending on what the user
is asking for.  This includes any search criteria, sort options, paging, etc.

My SortableUserDataProvider calls my service.  That preps the hibernate query
and calls my dao.

So for the SortableUserDataProvider you want to override the iterator() method,
something like:

public Iterator<User> iterator(int first, int count)
        {
                SortParam sp = getSort();

                userFilter.setFirstRecord(first);
                userFilter.setRecordsToReturn(count);
                userFilter.setSortCol(sp.getProperty());
                userFilter.setSortAsc(sp.isAscending());

                return UserService.getUsers(userFilter).iterator();
        }

I prep my queries in my service layer.  So something like

Criteria userCriteria = session.createCriteria(User.class)
.setFirstResult(filter.getFirstRecord())
//other filter info here as needed

List<User> userList = userCriteria.list();

Hibernate returns models and lists of models, and wicket
uses models and lists of models.  

The only catch with this is your web layer is getting hibernate
aware models, not POJOs.  So if it's a closed system where nobody
else hits your hibernate code, you're fine.  If the service layer
is an SOA type arch, you'll need to convert your hibernate models
(or the list), to equivalent pojos on select and vice-versa on saves.


The only thing I did which I regret was I defined my collections
in hibernate as Lists instead of sets.  I did this because wicket
takes a list as a param in a lot of places and Lists are generally
easier to work with.

But hibernate treats Lists as bags and when you are doing eager fetches
on multiple collections, Hibernate will complain.  
It won't let you fetch multiple bags simultaneously.  It used to though.
They keep threatening to fix it.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-1718

Re: wicket and hibernate

by Flavius :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You said you were using annotations.  Just read chapter 1 of the hibernate annotations docs

http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#setup-configuration

create a class called HibernateUtil.class (or you can call it Fluffy.class, but that's not as descriptive)
and add a static SessionFactory.

public static final SessionFactory sessionFactory;


Then add an initialize method to add your annotated classes with this:

AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure();
sessionFactory = cfg
.setInterceptor(new AuditInterceptor())
.addAnnotatedClass(User.class)
//others as needed
.buildSessionFactory();

The WebApplication object for your project has an init() method.
You can call the above method from there.  When wicket starts
up, it will call that init() method first, so you can do things like
setup your app.

Or just wrap the above in a static block and you don't have to worry
about it.  The first time you access the HibernateUtil, the class loader
will run the static block.

Then in your methods you can just get a connection from the pool.

Session session = HibernateUtil.sessionFactory.openSession();

I do this a little differently than hibernate examples.  In the service, I
open the connection, get all the data I need and close the connection
at the end of the method.  The way Hibernate works is when your working
thread gets a connection, hibernate attaches that connection to the thread.
So throughout the lifecycle you can make references to related objects and
hibernate will go fetch them for you.  I don't like that technique, personally.

HTH



LightInTheBox - Buy quality products at wholesale price!