Last active
January 13, 2017 15:05
-
-
Save mrotteveel/273aa9e836880211820f54ff21164ec1 to your computer and use it in GitHub Desktop.
Example of decoding UUID with eclipselink and Firebird/Jaybird
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE TODO | |
( | |
ID char(16) CHARACTER SET OCTETS NOT NULL, | |
CONSTRAINT PK_TODO PRIMARY KEY (ID) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | |
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> | |
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL"> | |
<class>nl.lawinegevaar.eclipselink.entity.Todo</class> | |
<class>nl.lawinegevaar.eclipselink.converter.UUIDAttributeConverter</class> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="org.firebirdsql.jdbc.FBDriver" /> | |
<property name="javax.persistence.jdbc.url" | |
value="jdbc:firebirdsql://localhost/d:/data/db/fb3/eclipselink.fdb?charSet=UTF-8&octetsAsBytes=true" /> | |
<property name="javax.persistence.jdbc.user" value="sysdba" /> | |
<property name="javax.persistence.jdbc.password" value="masterkey" /> | |
<!-- EclipseLink should create the database schema automatically --> | |
<!--<property name="eclipselink.ddl-generation" value="create-tables" />--> | |
<!--<property name="eclipselink.ddl-generation.output-mode" value="database" />--> | |
</properties> | |
</persistence-unit> | |
</persistence> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.lawinegevaar.eclipselink.converter; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
import java.nio.ByteBuffer; | |
import java.util.UUID; | |
/** | |
* @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a> | |
*/ | |
@Converter | |
public class UUIDAttributeConverter implements AttributeConverter<UUID, byte[]> { | |
@Override | |
public byte[] convertToDatabaseColumn(UUID uuid) { | |
if (uuid == null) return null; | |
byte[] buffer = new byte[16]; | |
ByteBuffer bb = ByteBuffer.wrap(buffer); | |
bb.putLong(uuid.getMostSignificantBits()); | |
bb.putLong(uuid.getLeastSignificantBits()); | |
return buffer; | |
} | |
@Override | |
public UUID convertToEntityAttribute(byte[] bytes) { | |
if (bytes == null) return null; | |
ByteBuffer bb = ByteBuffer.wrap(bytes); | |
long high = bb.getLong(); | |
long low = bb.getLong(); | |
return new UUID(high, low); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.lawinegevaar.eclipselink.entity; | |
import nl.lawinegevaar.eclipselink.converter.UUIDAttributeConverter; | |
import javax.persistence.Convert; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import java.util.UUID; | |
/** | |
* @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a> | |
*/ | |
@Entity | |
public class Todo { | |
@Id | |
@Convert(converter = UUIDAttributeConverter.class) | |
private UUID id; | |
public UUID getId() { | |
return id; | |
} | |
public void setId(UUID id) { | |
this.id = id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.lawinegevaar.eclipselink; | |
import nl.lawinegevaar.eclipselink.entity.Todo; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
import java.util.List; | |
import java.util.UUID; | |
/** | |
* @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a> | |
*/ | |
public class Main { | |
private static final String PERSISTENCE_UNIT_NAME = "todos"; | |
private static EntityManagerFactory factory; | |
public static void main(String[] args) { | |
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); | |
EntityManager em = factory.createEntityManager(); | |
// read the existing entries and write to console | |
Query q = em.createQuery("select t from Todo t"); | |
List<Todo> todoList = q.getResultList(); | |
for (Todo todo : todoList) { | |
System.out.println(todo.getId()); | |
} | |
System.out.println("Size: " + todoList.size()); | |
// create new todo | |
em.getTransaction().begin(); | |
Todo todo = new Todo(); | |
todo.setId(UUID.randomUUID()); | |
em.persist(todo); | |
em.getTransaction().commit(); | |
em.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>nl.lawinegevaar</groupId> | |
<artifactId>eclipselink</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.3</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>org.eclipse.persistence</groupId> | |
<artifactId>eclipselink</artifactId> | |
<version>2.6.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.firebirdsql.jdbc</groupId> | |
<artifactId>jaybird-jdk18</artifactId> | |
<version>2.2.12</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment