Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Struts/Hibernate and date troubles

 
   Database Help (Home) -> Java RSS
Next:  Cursor Error w/ ODBC SQL2000  
Author Message
fthomas

External


Since: Feb 17, 2008
Posts: 2



(Msg. 1) Posted: Sun Feb 17, 2008 5:31 am
Post subject: Struts/Hibernate and date troubles
Archived from groups: comp>lang>java>programmer, others (more info?)

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

 >> Stay informed about: Struts/Hibernate and date troubles 
Back to top
Login to vote
Donkey Hot

External


Since: Feb 01, 2008
Posts: 10



(Msg. 2) Posted: Sun Feb 17, 2008 4:38 pm
Post subject: Re: Struts/Hibernate and date troubles [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

fthomas <thomas.fazekas DeleteThis @gmail.com> wrote in news:e01e47a3-23bc-4911-8bcc-
2c7b1f87365f DeleteThis @72g2000hsu.googlegroups.com:

> 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
>

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...

 >> Stay informed about: Struts/Hibernate and date troubles 
Back to top
Login to vote
fthomas

External


Since: Feb 17, 2008
Posts: 2



(Msg. 3) Posted: Mon Feb 18, 2008 1:11 am
Post subject: Re: Struts/Hibernate and date troubles [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Feb 17, 5:38 pm, Donkey Hot <s....DeleteThis@plc.is-a-geek.com> wrote:
> fthomas <thomas.faze....DeleteThis@gmail.com> wrote in news:e01e47a3-23bc-4911-8bcc-
> 2c7b1f873....DeleteThis@72g2000hsu.googlegroups.com:
>
>
>
> > 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
>
> 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...

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
 >> Stay informed about: Struts/Hibernate and date troubles 
Back to top
Login to vote
Deepak Srivastava

External


Since: Feb 27, 2008
Posts: 2



(Msg. 4) Posted: Wed Feb 27, 2008 8:44 am
Post subject: Re: Struts/Hibernate and date troubles [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: Struts/Hibernate and date troubles 
Back to top
Login to vote
Deepak Srivastava

External


Since: Feb 27, 2008
Posts: 2



(Msg. 5) Posted: Wed Feb 27, 2008 8:46 am
Post subject: Re: Struts/Hibernate and date troubles [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: Struts/Hibernate and date troubles 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
how to use javascript functions in struts html:form - hi i have javascripts file validations.js. and i want to use these javascript functions in my jsp pages which use struts html tags.. i don't know how to invoke these functions from <html:form...(struts html tag). if it is a ordinary html form i would ...

To hibernate or not to hibernate? - Hello everyone, newbie here. I'm developing an application for a repair shop. Customer comes in with a product, gets registered and a receipt is printed out. I did it on Access five years ago and it's been working since then. But now I want to add a few....

Need some in Hibernate - Hello there, I am new in Hibernate. I look at some examples in Hibernate 3.2 There is ALWAYS a Long id in a table class, like public class State { private Long id; // no such column in my STATE table in database private String state_code; //....

Hibernate and log4j config under Tomcat. - "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works." "The XML configu...

Hibernate + Tomcat - where to bind the session to? - Hello group, I am building a web application with Tomcat and Hibernate as O/R mapper. Now my question is where should I bind the session to? At the moment I have the Hibernate Session as a ThreadLocal, so for each thread a new HibernateSession is..
   Database Help (Home) -> Java All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]