[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[eclipselink-users] UUID eclipselink 2.5.1
|
Ancoron & others,
I did a UUID converter for Eclipselink 2.4.0, and that was working fine.
Last weekend i decided to upgrade to 2.5.1 as i was working on a new
project. I noticed that my converter class did not work anymore. Also i
am not sure if it is really needed anymore and if e.l 2.5.x supports
uuid conversion out of the box. Granted since i was using a
persistence.xml file from and old project as a template i could have
left things in there that caused my issue. With that said i updated the
converter i had done for 2.4.x to work in 2.5.x. If a converter is not
needed please let me know. Below is the converter.
UUIDConverter.java:
package com.edmann.core;
import java.util.UUID;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectCollectionMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
/**
* Supports mapping of <tt>java.util.UUID</tt> inside JPA entities.
*
* <p>
* Example usage:
* <pre>
* //..
* import java.util.UUID;
* import org.ledger.core.UUIDConverter;
* //..
*
* @Entity
*
@Converter(name="uuidConverter",converterClass=UUIDConverter.class)
* public class Elink implements Serializable {
* @Convert("uuidConverter")
* @Column(name="elink_id")
* private UUID elinkId;
* //..
* }
* </pre>
* </p>
* Updated 09/28/2013 by Edward Mann to support eclipselink 2.5.x
* @author Edward Mann
* @version 2.0
* @see
https://github.com/ancoron/pg-inet-maven/blob/master/org.ancoron.postgresql.jpa/src/main/java/org/ancoron/postgresql/jpa/eclipselink/InetAddressConverter.java
* @see
http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg07359.html
*/
public class UUIDConverter implements Converter {
/**
*
*/
private static final long serialVersionUID = -8738182233279166782L;
@Override
public Object convertObjectValueToDataValue(Object objectValue,
Session session) {
return objectValue;
}
@Override
public UUID convertDataValueToObjectValue(Object dataValue,
Session session) {
return (UUID)dataValue;
}
@Override
public boolean isMutable() {
return true;
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
final DatabaseField field;
if(mapping instanceof DirectCollectionMapping) {
// handle @ElementCollection...
field = ((DirectCollectionMapping)
mapping).getDirectField();
} else {
field = mapping.getField();
}
field.setSqlType(java.sql.Types.OTHER);
field.setTypeName("java.util.UUID");
field.setColumnDefinition("uuid");
}
}
entry in persistence.xml:
<property name="eclipselink.session.customizer"
value="com.edmann.core.UUIDSequence" />
Test file Account.java:
/**
* The persistent class for the account database table.
*
*/
@Entity
@Table(name = "account", schema = "edmann")
@Converter(name = "uuidConverter", converterClass = UUIDConverter.class)
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Convert("uuidConverter")
@UuidGenerator(name = "uuid")
@Column(name = "acct_id")
private UUID acctId;
..getters/setters...
}
If this is no longer needed please someone let me know.
Thanks