-->
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.  [ 3 posts ] 
Author Message
 Post subject: double setter mapping problem / sqlite datetime or timestmp
PostPosted: Tue Mar 22, 2011 10:34 am 
Newbie

Joined: Tue Mar 22, 2011 10:30 am
Posts: 2
Hello,

I'm new to Java and hibernate. I've just passed the tutorial: http://netbeans.org/kb/docs/java/hibernate-java-se.html
then I've tried to replace MySQL with SQLite.

I've added to my project:

SQLite JDBC jar from http://www.zentus.com/sqlitejdbc
SQLiteDialect from https://github.com/gwenn/sqlite-dialect

I've changed hibernate.cfg.xml:

<property name="hibernate.dialect">sakila.hibernate.SQLiteDialect</property>
<property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
<property name="hibernate.connection.url">jdbc:sqlite:sakila.db</property>
<property name="hibernate.show_sql">true</property>

I've reloaded table sakila.actor from MySQL to SQLite:

sqlite> .schema actor
CREATE TABLE actor ( actor_id integer primary key, first_name varchar(50), last_name varchar(50), last_update datetime );

sqlite> select * from actor limit 1;
1|PENELOPE|GUINESS|2006-02-15 03:34:33


And everythig is "almost" working. The problem is in mapping last_update column - in compiled Java-app it is shown as 1970-01-01... i've tried also with timestamp column and java.sql.Timestamp fields in model - the same results.

I've changed mapping: src/sakila/entity/Actor.hbm.xml

<property name="lastUpdate" type="string">
<column name="last_update" not-null="true"/>
</property>

and the model class: src/sakila/entity/Actor.java

private Date lastUpdate;

public Date getLastUpdate() {
return lastUpdate;
}

public void setLastUpdate(String lastUpdate) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
this.lastUpdate = (Date) df.parse(lastUpdate);
}

and it works "fine" but if I want to add second overloaded setter:

public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}

hibernate tries to automagically use the setter and Exception occurs:

2011-03-22 15:26:37 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
SEVERE: IllegalArgumentException in class: sakila.entity.Actor, setter method of property: lastUpdate
2011-03-22 15:26:37 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
SEVERE: expected type: java.util.Date, actual value: java.lang.String
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of sakila.entity.Actor.lastUpdate
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
...

And the questions are:
-is it good idea to have such setter (with String-to-Date converter) ?
-is it possible to have two setters and tell hibernate to use proper one ?
-is there another solution ?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: double setter mapping problem / sqlite datetime or timestmp
PostPosted: Wed Mar 23, 2011 7:13 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

You should be able to map your date directly without using a converter ...

If you want only the date without the time, use a java.util.Date field with type="date" in your mapping
If you want the date and the time, use a java.util.Date field with type="timestamp" in your mapping

Regarding the setters (it's the same for getters), for a given property you should define only one setter with the name set<PropertyName>(...) because Hibernate uses reflection!


Top
 Profile  
 
 Post subject: Re: double setter mapping problem / sqlite datetime or timestmp
PostPosted: Wed Mar 23, 2011 8:44 am 
Newbie

Joined: Tue Mar 22, 2011 10:30 am
Posts: 2
Hi,

I've corrected mapping and setter as you adviced ( to java.util.Date and type="timestamp" ), converted field to timestamps in sakila.db using:

update actor set last_update = strftime('%s', last_update);

result was the same as earlier: 1970-01-01... then I realized that timestamps should be in miliseconds! And now it works!

Thank you very much for answer

greetings


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.