HHH-17187 - Disallow UUIDs with a trailing 0 byte in tests

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2023-09-08 22:58:55 +02:00 committed by Christian Beikov
parent b38bc76d87
commit 7d197329d6
50 changed files with 233 additions and 111 deletions

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.circle.identity;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.circle.sequence;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.multicircle.jpa.identity;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -30,7 +32,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.multicircle.jpa.sequence;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.multicircle.nonjpa.identity;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -30,7 +32,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.annotations.cascade.multicircle.nonjpa.sequence;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -25,7 +25,7 @@ public class MapKeyEnumeratedTest extends BaseCoreFunctionalTestCase {
public void testMapKeyEnumerated() {
Session s = openSession();
s.beginTransaction();
User user = new User(SocialNetwork.STUB_NETWORK_NAME, "facebookId");
User user = new User("User1", SocialNetwork.STUB_NETWORK_NAME, "facebookId");
s.save( user );
s.getTransaction().commit();
s.close();

View File

@ -24,8 +24,6 @@ import jakarta.persistence.UniqueConstraint;
@Table(name = "social_network_profile", uniqueConstraints = {@UniqueConstraint(columnNames = {"social_network", "network_id"})})
public class SocialNetworkProfile {
@jakarta.persistence.Id
@jakarta.persistence.GeneratedValue(generator = "system-uuid")
@org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid2")
@jakarta.persistence.Column(name = "id", unique = true)
private String id;
@ -44,6 +42,7 @@ public class SocialNetworkProfile {
}
protected SocialNetworkProfile(User user, SocialNetwork socialNetworkType, String networkId) {
this.id = "snp_" + networkId + "_" + user.getId();
this.user = user;
this.socialNetworkType = socialNetworkType;
this.networkId = networkId;

View File

@ -26,8 +26,6 @@ import java.util.Map;
@Table( name = "USER_TABLE" )
public class User {
@jakarta.persistence.Id
@jakarta.persistence.GeneratedValue(generator = "system-uuid")
@org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid2")
@jakarta.persistence.Column(name = "id", unique = true)
private String id;
@ -39,7 +37,8 @@ public class User {
protected User() {
}
public User(SocialNetwork sn, String socialNetworkId) {
public User(String id, SocialNetwork sn, String socialNetworkId) {
this.id = id;
SocialNetworkProfile profile = new SocialNetworkProfile(this, sn, socialNetworkId);
socialNetworkProfiles.put(sn, profile);
}

View File

@ -12,6 +12,7 @@ import java.util.UUID;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
/**
* Unlike Hibernate's UUID generator. This avoids
@ -24,7 +25,7 @@ import org.hibernate.id.IdentifierGenerator;
public class UUIDGenerator implements IdentifierGenerator {
@Override
public Object generate(SharedSessionContractImplementor session, Object entity) throws HibernateException {
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
String sud = uuid.toString();
System.out.println("uuid="+uuid);
sud = sud.replaceAll("-", "");

View File

@ -12,6 +12,7 @@ import java.util.UUID;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
/**
* Unlike Hibernate's UUID generator. This avoids
@ -24,7 +25,7 @@ import org.hibernate.id.IdentifierGenerator;
public class UUIDGenerator implements IdentifierGenerator {
@Override
public Object generate(SharedSessionContractImplementor session, Object entity) throws HibernateException {
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
String sud = uuid.toString();
System.out.println("uuid="+uuid);
sud = sud.replaceAll("-", "");

View File

@ -10,12 +10,12 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.hibernate.Hibernate;
import org.hibernate.Transaction;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.orm.test.bootstrap.binding.annotations.embedded.FloatLeg.RateIndex;
import org.hibernate.orm.test.bootstrap.binding.annotations.embedded.Leg.Frequency;
import org.hibernate.query.Query;
@ -772,7 +772,7 @@ public class EmbeddedTest {
scope.inTransaction(
session -> {
Book b = new Book();
b.setIsbn( UUID.randomUUID().toString() );
b.setIsbn( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
b.setSummary( new Summary() );
b = (Book) session.merge( b );
}

View File

@ -15,7 +15,8 @@ import org.junit.runner.RunWith;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
/**
* @author Luis Barreiro
@ -26,7 +27,7 @@ public class OneToOneAssociationTest {
@Test
public void test() {
User user = new User();
user.setLogin( UUID.randomUUID().toString() );
user.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
Customer customer = new Customer();
customer.setUser( user );
@ -37,7 +38,7 @@ public class OneToOneAssociationTest {
EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" );
User anotherUser = new User();
anotherUser.setLogin( UUID.randomUUID().toString() );
anotherUser.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
customer.setUser( anotherUser );
@ -52,7 +53,7 @@ public class OneToOneAssociationTest {
@Test
public void testSetNull() {
User user = new User();
user.setLogin( UUID.randomUUID().toString() );
user.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
Customer customer = new Customer();
customer.setUser( user );

View File

@ -16,7 +16,8 @@ import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.getFieldByReflection;
import static org.junit.Assert.assertEquals;
@ -30,7 +31,7 @@ public class ExtendedAssociationManagementTest {
@Test
public void test() {
User user = new User();
user.login = UUID.randomUUID().toString();
user.login = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
Customer customer = new Customer();
customer.user = user;
@ -41,7 +42,7 @@ public class ExtendedAssociationManagementTest {
EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" );
User anotherUser = new User();
anotherUser.login = UUID.randomUUID().toString();
anotherUser.login = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
customer.user = anotherUser;

View File

@ -10,7 +10,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
@ -19,6 +18,7 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.annotations.Immutable;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -172,7 +172,7 @@ public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase {
private String name;
public EntityWithEmbeddedIdWithFinalField() {
this.id = EmbeddableId.of( UUID.randomUUID().toString() );
this.id = EmbeddableId.of( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
}
public EmbeddableId getId() {

View File

@ -21,7 +21,8 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@ -95,7 +96,7 @@ public class CascadeWithFkConstraintTest extends BaseCoreFunctionalTestCase {
Set<Car> cars = new HashSet<>();
Garage() {
id = UUID.randomUUID().toString();
id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
void insert(Car aCar) {
@ -111,7 +112,7 @@ public class CascadeWithFkConstraintTest extends BaseCoreFunctionalTestCase {
String id;
Car() {
id = UUID.randomUUID().toString();
id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
}
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.batch;
import java.util.UUID;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@ -19,6 +18,7 @@ import org.hibernate.Hibernate;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
@ -40,7 +40,7 @@ import static org.junit.Assert.assertThat;
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class })
public abstract class AbstractBatchingTest extends BaseNonConfigCoreFunctionalTestCase {
protected String childName = UUID.randomUUID().toString();
protected String childName = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
protected Long parentId;
@Override

View File

@ -1,13 +1,12 @@
package org.hibernate.orm.test.entitygraph;
import java.util.List;
import java.util.UUID;
import org.hibernate.Hibernate;
import org.hibernate.jpa.SpecHints;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -39,7 +38,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
DiscriminatorColumnFetchGraphTest.Behaviour.class
}
)
@TestForIssue(jiraKey = "HHH-15622")
@JiraKey(value = "HHH-15622")
public class DiscriminatorColumnFetchGraphTest {
@BeforeEach
@ -84,7 +83,7 @@ public class DiscriminatorColumnFetchGraphTest {
@Id
@GeneratedValue(strategy = AUTO)
private UUID id;
private Long id;
@MapsId
@OneToOne(fetch = FetchType.LAZY, optional = false)
@ -123,7 +122,7 @@ public class DiscriminatorColumnFetchGraphTest {
@Id
@GeneratedValue(strategy = AUTO)
private UUID id;
private Long id;
@OneToOne(mappedBy = "behaviour", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Animal animal;

View File

@ -20,12 +20,14 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.hibernate.testing.util.ServiceRegistryUtil;
import org.junit.jupiter.api.Test;
@ -39,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
* @author Steve Ebersole
*/
@BaseUnitTest
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true, reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class GeneratedValueTest {
@Test
public void testGeneratedUuidId() throws Exception {

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.orm.test.id.uuid.annotation;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Property;
@ -13,12 +14,15 @@ import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.DomainModelScope;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@DomainModel( annotatedClasses = {TheEntity.class, TheOtherEntity.class} )
@SessionFactory
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true,
reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class UuidGeneratorAnnotationTests {
@Test
public void verifyModel(DomainModelScope scope) {

View File

@ -17,6 +17,7 @@ import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.VarcharUUIDJdbcType;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.UUIDJavaType;
@ -76,7 +77,7 @@ public class UUIDBasedIdInterpretationTest {
@RequiresDialect(H2Dialect.class)
public void testBinaryRuntimeUsage(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.byId( UuidIdEntity.class ).load( UUID.randomUUID() );
session.byId( UuidIdEntity.class ).load( SafeRandomUUIDGenerator.safeRandomUUID() );
} );
}

View File

@ -11,6 +11,7 @@ import java.util.UUID;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.persister.entity.EntityPersister;
@ -36,6 +37,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
@DomainModel(annotatedClasses = { UUIDBinaryTest.Node.class })
@SessionFactory
@SkipForDialect(dialectClass = PostgreSQLDialect.class, reason = "Postgres has its own UUID type")
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true,
reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class UUIDBinaryTest {
private static class UUIDPair {

View File

@ -13,10 +13,12 @@ import jakarta.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
@ -32,6 +34,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
annotatedClasses = CustomStrategyTest.Node.class
)
@SessionFactory
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true,
reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class CustomStrategyTest {
@Test

View File

@ -8,10 +8,12 @@ package org.hibernate.orm.test.insertordering;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.hibernate.testing.orm.junit.DialectFeatureCheck;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.junit.Test;
import jakarta.persistence.CascadeType;
@ -34,13 +36,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.mockito.Mockito.verify;
@TestForIssue(jiraKey = "HHH-16485")
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class)
@JiraKey(value = "HHH-16485")
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class InsertOrderingRCATest extends BaseNonConfigCoreFunctionalTestCase {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
@ -1306,16 +1306,16 @@ public class InsertOrderingRCATest extends BaseNonConfigCoreFunctionalTestCase {
}
@MappedSuperclass
public static abstract class BaseEntity implements Serializable {
private UUID id;
private Long id;
@Id
@Column(name = "f_id")
@GeneratedValue
public UUID getId() {
public Long getId() {
return id;
}
public void setId(UUID id) {
public void setId(Long id) {
this.id = id;
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.orm.test.insertordering;
import java.util.UUID;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
@ -20,6 +19,7 @@ import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
@ -55,10 +55,10 @@ public class InsertOrderingWithCompositeTypeAssociation {
// entity associated in the embeddable takes insert priority over the parent Book entity.
scope.inTransaction( entityManager -> {
Book bookNoComment = new Book();
bookNoComment.setId( UUID.randomUUID().toString() );
bookNoComment.setId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
Book bookWithComment = new Book();
bookWithComment.setId( UUID.randomUUID().toString() );
bookWithComment.setId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
bookWithComment.setIntermediateObject( new IntermediateObject( new Comment( "This is a comment" ) ) );
entityManager.persist( bookNoComment );
@ -114,7 +114,7 @@ public class InsertOrderingWithCompositeTypeAssociation {
}
Comment(String comment) {
this.id = UUID.randomUUID().toString();
this.id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
this.comment = comment;
}

View File

@ -11,11 +11,10 @@ import java.math.BigDecimal;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -27,7 +26,6 @@ import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
@ -38,7 +36,7 @@ import jakarta.persistence.ManyToOne;
*
* @author dcebotarenco
*/
@TestForIssue(jiraKey = "HHH-2558")
@JiraKey(value = "HHH-2558")
@DomainModel(
annotatedClasses = {
JoinedSubclassBatchingTest.Person.class,
@ -90,7 +88,7 @@ public class JoinedSubclassBatchingTest {
scope.inTransaction( s -> {
for ( int i = 0; i < nEntities; i++ ) {
Employee e = new Employee();
e.getId();
e.setId( "Employee " + i );
e.setName( "Mark" );
e.setTitle( "internal sales" );
e.setSex( 'M' );
@ -240,8 +238,6 @@ public class JoinedSubclassBatchingTest {
public static class Person {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
@Column(nullable = false, length = 80)

View File

@ -8,7 +8,9 @@ package org.hibernate.orm.test.jpa.cascade.multicircle;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
@ -29,7 +31,7 @@ public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

View File

@ -12,7 +12,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -37,6 +36,7 @@ import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Root;
import org.hibernate.Hibernate;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
@ -345,7 +345,7 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
String authorName = UUID.randomUUID().toString();
String authorName = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
Set<Integer> authorIds = IntStream.range(0, 3)
.mapToObj(v -> {
Author author = new Author(authorName);
@ -391,7 +391,7 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
String authorName = UUID.randomUUID().toString();
String authorName = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
Set<Integer> authorIds = IntStream.range(0, 3)
.mapToObj(v -> {
Author author = new Author(authorName);

View File

@ -2,10 +2,10 @@ package org.hibernate.orm.test.jpa.orphan.onetomany;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
@ -52,7 +52,7 @@ public class PersistAndQueryingInSameTransactionTest {
@Test
public void testSelectingThePersistedEntity(SessionFactoryScope scope) {
String parentId = UUID.randomUUID().toString();
String parentId = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
scope.inTransaction(
session -> {
Child child = new Child();
@ -93,7 +93,7 @@ public class PersistAndQueryingInSameTransactionTest {
@Test
public void testSelectingAndModifying(SessionFactoryScope scope) {
String parentId = UUID.randomUUID().toString();
String parentId = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
scope.inTransaction(
session -> {
Child child = new Child();

View File

@ -8,6 +8,9 @@ package org.hibernate.orm.test.jpa.query;
import java.util.Objects;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
@ -104,7 +107,7 @@ public class NonWhereQueryTest {
public AbstractEntity() {
super();
this.uuid = UUID.randomUUID();
this.uuid = SafeRandomUUIDGenerator.safeRandomUUID();
}
@Override

View File

@ -10,6 +10,7 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.HQLSelect;
import org.hibernate.dialect.SybaseASEDialect;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -83,7 +84,7 @@ public class HqlSelectTest {
@Column(name = "Uuid_id")
Long id;
java.util.UUID uuid = java.util.UUID.randomUUID();
java.util.UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
boolean deleted = false;

View File

@ -11,6 +11,8 @@ import jakarta.persistence.JoinColumn;
import jakarta.persistence.SqlResultSetMapping;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -30,9 +32,9 @@ public class SqlSelectTest {
void test(SessionFactoryScope scope) {
WithSqlSelect withSqlSelect = new WithSqlSelect();
withSqlSelect.name = "Hibernate";
withSqlSelect.uuids.add( UUID.randomUUID() );
withSqlSelect.uuids.add( UUID.randomUUID() );
withSqlSelect.uuids.add( UUID.randomUUID() );
withSqlSelect.uuids.add( SafeRandomUUIDGenerator.safeRandomUUID() );
withSqlSelect.uuids.add( SafeRandomUUIDGenerator.safeRandomUUID() );
withSqlSelect.uuids.add( SafeRandomUUIDGenerator.safeRandomUUID() );
scope.inTransaction( s -> s.persist( withSqlSelect ) );

View File

@ -7,7 +7,8 @@
package org.hibernate.orm.test.mapping.converted.converter.onetoone;
import java.io.Serializable;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
@ -45,11 +46,11 @@ public class BidirectionalOneToOneWithConverterEagerTest {
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
BarEntity bar = new BarEntity();
bar.setBusinessId( new BusinessId( UUID.randomUUID().toString() ) );
bar.setBusinessId( new BusinessId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() ) );
bar.setaDouble( 0.5 );
FooEntity foo = new FooEntity();
foo.setBusinessId( new BusinessId( UUID.randomUUID().toString() ) );
foo.setBusinessId( new BusinessId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() ) );
foo.setName( "foo_name" );
foo.setBar( bar );

View File

@ -7,7 +7,8 @@
package org.hibernate.orm.test.mapping.converted.converter.onetoone;
import java.io.Serializable;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
@ -45,11 +46,11 @@ public class BidirectionalOneToOneWithConverterLazyTest {
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
BarEntity bar = new BarEntity();
bar.setBusinessId( new BusinessId( UUID.randomUUID().toString() ) );
bar.setBusinessId( new BusinessId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() ) );
bar.setaDouble( 0.5 );
FooEntity foo = new FooEntity();
foo.setBusinessId( new BusinessId( UUID.randomUUID().toString() ) );
foo.setBusinessId( new BusinessId( SafeRandomUUIDGenerator.safeRandomUUIDAsString() ) );
foo.setName( "foo_name" );
foo.setBar( bar );

View File

@ -22,6 +22,7 @@ import org.hibernate.annotations.ValueGenerationType;
import org.hibernate.dialect.SybaseASEDialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.EventType;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.tuple.GenerationTiming;
import org.hibernate.generator.BeforeExecutionGenerator;
@ -102,7 +103,7 @@ public class GeneratedUuidTests {
@Override
public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
return UUID.randomUUID();
return SafeRandomUUIDGenerator.safeRandomUUID();
}
}
//end::mapping-generated-custom-ex3[]

View File

@ -13,8 +13,10 @@ import jakarta.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@ -23,6 +25,7 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true, reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class UuidCustomGeneratedValueTest extends BaseEntityManagerFunctionalTestCase {
@Override

View File

@ -10,10 +10,11 @@ import java.util.UUID;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@ -23,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true, reason = "Skipped for Sybase to avoid problems with UUIDs potentially ending with a trailing 0 byte")
public class UuidGeneratedValueTest extends BaseEntityManagerFunctionalTestCase {
@Override

View File

@ -14,6 +14,7 @@ import javax.money.Monetary;
import org.hibernate.SimpleNaturalIdLoadAccess;
import org.hibernate.NaturalIdMultiLoadAccess;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.metamodel.MappingMetamodel;
@ -41,7 +42,7 @@ import static org.hamcrest.Matchers.notNullValue;
@DomainModel( standardModels = StandardDomainModel.RETAIL )
@SessionFactory
public class SimpleNaturalIdTests {
private static final UUID uuid = UUID.randomUUID();
private static final UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
@BeforeEach
public void prepareTestData(SessionFactoryScope scope) {

View File

@ -1,9 +1,9 @@
package org.hibernate.orm.test.onetoone.embeddedid;
import java.io.Serializable;
import java.util.UUID;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
@ -96,12 +96,12 @@ class OneToOneWithEmbeddedIdTest {
@Embeddable
static class ID1 implements Serializable {
String id = UUID.randomUUID().toString();
String id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
@Embeddable
static class ID2 implements Serializable {
String id = UUID.randomUUID().toString();
String id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
}

View File

@ -8,13 +8,14 @@ package org.hibernate.orm.test.pagination;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -47,13 +48,13 @@ public class DistinctSelectTest extends BaseCoreFunctionalTestCase {
Transaction t = s.beginTransaction();
for (int i = 0; i < 5; i++) {
Tag tag = new Tag("Tag: " + UUID.randomUUID());
Tag tag = new Tag("Tag: " + SafeRandomUUIDGenerator.safeRandomUUID());
tags.add(tag);
s.save(tag);
}
for (int i = 0; i < NUM_OF_USERS; i++) {
Entry e = new Entry("Entry: " + UUID.randomUUID());
Entry e = new Entry("Entry: " + SafeRandomUUIDGenerator.safeRandomUUID());
e.getTags().addAll(tags);
s.save(e);
}

View File

@ -24,11 +24,11 @@ import java.util.Currency;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.UUID;
import jakarta.persistence.ParameterMode;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.query.TypedParameterValue;
import org.hibernate.type.NumericBooleanConverter;
@ -399,7 +399,7 @@ public class StoredProcedureParameterTypeTest {
scope.inTransaction(
session -> session.createStoredProcedureQuery("test")
.registerStoredProcedureParameter( 1, StandardBasicTypes.UUID_BINARY, ParameterMode.IN)
.setParameter( 1, UUID.randomUUID())
.setParameter( 1, SafeRandomUUIDGenerator.safeRandomUUID())
);
}

View File

@ -9,6 +9,8 @@ package org.hibernate.orm.test.query.hql;
import java.io.Serializable;
import java.time.LocalDateTime;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -29,7 +31,6 @@ import jakarta.persistence.Table;
import jakarta.persistence.TypedQuery;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
@ -128,7 +129,7 @@ public class SameTableAliasInSubqueryWithEmbeddedTest {
private String value;
public PrimaryKey() {
value = randomUUID().toString();
value = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
public String getValue() {

View File

@ -6,8 +6,7 @@
*/
package org.hibernate.orm.test.query.joinfetch;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.testing.jdbc.SQLStatementInspector;
@ -121,7 +120,7 @@ public class JoinResultTests {
void createTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final Vendor vendor = new Vendor( 1, "ACME", "acme", "Some notes" );
final Product product = new Product( 1, UUID.fromString( "53886a8a-7082-4879-b430-25cb94415be8" ), vendor );
final Product product = new Product( 1, SafeRandomUUIDGenerator.safeRandomUUID(), vendor );
session.persist( vendor );
session.persist( product );
} );

View File

@ -7,13 +7,13 @@
package org.hibernate.orm.test.sql.results;
import java.util.List;
import java.util.UUID;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import jakarta.persistence.Tuple;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.query.TypedTupleTransformer;
import org.hibernate.testing.orm.domain.StandardDomainModel;
@ -236,7 +236,7 @@ public class ResultsShapeTests {
final Vendor acme = new Vendor( 1, "Acme, Inc.", null );
session.persist( acme );
final Product widget = new Product( 1, UUID.randomUUID(), acme );
final Product widget = new Product( 1, SafeRandomUUIDGenerator.safeRandomUUID(), acme );
session.persist( widget );
final SalesAssociate associate = new SalesAssociate( 1, new Name( "John", "Doe" ) );

View File

@ -18,6 +18,8 @@ import org.hibernate.testing.orm.junit.SessionFactoryProducer;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.binder.internal.TenantIdBinder;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@ -38,8 +40,8 @@ import static org.junit.jupiter.api.Assertions.fail;
)
public class TenantUuidTest implements SessionFactoryProducer {
private static final UUID mine = UUID.randomUUID();
private static final UUID yours = UUID.randomUUID();
private static final UUID mine = SafeRandomUUIDGenerator.safeRandomUUID();
private static final UUID yours = SafeRandomUUIDGenerator.safeRandomUUID();
UUID currentTenant;

View File

@ -30,6 +30,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator.safeRandomUUID;
@SessionFactory
@DomainModel( annotatedClasses = { H2JsonListTest.Path.class, H2JsonListTest.PathClob.class } )
@ -39,8 +40,8 @@ public class H2JsonListTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.persist( new Path( List.of( UUID.randomUUID(), UUID.randomUUID() ) ) );
session.persist( new PathClob( List.of( UUID.randomUUID(), UUID.randomUUID() ) ) );
session.persist( new Path( List.of( safeRandomUUID(), safeRandomUUID() ) ) );
session.persist( new PathClob( List.of( safeRandomUUID(), safeRandomUUID() ) ) );
} );
}

View File

@ -27,6 +27,8 @@ import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import static org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator.safeRandomUUID;
@DomainModel(
annotatedClasses = {
UUIDTypeConverterTest.Image.class,
@ -65,7 +67,7 @@ public class UUIDTypeConverterTest {
);
scope.inTransaction(
session -> {
image.setThumbId( UUID.randomUUID() );
image.setThumbId( safeRandomUUID() );
session.merge( image );
session.flush();
}
@ -77,7 +79,7 @@ public class UUIDTypeConverterTest {
scope.inTransaction(
session -> {
Image image = session.merge( new Image() );
image.setThumbId( UUID.randomUUID() );
image.setThumbId( safeRandomUUID() );
session.merge( image );
session.flush();
}
@ -91,7 +93,7 @@ public class UUIDTypeConverterTest {
MarbleBox marbleBox = new MarbleBox( List.of( new Marble() ) );
MarbleBox saved = session.merge( marbleBox );
saved.getMarbles().get( 0 ).setMaterialId( UUID.randomUUID() );
saved.getMarbles().get( 0 ).setMaterialId( safeRandomUUID() );
session.merge( saved );
}
);
@ -110,7 +112,7 @@ public class UUIDTypeConverterTest {
scope.inTransaction(
session -> {
marbleBox.getMarbles().get( 0 ).setMaterialId( UUID.randomUUID() );
marbleBox.getMarbles().get( 0 ).setMaterialId( safeRandomUUID() );
session.merge( marbleBox );
}
);
@ -121,7 +123,7 @@ public class UUIDTypeConverterTest {
@Column(unique = true, length = 16, nullable = false)
@jakarta.persistence.Id
@Convert(converter = UuidBase64TypeConverter.class)
private UUID id = UUID.randomUUID();
private UUID id = safeRandomUUID();
}
@Entity(name = "Image")
@ -130,7 +132,7 @@ public class UUIDTypeConverterTest {
@Column(unique = true, length = 16, nullable = false)
@Convert(converter = UuidBase64TypeConverter.class)
private UUID thumbId = UUID.randomUUID();
private UUID thumbId = safeRandomUUID();
private int position;

View File

@ -2,6 +2,8 @@ package org.hibernate.orm.test.version;
import java.util.UUID;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -44,7 +46,7 @@ public class VersionedBidirectionalOneToOneMergeTest {
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
@Version
Long version;
@ -64,7 +66,7 @@ public class VersionedBidirectionalOneToOneMergeTest {
@Entity(name = "AnotherTestEntity")
public static class AnotherTestEntity {
@Id
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
String name;

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.testing.util.uuid;
import java.util.UUID;
/**
* @author Jan Schatteman
*/
public class SafeRandomUUIDGenerator {
/**
* @return A random UUID that is guaranteed not to end with a trailing 0 byte
*/
public static UUID safeRandomUUID() {
UUID uuid = UUID.randomUUID();
if ( !isSafeUUID(uuid) ) {
uuid = safeRandomUUID();
}
return uuid;
}
/**
* @return The String representation of a random UUID that is guaranteed not to end with a trailing 0 byte
*/
public static String safeRandomUUIDAsString() {
return safeRandomUUID().toString();
}
/**
* @param uuid The UUID to be tested
* @return true if the UUID doesn't end with a trailing 0 byte, false otherwise
*/
public static boolean isSafeUUID(UUID uuid) {
if ( uuid == null ) {
throw new RuntimeException("The UUID cannot be tested if it's null!");
}
return ( (uuid.getLeastSignificantBits() & 0xFF) != 0 );
}
}

View File

@ -0,0 +1,28 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.testing.util.uuid;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Jan Schatteman
*/
public class SafeRandomUUIDGeneratorTest {
@Test
public void testSafeUUIDGenerator() {
String nonZeroEnd = "53886a8a-7082-4879-b430-25cb94415be8";
String zeroEnd = "53886a8a-7082-4879-b430-25cb94415b00";
assertTrue( SafeRandomUUIDGenerator.isSafeUUID( UUID.fromString( nonZeroEnd )) );
assertFalse( SafeRandomUUIDGenerator.isSafeUUID( UUID.fromString( zeroEnd )) );
}
}