2007-05-24

SimpleTapestry 5 CRUD application -Step 04 Adding some basic Hibernate features into the project.

1.Create new class package = org.example.TSA504.hibernate, name=HibernateUtil
2. Add the hibernate3.jar (I used Hibernate Core 3.2.4.SP1 ) into the project's
Java Build Path.
3. I used Hibernate on-line documentation for constructing the class
and several samples from «unknown java developers», so contact me if you think I violate someone's copyright.
4. Now, I'm using PostgreSQL 8.2 and Hibernate annotations in order to «auto-create» DB table(s), so the
hiberbate.cfg.xml file might looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost/TSA</property>
<property name="connection.username">postgre</property>
<property name="connection.password">postgre</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<property name="jdbc.use_streams_for_binary">true</property>
<property name="transaction.flush_before_completion">true</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="org.example.TSA504.beans.TUser"/>
</session-factory>
</hibernate-configuration>


I also have TSA DB already created (with zero tables at the moment, it means that I simly had created new DB and nothing more ) and I have postgre user among DB's Login Roles.

Note, the line

<property name="hbm2ddl.auto">create</property>

provides DB tables "auto-creation" (be means of Hibernate annotations). If you leave the line then every time you use a page linked to a table that table will be created again (and all previous data will gone away).
So, when all tables which you need were created - just comment the line.


(Another approach is to use (as it was pointed out by experienced Java-devs)
<property name="hibernate.hbm2ddl.auto">update</property>
but I haven't tried that yet)

5.Place hiberbate.cfg.xml in TSA504\src\main\resources\.
6.Add postgresql-8.1-409.jdbc3.jar library to the project.
7.
Let's tweak UserAuthenticator interface , add two new methods:

public
TUser getTUserByUserName(String userName);
public boolean addNewTUser(TUser tUser);

8.
We need to have just added methods implementation and to have isValid() method modified, so I suggest to have following UserAuthenticatorImpl.java (if you have a better implementation – I'd like to be taught ).
9. Except getting a «user» from DB, it's quite good to have ability to add new entries into DB. Try this – create new html file \TSA504\src\main\webapp\WEB-INF\AddNew.html and new page class org.example.TSA504.pages.AddNew.java (very similar to Login.java) . Also, modify our Start.html, add

<br/>
[<a t:type="PageLink" page="AddNew">Add New </a>]

10. Well, let's try to use all that - run the jetty and open http://localhost:8080/TSA504/start, click the AddNew link, fill in the form, submit and ... I got:

java.lang.ExceptionInInitializerError exception java.lang.NoClassDefFoundError: org/dom4j/DocumentException

There are, probably, no «expected» libraries ... so, I added all (just in case...) from hibernate-3.2\lib\ and
hibernate-annotations-3.3.0.GA\lib\hibernate-commons-annotations.jar into the project class-path.

Please remember also about ejb3-persistence.jar (if you'd like to use Hibernate annotations), see the previous post.

11. ->jetty -> http://localhost:8080/TSA504/start, -> the AddNew link, fill in the form, submit and ... well, it works at least for the 1-st time and new table is added into the DB... . There is a lot to be changed and tweaked here, so - let me learn a bit more about Java before continuation ;).

6. SimpleTapestry 5 CRUD application -Step 06, tweaking a grid a bit.
5. Simple Tapestry 5 CRUD application - BeanEditForm and Grid screencasts aprobation.
4. SimpleTapestry 5 CRUD application -Step 04 Adding some basic Hibernate features into the project.
3. SimpleTapestry 5 CRUD application -Step 03, "user" bean creation.
2. SimpleTapestry 5 CRUD application -Step 02, adding a service.
1. SimpleTapestry 5 CRUD application -Step 01.



7 comments:

Kara Lee Jones said...

onSucess does not work for the method handling the form sub mission, i had to use onActionFromNameOfForm()

also, i had to manually create the tuser table in my db along with a public schema to get hibernate not to complain. other then that works great.

Лозован Евгений said...

Are using T5.0.5?
Evan Rawson reported similar issue at http://comments.gmane.org/gmane.comp.java.tapestry.user/50905

Not sure, it's truly could be T5/0/5 issue, the original code was written with 5.0.4 .

Лозован Евгений said...

As for "i had to manually create the tuser table in my" - yes, as the post says "I also have TSA DB created (with zero tables at the moment) and I have postgre user among DB's Login Roles."

and following line in hibernate.cfg.xml

<property name="hbm2ddl.auto">create </property>

is responsible for "auto-creation" of tUser table, so, have you used it?

Kara Lee Jones said...

yes i did have

property name="hbm2ddl.auto"
create
property

it autogenerated the columns for me. but needed to have the table in there to begin with.

if i didn't have the table created i get this error


# org.hibernate.exception.SQLGrammarException
could not execute query

SQL
select this_.id as id0_0_, this_.EMAIL as EMAIL0_0_, this_.FIRSTNAME as FIRSTNAME0_0_, this_.LASTNAME as LASTNAME0_0_, this_.MI as MI0_0_, this_.PASSWORD as PASSWORD0_0_, this_.USERNAME as USERNAME0_0_ from TUSER this_ where this_.USERNAME=?
SQLException
org.postgresql.util.PSQLException: ERROR: relation "tuser" does not exist
SQLState
42P01
errorCode
0
messages
[Ljava.lang.String;@19c6163
throwableCount
2
throwables
[Ljava.lang.Throwable;@1bdbfec

# org.postgresql.util.PSQLException
ERROR: relation "tuser" does not exist

SQLState
42P01
errorCode
0
serverErrorMessage
ERROR: relation "tuser" does not exist
Stack trace

* org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
* org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
* org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
* org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
* org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
* org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255)
* org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
* org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
* org.hibernate.loader.Loader.doQuery(Loader.java:674)
* org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
* org.hibernate.loader.Loader.doList(Loader.java:2220)
* org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
* org.hibernate.loader.Loader.list(Loader.java:2099)
* org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
* org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
* org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
* org.example.TSA505.services.UserAuthenticatorImpl.getTUserByUserName(UserAuthenticatorImpl.java:84)

Лозован Евгений said...

Interesting... are you DB admin name is the same (I used postgre)?

Probably I missed something or there are too few details about the exception, whta does the server log shows?

Actually I was able to get the same only if I had an empty DB (TSA) and

<property name="hbm2ddl.auto">create </property>

was commented.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.