-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Mapping between Date attribute and VARCHAR(16) column
PostPosted: Wed Apr 14, 2004 3:26 am 
Newbie

Joined: Wed Apr 14, 2004 3:10 am
Posts: 2
Location: France
Hi,

I'm porting a Php application to J2EE and have a MySql database.
It's the first time i use Hibernate and have trying to search in FAQ, Reference Doc before trying this forum.

In my "old" database, I have a table that contain a VARCHAR(16) where there is a date (example of this data : "2003-09-22 22:37"). In my Java class, I naturally have a Date attribute for this data. How can I map these data with Hibernate ? I have seen that there is custom attribute but I don't understand if this solve my problem and how to use that.

When I use a String Attribute (instead of Date) , I got the data in my Java Object but I 'd like to do the conversion automatically by Hibernate ? Is this possible ?

Thanks,

Laurent


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 5:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you show your mapping file and the query you're doing?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 5:19 pm 
Newbie

Joined: Wed Apr 14, 2004 3:10 am
Posts: 2
Location: France
I'm using the java class :

Code:
public class User {

   private int id;
   private String login;
   private String password;
   private String comments;
   private int profil;
   private String created;
   private int logout_time;
   private String lastPage;
   private String timezone;
   private int organisation;
   private Identity identity;

//  ... constructor, getters and setters

}


The mapping file I use :

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="User" table="members">
   <id name="id" type="int" unsaved-value="null">
      <column name="id" not-null="true"/>
      <generator class="native"/>
   </id>
   <property name="comments" column="comments" />
   <property name="created" column="created"/>
   <property name="lastPage" column="last_page"/>
   <property name="login" column="login"/>
   <property name="logout_time" column="logout_time"/>
   <property name="password" column="password"/>
   <property name="profil" column="profil"/>
   <property name="timezone" column="timezone"/>
   <component name="identity" class="org.jcollab.metier.Identity">
      <property name="name" column="name"/>
      <property name="title" column="title"/>
      <property name="emailWork" column="email_work"/>
      <property name="emailHome" column="email_home"/>
      <property name="phoneWork" column="phone_work"/>
      <property name="phone_Home" column="phone_home"/>
      <property name="mobile" column="mobile"/>
      <property name="fax" column="fax"/>
   </component>
        <property name="organisation" column="organization"/>
    </class>
</hibernate-mapping>


The query I use : "from User".
This query give me all the rows of my table.

I would like to use java.util.Date for the "created" attribute. In my database, the sql type tha I use is VARCHAR(16) and not TIMESTAMP.

Here is the SQL code for creating the table :

Code:
CREATE TABLE `members` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `organization` mediumint(8) unsigned NOT NULL default '0',
  `login` varchar(155) default NULL,
  `password` varchar(155) default NULL,
  `name` varchar(155) default NULL,
  `title` varchar(155) default NULL,
  `email_work` varchar(155) default NULL,
  `email_home` varchar(155) default NULL,
  `phone_work` varchar(155) default NULL,
  `phone_home` varchar(155) default NULL,
  `mobile` varchar(155) default NULL,
  `fax` varchar(155) default NULL,
  `comments` text,
  `profil` char(1) NOT NULL default '',
  `created` varchar(16) default NULL,
  `logout_time` mediumint(8) unsigned NOT NULL default '0',
  `last_page` varchar(255) default NULL,
  `timezone` char(3) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just implement a custom UserType. It is quite simple, there are examples in the Wiki Area of the website.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.