spring - Initial data on JPA repositories -
i'm looking convenient way provide initial data application. i've implemented spring data jpa based project foundation of database related operation.
example:
i've got entity role
can assigned entity user
. on clean application start provide directly default roles (e.g. admin, manager, etc).
best
i built random data factory :
public class randomdatafactory { private static final string ungenerated_value_marker = "ungenerated_value_marker"; private static void randomlypopulatefields(object object) { new randomvaluefieldpopulator().populate(object); } /** * instantiates single object random data */ public static <t> t getsingle(class<t> clazz) throws illegalaccessexception, instantiationexception { t object = clazz.newinstance(); randomlypopulatefields(object); return object; } /** * returns unmodifiable list of specified type objects random data * * @param clazz mypojo.class instantiated random data * @param maxlength length of list returned */ public static <t> list<t> getlist(class<t> clazz, int maxlength) throws illegalaccessexception, instantiationexception { list<t> list = new arraylist<t>(maxlength); (int = 0; < maxlength; i++) { t object = clazz.newinstance(); randomlypopulatefields(object); list.add(i, object); } return collections.unmodifiablelist(list); } /** * returns unmodifiable list of specified type t objects random data * <p>list length 3</p> * * @param clazz mypojo.class instantiated random data */ public static <t> list<t> getlist(class<t> clazz) throws instantiationexception, illegalaccessexception { return getlist(clazz, 3); } public static <t> t getprimitive(class<t> clazz) { return (t) randomvaluefieldpopulator.generaterandomvalue(clazz); } public static <t> list<t> getprimitivelist(class<t> clazz) { return getprimitivelist(clazz, 3); } public static <t> list<t> getprimitivelist(class<t> clazz, int length) { list<t> randoms = new arraylist<t>(length); (int = 0; < length; i++) { randoms.add(getprimitive(clazz)); } return randoms; } private static class randomvaluefieldpopulator { public static object generaterandomvalue(class<?> fieldtype) { random random = new random(); if (fieldtype.equals(string.class)) { return uuid.randomuuid().tostring(); } else if (date.class.isassignablefrom(fieldtype)) { return new date(system.currenttimemillis() - random.nextint()); } else if (localdate.class.isassignablefrom(fieldtype)) { date date = new date(system.currenttimemillis() - random.nextint()); return new localdate(date); } else if (fieldtype.equals(character.class) || fieldtype.equals(character.type)) { return (char) (random.nextint(26) + 'a'); } else if (fieldtype.equals(integer.type) || fieldtype.equals(integer.class)) { return random.nextint(); } else if (fieldtype.equals(short.type) || fieldtype.equals(short.class)) { return (short) random.nextint(); } else if (fieldtype.equals(long.type) || fieldtype.equals(long.class)) { return random.nextlong(); } else if (fieldtype.equals(float.type) || fieldtype.equals(float.class)) { return random.nextfloat(); } else if (fieldtype.equals(double.type)) { return random.nextint(); //if double used, jsonpath uses bigdecimal convert } else if (fieldtype.equals(double.class)) { return random.nextdouble(); //if double used, jsonpath uses bigdecimal convert } else if (fieldtype.equals(boolean.type) || fieldtype.equals(boolean.class)) { return random.nextboolean(); } else if (fieldtype.equals(bigdecimal.class)) { return new bigdecimal(random.nextfloat()); } else if (enum.class.isassignablefrom(fieldtype)) { object[] enumvalues = fieldtype.getenumconstants(); return enumvalues[random.nextint(enumvalues.length)]; } else if (number.class.isassignablefrom(fieldtype)) { return random.nextint(byte.max_value) + 1; } else { return ungenerated_value_marker; } public void populate(object object) { reflectionutils.dowithfields(object.getclass(), new randomvaluefieldsettercallback(object)); } private static class randomvaluefieldsettercallback implements reflectionutils.fieldcallback { private final object targetobject; public randomvaluefieldsettercallback(object targetobject) { this.targetobject = targetobject; } @override public void dowith(field field) throws illegalaccessexception { class<?> fieldtype = field.gettype(); if (!modifier.isfinal(field.getmodifiers())) { object value = generaterandomvalue(fieldtype); if (!value.equals(ungenerated_value_marker)) { reflectionutils.makeaccessible(field); field.set(targetobject, value); } } } } } }
Comments
Post a Comment