Hello,
I'm new to Java and hibernate. I've just passed the tutorial:
http://netbeans.org/kb/docs/java/hibernate-java-se.htmlthen I've tried to replace MySQL with SQLite.
I've added to my project:
SQLite JDBC jar from
http://www.zentus.com/sqlitejdbcSQLiteDialect from
https://github.com/gwenn/sqlite-dialectI'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