Created
May 23, 2018 03:40
-
-
Save ebongzzang/b43cd37f46243689b0a4f48f9e8050e8 to your computer and use it in GitHub Desktop.
Convert delimited string columns to Integer list in JPA
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
@Slf4j | |
@Data | |
@Entity | |
@Table(name = "ad_segment") | |
public class AdSegment { | |
@Id | |
@GeneratedValue | |
private long id; | |
private long advertisement; | |
@Convert(converter = DelimitedStringToIntListConverter.class) | |
private List<Integer> segment; | |
private boolean isExcluded; | |
} |
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
@Repository | |
public interface AdSegmentationRepo extends CrudRepository<AdSegment, Long>, QueryDslPredicateExecutor<AdSegment> { | |
} | |
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
class AdSegmentTest extends Specification { | |
static final def log = org.slf4j.LoggerFactory.getLogger(AdSegmentTest) | |
static final long TARGET_AD_ID = 11 | |
@Shared | |
DataSource datasource | |
@Autowired | |
ApplicationContext applicationContext | |
@Shared | |
AdSegmentationRepo adSegmentationRepo | |
@DbUnit(configure = { IDatabaseTester it -> | |
it.setUpOperation = DatabaseOperation.CLEAN_INSERT | |
it.tearDownOperation = DatabaseOperation.TRUNCATE_TABLE | |
}) | |
def adSegmentRows = { | |
ad_segment(id: 1, advertisement: TARGET_AD_ID, segment: "111|222|333|444", is_excluded: false) | |
ad_segment(id: 2, advertisement: TARGET_AD_ID, segment: "555|666|777|888", is_excluded: false) | |
ad_segment(id: 3, advertisement: TARGET_AD_ID, segment: "111|222", is_excluded: true) | |
} | |
def setup() { | |
datasource = applicationContext.getBean(DataSource.class) | |
adSegmentationRepo = applicationContext.getBean(AdSegmentationRepo.class) | |
} | |
def "when loading AdSegment entity, entity's segment should be Integer List "() { | |
expect: | |
expectResult.containsAll(adSegmentationRepo.findOne(expectId).getSegment()) | |
where: | |
expectId || expectResult | |
1L || Lists.newArrayList(111,222,333,444) | |
} | |
} |
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
@Converter | |
public class DelimitedStringToIntListConverter implements AttributeConverter<List<Integer>, String> { | |
private static final Splitter splitter = Splitter.on("|").omitEmptyStrings(); | |
private static final Joiner joiner = Joiner.on("|").skipNulls(); | |
@Override | |
public String convertToDatabaseColumn(List<Integer> attribute) { | |
return joiner.join(attribute); | |
} | |
@Override | |
public List<Integer> convertToEntityAttribute(String dbData) { | |
return splitter.splitToList(dbData).stream() | |
.map(Integer::valueOf) | |
.collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment