Showing posts with label jpa. Show all posts
Showing posts with label jpa. Show all posts

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