Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / February 2008

Tip: Looking for answers? Try searching our database.

Struts/Hibernate and date troubles

Thread view: 
fthomas - 17 Feb 2008 13:31 GMT
Dear all,

I'm getting squeezed between Struts and Hibernate when I use Date type
for a record.
My setup is the simplest possible.
This is my mappings file
<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="com.ft.ht.DictatorForm" table="DICTATOR">

   <meta attribute="class-description">
     testing Struts with Hibernate
   </meta>

   <id name="id" type="int" column="DICTATOR_ID">
     <meta attribute="scope-set">protected</meta>
     <generator class="native"/>
   </id>

   <property name="added" column="ADDED" type="timestamp"/>
   <property name="firstName" type="java.lang.String" not-null="true"
column="FIRST_NAME"/>
   <property name="lastName" type="java.lang.String" not-null="true"
column="LAST_NAME"/>
   <property name="birthDate" column="BIRTH_DATE" type="timestamp"/>
   <property name="offsprings" type="short" column="OFFSPRINGS"/>

 </class>
</hibernate-mapping>

The timestamp type gets translated into java.util.Data in the POJO and
in datetime in the
database schema. I heve MySQL as my DB.

I use the above mapping to generate a POJO which then I modify to
extend ActionForm.
I have to modify then all the setters to accept String (instead of
java.util.Date), that's to make
Struts happy.

The trouble is that Hibernate is unhappy with the setter. When I try
to commit something with Date I get :

org.hibernate.PropertyAccessException: IllegalArgumentException
occurred while calling setter of com.ft.ht.DictatorForm.birthDate

If the setters are let to accept Date then Strings shouts at me...

So far I was experimenting only with Strings and Dates. If I go only
with String everything is working...

What am I to do then? Can somebody point me to a complete example
which uses Hibernate with Struts and
commits multiple types of data to the Database (MySQL)

Cheers,
Thomas
Donkey Hot - 17 Feb 2008 16:38 GMT
fthomas <thomas.fazekas@gmail.com> wrote in news:e01e47a3-23bc-4911-8bcc-
2c7b1f87365f@72g2000hsu.googlegroups.com:

> Dear all,
>
[quoted text clipped - 58 lines]
> Cheers,
> Thomas

I have  not a master's degree on Hibernate, but what strikes to my eye is
that you declare type="java.lang.String" to strings and type="timestamp" to
dates. If java.lang.String is correct, then maybe date-columns want a java-
classdef too, no?

Dunno if it has to be java.util.Date or java.sql.Date or
java.sql.Timestamp...
fthomas - 18 Feb 2008 09:11 GMT
> fthomas <thomas.faze...@gmail.com> wrote in news:e01e47a3-23bc-4911-8bcc-
> 2c7b1f873...@72g2000hsu.googlegroups.com:
[quoted text clipped - 69 lines]
> Dunno if it has to be java.util.Date or java.sql.Date or
> java.sql.Timestamp...

Thanks the tip but unfortunately it doesn't help.
The basic problem still remains, namely Struts and Hibernate require
different types for the accessor methods.
After some thinking I've came to realize that my approach might be
wrong,
I shouldn't use the same class for the Struts ActionForm and for the
Hibernate persistence objects. It might work for simple classes where
all
the members are Strings but this is not the way to go for other types.

I'm not sure though, so I would really appreciate if somebody
with experience with both Hibernate and Struts would weigh in on this.

Thanks,
Thomas
Deepak Srivastava - 27 Feb 2008 16:44 GMT
Hi  Thomas,

The issue you addressed is simply of type conversion,
I suggest you to take the appropriate type at JAVA end,
i.e.; declare the "birthDate" as of type TimeStamp in corresponding
JAVA bean, and there's no need to explicitly mention the type in
hibernate mapping file for any of the property,
as this ORM tool is smart enough to handle the conversion between Java
type and DB type.

And other thing is, you shouldn't try to use the form beans as
persistence objects,
as both the things are supposed to serve different purposes. You can't
use these two interchangeably.

Will discuss more next time, till then solve this issue.

Hope this answers your question.

--Deepak
Deepak Srivastava - 27 Feb 2008 16:46 GMT
Hi  Thomas,

The issue you addressed is simply of type conversion,
I suggest you to take the appropriate type at JAVA end,
i.e.; declare the "birthDate" as of type TimeStamp in corresponding
JAVA bean, and there's no need to explicitly mention the type in
hibernate mapping file for any of the property,
as this ORM tool is smart enough to handle the conversion between Java
type and DB type.

And other thing is, you shouldn't try to use the form beans as
persistence objects,
as both the things are supposed to serve different purposes. You can't
use these two interchangeably.

Will discuss more next time, till then solve this issue.

Hope this answers your question.

--Deepak


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.