UUID as entity identifier

This commit is contained in:
Konstantin Krivopustov 2008-11-07 15:35:10 +00:00
parent 9d4f28f634
commit 3785cc2b21
11 changed files with 192 additions and 12 deletions

View File

@ -1,6 +1,7 @@
create table SYS_SERVER (
ID varchar(36),
NAME varchar(255),
ADDRESS varchar(255),
IS_RUNNING smallint,
primary key (NAME)
primary key (ID)
);

View File

@ -3,11 +3,14 @@
<persistence-unit name="cuba" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<class>com.haulmont.cuba.core.entity.BaseUuidEntity</class>
<class>com.haulmont.cuba.core.entity.Server</class>
<properties>
<property name="openjpa.Log" value="log4j"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/>
<property name="openjpa.jdbc.DBDictionary" value="hsql(SimulateLocking=true)"/>
<property name="openjpa.jdbc.MappingDefaults"
value="FieldStrategies='java.util.UUID=com.haulmont.cuba.core.persistence.UuidValueHandler'"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -10,7 +10,11 @@
package com.haulmont.cuba.core.entity;
import java.io.Serializable;
import java.util.UUID;
public interface BaseEntity extends Serializable
public interface BaseEntity<T> extends Serializable
{
T getId();
UUID getUuid();
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 07.11.2008 16:38:30
* $Id$
*/
package com.haulmont.cuba.core.entity;
import java.util.UUID;
public class BaseLongIdEntity implements BaseEntity<Long>
{
private Long id;
private UUID uuid;
public Long getId() {
return id;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 07.11.2008 16:40:37
* $Id$
*/
package com.haulmont.cuba.core.entity;
import org.apache.openjpa.persistence.Persistent;
import javax.persistence.MappedSuperclass;
import javax.persistence.Id;
import javax.persistence.Column;
import java.util.UUID;
@MappedSuperclass
public class BaseUuidEntity implements BaseEntity<UUID>
{
@Id
@Column(name = "ID")
@Persistent
private UUID id;
public BaseUuidEntity() {
id = UUID.randomUUID();
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public UUID getUuid() {
return id;
}
public void setUuid(UUID id) {
this.id = id;
}
}

View File

@ -17,7 +17,7 @@ import javax.persistence.Column;
@Entity
@Table(name = "SYS_SERVER")
public class Server implements BaseEntity
public class Server extends BaseUuidEntity
{
@Column(name = "NAME")
private String name;

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 07.11.2008 19:09:04
* $Id$
*/
package com.haulmont.cuba.core.persistence;
import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.jdbc.kernel.JDBCStore;
import java.util.UUID;
public class UuidValueHandler extends AbstractValueHandler
{
public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean adapt) {
Column col = new Column();
col.setName(name);
col.setJavaType(JavaSQLTypes.STRING);
col.setSize(-1);
return new Column[]{col};
}
public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
return ((UUID) val).toString();
}
public Object toObjectValue(ValueMapping vm, Object val) {
return UUID.fromString((String) val);
}
}

View File

@ -14,6 +14,7 @@ import junit.framework.TestCase;
public class CubaTestCase extends TestCase
{
protected void setUpDeploymentFiles() {
TestContainer.addDeploymentFile("cuba-core-intf.jar");
TestContainer.addDeploymentFile("20-cuba-core.jar");
}

View File

@ -11,15 +11,76 @@ package com.haulmont.cuba.core;
import com.haulmont.cuba.core.entity.Server;
import javax.transaction.*;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.UUID;
public class PersistenceTest extends CubaTestCase
{
public void test() {
UUID id;
beginTran();
try {
EntityManagerAdapter em = Locator.getEntityManager();
assertNotNull(em);
Server server = new Server();
id = server.getId();
server.setName("localhost");
server.setAddress("127.0.0.1");
server.setRunning(true);
em.persist(server);
commitTran();
} catch (Exception e) {
rollbackTran();
throw new RuntimeException(e);
}
EntityManagerAdapter em = Locator.getEntityManager();
assertNotNull(em);
Server server = new Server();
server.setName("localhost");
server.setAddress("127.0.0.1");
server.setRunning(true);
em.persist(server);
Server server = em.find(Server.class, id);
assertEquals(id, server.getId());
}
private void beginTran() {
try {
getTransactionManager().begin();
} catch (NotSupportedException e) {
throw new RuntimeException(e);
} catch (SystemException e) {
throw new RuntimeException(e);
}
}
private void commitTran() {
try {
getTransactionManager().commit();
} catch (RollbackException e) {
throw new RuntimeException(e);
} catch (HeuristicMixedException e) {
throw new RuntimeException(e);
} catch (HeuristicRollbackException e) {
throw new RuntimeException(e);
} catch (SystemException e) {
throw new RuntimeException(e);
}
}
private void rollbackTran() {
TransactionManager tm = getTransactionManager();
try {
if (tm.getStatus() == Status.STATUS_ACTIVE)
tm.rollback();
} catch (SystemException e) {
throw new RuntimeException(e);
}
}
private TransactionManager getTransactionManager() {
Context ctx = Locator.getJndiContext();
try {
return (TransactionManager) ctx.lookup("java:/TransactionManager");
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -73,8 +73,8 @@
<bean name="DefaultDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
<property name="driverClass">org.hsqldb.jdbcDriver</property>
<property name="connectionURL">jdbc:hsqldb:.</property>
<!-- <property name="connectionURL">jdbc:hsqldb:./hypersonic/localDB</property> -->
<!--<property name="connectionURL">jdbc:hsqldb:.</property>-->
<property name="connectionURL">jdbc:hsqldb:hsql://localhost/cubadb</property>
<property name="userName">sa</property>
<property name="jndiName">java:/DefaultDS</property>
<property name="minSize">0</property>

View File

@ -19,7 +19,7 @@
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Messagen -->