javax.persistence.UUIDToStringConverter Maven / Gradle / Ivy
package javax.persistence;
import java.util.UUID;
import org.springframework.util.StringUtils;
/**
* @see Converter#autoApply()
*/
@Converter
public class UUIDToStringConverter implements AttributeConverter {
@Override
public String convertToDatabaseColumn(UUID attribute) {
if (attribute != null) {
String dbData = attribute.toString();
return StringUtils.hasText(dbData) ? dbData : UUID.randomUUID().toString();
}
return UUID.randomUUID().toString();
}
@Override
public UUID convertToEntityAttribute(String dbData) {
return StringUtils.hasText(dbData) ? UUID.fromString(dbData) : UUID.randomUUID();
}
}