Tuesday, December 6, 2011

Persist Joda Datetime with Eclipselink

We have to create an encoder

import java.sql.Timestamp;

import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
import org.joda.time.DateTime;

public class JodaDateTimeConverter implements Converter {

 private static final long serialVersionUID = 1L;

 @Override
 public Object convertDataValueToObjectValue(Object dataValue, Session session) {
  return dataValue == null ? null : new DateTime((Timestamp) dataValue);
 }

 @Override
 public Object convertObjectValueToDataValue(Object objectValue, Session session) {
  return objectValue == null ? null : new Timestamp(((DateTime) objectValue).getMillis());
 }

 @Override
 public void initialize(DatabaseMapping mapping, Session session) {
 }

 @Override
 public boolean isMutable() {
  return false;
 }

}

Then the encoder can be used to annotate the fields:

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
public class DateToRemember {

 @Id
 @GeneratedValue(strategy = GenerationType.TABLE)
 private int   id;

 @Column(columnDefinition = "TIMESTAMP")
 @Converter(name = "dateTimeConverter", converterClass = net.xan.taskstack.JodaDateTimeConverter.class)
 @Convert("dateTimeConverter")
 private DateTime date;
}

Or the classic version:

@Entity
public class DateToRemember {

 @Id
 @GeneratedValue(strategy = GenerationType.TABLE)
 private int   id;

 @Column(columnDefinition = "TIMESTAMP")
 @Converter(name = "dateTimeConverter", converterClass = net.xan.taskstack.JodaDateTimeConverter.class)
 @Convert("dateTimeConverter")
 private DateTime date;

 public int getId(){return id;}
 public DateTime getDate(){return date;}
 public void setId(int id ){ this.id = id;}
 public void setDate(DateTime date ){ this.date = date;}

 public DateToRemember(){}
}

One To Many relationship without join table in EclpseLink

The following classes:
@Data
@Entity
public class Num {
 @Id
 @GeneratedValue
 private Integer id;

 private Integer value;

}
@Data
@Entity
public class Suma {
 @Id
 @GeneratedValue
 private Integer  id;

 private String  value;

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
 @JoinColumn( name = "sum_id")
 private List<Num> nums;
}
Generate the following tables:
CREATE TABLE SUMA (
  ID INTEGER NOT NULL,
  VALUE VARCHAR(255)
 );

CREATE UNIQUE INDEX SQL111206140004130 ON SUMA (ID ASC);

ALTER TABLE SUMA ADD CONSTRAINT SQL111206140004130 PRIMARY KEY (ID);
CREATE TABLE NUM (
  ID INTEGER NOT NULL,
  VALUE INTEGER,
  SUM_ID INTEGER
 );

CREATE INDEX SQL111206140004360 ON NUM (SUM_ID ASC);

CREATE UNIQUE INDEX SQL111206140003900 ON NUM (ID ASC);

ALTER TABLE NUM ADD CONSTRAINT SQL111206140003900 PRIMARY KEY (ID);

ALTER TABLE NUM ADD CONSTRAINT FK_NUM_SUM_ID FOREIGN KEY (SUM_ID)
 REFERENCES SUMA (ID);
CREATE TABLE SEQUENCE (
  SEQ_NAME VARCHAR(50) NOT NULL,
  SEQ_COUNT DECIMAL(15 , 0)
 );

CREATE UNIQUE INDEX SQL111206140004490 ON SEQUENCE (SEQ_NAME ASC);

ALTER TABLE SEQUENCE ADD CONSTRAINT SQL111206140004490 PRIMARY KEY (SEQ_NAME);

Apache Derby + Eclipse integration

How to Integrate Apache Derby in Eclipse.
  1. Download the latest official release of Derby from http://db.apache.org/derby/derby_downloads.html. Now it is 10.8.2.2. Get the files:
  2. Unzip The contents of the files. Choose a folder to store you libraries. I use ~/lib, but you can use whatever you want. Uncompress the files in separate folders. Both the files "derby_core_plugin" and "derby_ui_doc_plugin" create a folder named "plugins".
  3. Add the plugins to Eclipse. For this copy the "plugins" folders to your eclipse plugins directory. In my case it is "/usr/lib/eclipse/plugins" but it might be anything in your system. It is always ECLIPSE_DIR/plugins. You can either copy your uncompressed "plugins" folder to you ECLIPSE_DIR/ and merge the folders or copy the contents of your uncompressed "plugins" folder into your ECLIPSE_DIR/plugins folder. In both cases the result is that you get in your plugins folder the two following folders:
    • org.apache.derby.core_10.8.2
    • org.apache.derby.plugin.doc_1.1.2
    • org.apache.derby.ui_1.1.2
  4. Start or restart your Eclipse.



Monday, December 5, 2011

Ubuntu: Update eclipse to latest version (3.7 Indigo)

For some reason Ubuntu does't include the latest version of Eclipse in the repositories. In order to install or upgrade to latest version we can do the following:

1 - Install Eclipse from repositories. It will install Eclipse 3.5 Galileo. This is the easy way to get the integration with the system: configuration, icons, launchers from the menu and this kind of staff. If you already have an old Eclipse installed and you just want to update it, skip this step.

2 - Download the latest version of eclipse from its web site. Choose the best version for you in this page http://www.eclipse.org/downloads/compare.php

3 - Unzip the downloaded file in your desktop. The folder "eclipse" contains a full installation of eclipse. You can use it directly from the desktop just by double-clicking the file named "eclipse". Now we will substitute the previous version installed via Ubuntu for this new one.

3 - Remove the old installation of eclipse. You can do a backup of the directory if you want to preserve any plugin or whatever.

#tar -zcvf ~/Desktop/eclipse.backup /usr/lib/eclipse
#sudo rm -Rf /usr/lib/eclipse

4 - Put the new installation in its place

#sudo mv ~/Desktop/eclipse /usr/lib/eclipse

5 - Change the eclipse.ini file

#sudo cp /usr/lib/eclipse/eclipse.ini /etc/eclipse.ini
#sudo mv /usr/lib/eclipse/eclipse.ini /usr/lib/eclipse/eclipse.ini.bak
#sudo ln -s /etc/eclipse.ini /usr/lib/eclipse/eclipse.ini

6 - Tune the launcher

#sudo sed -i 's:galileo:indigo:g' /usr/bin/eclipse
#sudo sed -i 's:Galileo:Indigo:g' /usr/bin/eclipse

7 - Enjoy! Your Eclipse installation is accessible from any icon or menu item you had. Check if you have to install plugins.